Python chainer.functions.sum() Examples
The following are 30
code examples of chainer.functions.sum().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer.functions
, or try the search function
.
Example #1
Source File: listwise.py From shoelace with MIT License | 6 votes |
def _pl_sample(t, α): """ Sample from the plackett luce distribution directly :param t: The target labels :return: A random permutation from the plackett-luce distribution parameterized by the target labels """ xp = cuda.get_array_module(t) t = t[:, 0] probs = xp.exp(t * α) probs /= xp.sum(probs) # Use CPU-based numpy implementation, because cupy.random.choice with # replace=False does not work probs = cuda.to_cpu(probs) result = np.random.choice(probs.shape[0], probs.shape[0], replace=False, p=probs) return xp.array(result, copy=False)
Example #2
Source File: nin.py From chainer-compiler with MIT License | 6 votes |
def softmax_cross_entropy(self, y, t): import numpy as np log_softmax = F.log_softmax(y) # SelectItem is not supported by onnx-chainer. # TODO(hamaji): Support it? # log_prob = F.select_item(log_softmax, t) # TODO(hamaji): Currently, F.sum with axis=1 cannot be # backpropped properly. # log_prob = F.sum(log_softmax * t, axis=1) # self.batch_size = chainer.Variable(np.array(t.size, np.float32), # name='batch_size') # return -F.sum(log_prob, axis=0) / self.batch_size log_prob = F.sum(log_softmax * t, axis=(0, 1)) batch_size = chainer.Variable(np.array(t.shape[0], np.float32), name='batch_size') self.extra_inputs = [batch_size] loss = -log_prob / batch_size loss.name = 'loss' return loss
Example #3
Source File: listwise.py From shoelace with MIT License | 6 votes |
def listmle(x, t): """ The ListMLE loss as in Xia et al (2008), Listwise Approach to Learning to Rank - Theory and Algorithm. :param x: The activation of the previous layer :param t: The target labels :return: The loss """ # Get the ground truth by sorting activations by the relevance labels xp = cuda.get_array_module(t) t_hat = t[:, 0] x_hat = x[xp.flip(xp.argsort(t_hat), axis=0)] # Compute MLE loss final = logcumsumexp(x_hat) return F.sum(final - x_hat)
Example #4
Source File: alex.py From chainer-compiler with MIT License | 6 votes |
def softmax_cross_entropy(self, y, t): import numpy as np log_softmax = F.log_softmax(y) # SelectItem is not supported by onnx-chainer. # TODO(hamaji): Support it? # log_prob = F.select_item(log_softmax, t) # TODO(hamaji): Currently, F.sum with axis=1 cannot be # backpropped properly. # log_prob = F.sum(log_softmax * t, axis=1) # self.batch_size = chainer.Variable(np.array(t.size, np.float32), # name='batch_size') # return -F.sum(log_prob, axis=0) / self.batch_size log_prob = F.sum(log_softmax * t, axis=(0, 1)) batch_size = chainer.Variable(np.array(t.shape[0], np.float32), name='batch_size') self.extra_inputs = [batch_size] loss = -log_prob / batch_size loss.name = 'loss' return loss
Example #5
Source File: resnet50.py From chainer-compiler with MIT License | 6 votes |
def softmax_cross_entropy(self, y, t): import numpy as np log_softmax = F.log_softmax(y) # SelectItem is not supported by onnx-chainer. # TODO(hamaji): Support it? # log_prob = F.select_item(log_softmax, t) # TODO(hamaji): Currently, F.sum with axis=1 cannot be # backpropped properly. # log_prob = F.sum(log_softmax * t, axis=1) # self.batch_size = chainer.Variable(np.array(t.size, np.float32), # name='batch_size') # return -F.sum(log_prob, axis=0) / self.batch_size log_prob = F.sum(log_softmax * t, axis=(0, 1)) batch_size = chainer.Variable(self.xp.array(t.shape[0], np.float32), name='batch_size') self.extra_inputs = [batch_size] loss = -log_prob / batch_size loss.name = 'loss' return loss
Example #6
Source File: listwise.py From shoelace with MIT License | 6 votes |
def listpl(x, t, α=15.0): """ The ListPL loss, a stochastic variant of ListMLE that in expectation approximates the true ListNet loss. :param x: The activation of the previous layer :param t: The target labels :param α: The smoothing factor :return: The loss """ # Sample permutation from PL(t) index = _pl_sample(t, α) x = x[index] # Compute MLE loss final = logcumsumexp(x) return F.sum(final - x)
Example #7
Source File: test_action_value.py From chainerrl with MIT License | 6 votes |
def setUp(self): def evaluator(actions): # negative square norm of actions return -F.sum(actions ** 2, axis=1) self.evaluator = evaluator if self.has_maximizer: def maximizer(): return chainer.Variable(np.zeros( (self.batch_size, self.action_size), dtype=np.float32)) else: maximizer = None self.maximizer = maximizer self.av = action_value.SingleActionValue( evaluator=evaluator, maximizer=maximizer)
Example #8
Source File: gen_mnist_mlp.py From chainer-compiler with MIT License | 6 votes |
def forward(self, x, t): xp = cuda.get_array_module(x) y = self.predictor(x) log_softmax = F.log_softmax(y) # SelectItem is not supported by onnx-chainer. # TODO(hamaji): Support it? # log_prob = F.select_item(log_softmax, t) batch_size = chainer.Variable(xp.array(t.size, xp.float32), name='batch_size') self.extra_inputs = [batch_size] # TODO(hamaji): Currently, F.sum with axis=1 cannot be # backpropped properly. # log_prob = F.sum(log_softmax * t, axis=1) # return -F.sum(log_prob, axis=0) / self.batch_size log_prob = F.sum(log_softmax * t, axis=(0, 1)) loss = -log_prob / batch_size reporter.report({'loss': loss}, self) if self.compute_accuracy: acc = accuracy.accuracy(y, xp.argmax(t, axis=1)) reporter.report({'accuracy': acc}, self) loss.name = 'loss' return loss
Example #9
Source File: dueling_dqn.py From chainerrl with MIT License | 6 votes |
def __call__(self, x): h = x for l in self.conv_layers: h = self.activation(l(h)) # Advantage batch_size = x.shape[0] ya = self.a_stream(h) mean = F.reshape( F.sum(ya, axis=1) / self.n_actions, (batch_size, 1)) ya, mean = F.broadcast(ya, mean) ya -= mean # State value ys = self.v_stream(h) ya, ys = F.broadcast(ya, ys) q = ya + ys return action_value.DiscreteActionValue(q)
Example #10
Source File: dueling_dqn.py From chainerrl with MIT License | 6 votes |
def __call__(self, x): h = x for l in self.conv_layers: h = self.activation(l(h)) # Advantage batch_size = x.shape[0] h = self.activation(self.main_stream(h)) h_a, h_v = F.split_axis(h, 2, axis=-1) ya = F.reshape(self.a_stream(h_a), (batch_size, self.n_actions, self.n_atoms)) mean = F.sum(ya, axis=1, keepdims=True) / self.n_actions ya, mean = F.broadcast(ya, mean) ya -= mean # State value ys = F.reshape(self.v_stream(h_v), (batch_size, 1, self.n_atoms)) ya, ys = F.broadcast(ya, ys) q = F.softmax(ya + ys, axis=2) return action_value.DistributionalDiscreteActionValue(q, self.z_values)
Example #11
Source File: acer.py From chainerrl with MIT License | 6 votes |
def compute_policy_gradient_full_correction( action_distrib, action_distrib_mu, action_value, v, truncation_threshold): """Compute off-policy bias correction term wrt all actions.""" assert truncation_threshold is not None assert np.isscalar(v) with chainer.no_backprop_mode(): rho_all_inv = compute_full_importance(action_distrib_mu, action_distrib) correction_weight = ( np.maximum(1 - truncation_threshold * rho_all_inv, np.zeros_like(rho_all_inv)) * action_distrib.all_prob.array[0]) correction_advantage = action_value.q_values.array[0] - v return -F.sum(correction_weight * action_distrib.all_log_prob * correction_advantage, axis=1)
Example #12
Source File: iqn.py From chainerrl with MIT License | 6 votes |
def compute_value_loss(eltwise_loss, batch_accumulator='mean'): """Compute a loss for value prediction problem. Args: eltwise_loss (Variable): Element-wise loss per example batch_accumulator (str): 'mean' or 'sum'. 'mean' will use the mean of the loss values in a batch. 'sum' will use the sum. Returns: (Variable) scalar loss """ assert batch_accumulator in ('mean', 'sum') assert eltwise_loss.ndim == 3 if batch_accumulator == 'sum': # mean over N_prime, then sum over (batch_size, N) loss = F.sum(F.mean(eltwise_loss, axis=2)) else: # mean over (batch_size, N_prime), then sum over N loss = F.sum(F.mean(eltwise_loss, axis=(0, 2))) return loss
Example #13
Source File: categorical_dqn.py From chainerrl with MIT License | 6 votes |
def compute_value_loss(eltwise_loss, batch_accumulator='mean'): """Compute a loss for value prediction problem. Args: eltwise_loss (Variable): Element-wise loss per example per atom batch_accumulator (str): 'mean' or 'sum'. 'mean' will use the mean of the loss values in a batch. 'sum' will use the sum. Returns: (Variable) scalar loss """ assert batch_accumulator in ('mean', 'sum') if batch_accumulator == 'sum': loss = F.sum(eltwise_loss) else: loss = F.mean(F.sum(eltwise_loss, axis=1)) return loss
Example #14
Source File: categorical_dqn.py From chainerrl with MIT License | 6 votes |
def compute_weighted_value_loss(eltwise_loss, batch_size, weights, batch_accumulator='mean'): """Compute a loss for value prediction problem. Args: eltwise_loss (Variable): Element-wise loss per example per atom weights (ndarray): Weights for y, t. batch_accumulator (str): 'mean' will divide loss by batchsize Returns: (Variable) scalar loss """ assert batch_accumulator in ('mean', 'sum') # eltwise_loss is (batchsize, n_atoms) array of losses # weights is an array of shape (batch_size) # sum loss across atoms and then apply weight per example in batch loss_sum = F.matmul(F.sum(eltwise_loss, axis=1), weights) if batch_accumulator == 'mean': loss = loss_sum / batch_size elif batch_accumulator == 'sum': loss = loss_sum return loss
Example #15
Source File: categorical_dqn.py From chainerrl with MIT License | 6 votes |
def _compute_loss(self, exp_batch, errors_out=None): """Compute a loss of categorical DQN.""" y, t = self._compute_y_and_t(exp_batch) # Minimize the cross entropy # y is clipped to avoid log(0) eltwise_loss = -t * F.log(F.clip(y, 1e-10, 1.)) if errors_out is not None: del errors_out[:] # The loss per example is the sum of the atom-wise loss # Prioritization by KL-divergence delta = F.sum(eltwise_loss, axis=1) delta = cuda.to_cpu(delta.array) for e in delta: errors_out.append(e) if 'weights' in exp_batch: return compute_weighted_value_loss( eltwise_loss, y.shape[0], exp_batch['weights'], batch_accumulator=self.batch_accumulator) else: return compute_value_loss( eltwise_loss, batch_accumulator=self.batch_accumulator)
Example #16
Source File: policy_output.py From async-rl with MIT License | 6 votes |
def _sample_discrete_actions(batch_probs): """Sample a batch of actions from a batch of action probabilities. Args: batch_probs (ndarray): batch of action probabilities BxA Returns: List consisting of sampled actions """ action_indices = [] # Subtract a tiny value from probabilities in order to avoid # "ValueError: sum(pvals[:-1]) > 1.0" in numpy.multinomial batch_probs = batch_probs - np.finfo(np.float32).epsneg for i in range(batch_probs.shape[0]): histogram = np.random.multinomial(1, batch_probs[i]) action_indices.append(int(np.nonzero(histogram)[0])) return action_indices
Example #17
Source File: block.py From Deep_VoiceChanger with MIT License | 6 votes |
def __call__(self, x): if self.dr: with chainer.using_config('train', True): x = F.dropout(x, self.dr) if self.gap: x = F.sum(x, axis=(2,3)) N = x.shape[0] #Below code copyed from https://github.com/pfnet-research/chainer-gan-lib/blob/master/minibatch_discrimination/net.py feature = F.reshape(F.leaky_relu(x), (N, -1)) m = F.reshape(self.md(feature), (N, self.B * self.C, 1)) m0 = F.broadcast_to(m, (N, self.B * self.C, N)) m1 = F.transpose(m0, (2, 1, 0)) d = F.absolute(F.reshape(m0 - m1, (N, self.B, self.C, N))) d = F.sum(F.exp(-F.sum(d, axis=2)), axis=2) - 1 h = F.concat([feature, d]) h = self.l(h) return h
Example #18
Source File: Sum.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x): return F.sum(x, axis=0, keepdims=True)
Example #19
Source File: Sum.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x): return x.sum(axis=1)
Example #20
Source File: chainer_functions.py From chainer-compiler with MIT License | 5 votes |
def infer_return(self, xs_type): ret_shape = list(xs_type[0].shape) ret_shape[self.axis] = sum([x_type.shape[self.axis] for x_type in xs_type]) return TyChainerVariable(dtype=xs_type[0].dtype, shape=ret_shape)
Example #21
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
def assign(self, target : 'Object'): # unimplemented temp = np.array(0) for v in dir(temp): func = values.Object( values.FuncValue(functions.UnimplementedFunction(v), target, None)) target.attributes.set_predefined_obj(str(v), func) shape_func = values.Object( values.FuncValue(NDArrayShapeFunction(), target, None)) target.attributes.set_predefined_obj('shape', shape_func) size_func = values.Object( values.FuncValue(NDArraySizeFunction(), target, None)) target.attributes.set_predefined_obj('size', size_func) cumsum_func = values.Object( values.FuncValue(NDArrayCumsumFunction(), target, None)) target.attributes.set_predefined_obj('cumsum', cumsum_func) def add_chainer_function(func): func_ = values.Object( values.FuncValue(NDArrayChainerFunction(func), target, None)) target.attributes.set_predefined_obj(func.__name__, func_) add_chainer_function(F.reshape) add_chainer_function(F.sum) add_chainer_function(F.swapaxes) add_chainer_function(F.transpose)
Example #22
Source File: char_encdec.py From knmt with GNU General Public License v3.0 | 5 votes |
def compute_h(self, dataset_as_array, train = True, use_workaround = True): dataset_as_var = [Variable(a) for a in dataset_as_array] dataset_as_emb = [self.c_emb(v) for v in dataset_as_var] hx, cx, xs = self.nstep_enc(None, None, dataset_as_emb, train = train) if self.nlayers > 1: _, last_layer = F.split_axis(hx, (self.nlayers-1,), axis = 0, force_tuple = True) else: last_layer = hx if use_workaround: zeroes = [0 * F.sum(xx) for xx in xs] for z in zeroes: last_layer += F.broadcast_to(z.reshape(1,1,1), last_layer.shape) return last_layer
Example #23
Source File: char_encdec.py From knmt with GNU General Public License v3.0 | 5 votes |
def compute_loss(self, hx, dataset_as_array_list, need_to_append_eos = True, use_gumbel = False, temperature = None, train = True, verbose = False): if need_to_append_eos: dataset_as_var_with_eos = [Variable(self.append_eos_id(a)) for a in dataset_as_array_list] else: dataset_as_var_with_eos = [Variable(a) for a in dataset_as_array_list] if need_to_append_eos: dataset_as_var_without_eos = [Variable(a) for a in dataset_as_array_list] else: dataset_as_var_without_eos = [Variable(a[:-1]) for a in dataset_as_array_list] nb_ex = len(dataset_as_array_list) dataset_as_emb_dec = [self.c_emb_dec(v) for v in dataset_as_var_without_eos] hx_dec, cx_dec, xs_dec = self.nstep_dec(hx, None, dataset_as_emb_dec, train = train) hx_initial = F.split_axis(hx.reshape(nb_ex, -1), nb_ex, axis = 0, force_tuple = True) logits_list = [self.lin_out(F.concat((hxi, h), axis = 0)) for hxi, h in zip(hx_initial, xs_dec)] if verbose: print "logits:" for logits in logits_list: print logits.data print if use_gumbel: logits_list = [(logits + self.xp.random.gumbel(size = logits.data.shape)) for logits in logits_list] if temperature is not None: logits_list = [logits/temperature for logits in logits_list] losses = [F.softmax_cross_entropy(logits, tgt) for logits,tgt in zip(logits_list, dataset_as_var_with_eos)] loss = sum(losses)/nb_ex return loss
Example #24
Source File: char_encdec.py From knmt with GNU General Public License v3.0 | 5 votes |
def compute_loss(self, hx, dataset_as_array_list, need_to_append_eos = True, use_gumbel = False, temperature = None, train = True, verbose = False): if need_to_append_eos: dataset_as_var_with_eos = [Variable(self.append_eos_id(a)) for a in dataset_as_array_list] else: dataset_as_var_with_eos = [Variable(a) for a in dataset_as_array_list] if need_to_append_eos: dataset_as_var_without_eos = [Variable(a) for a in dataset_as_array_list] else: dataset_as_var_without_eos = [Variable(a[:-1]) for a in dataset_as_array_list] nb_ex = len(dataset_as_array_list) dataset_as_emb_dec = [self.c_emb_dec(v) for v in dataset_as_var_without_eos] hx_dec, cx_dec, xs_dec = self.nstep_dec(hx, None, dataset_as_emb_dec, train = train) hx_initial = F.split_axis(hx.reshape(nb_ex, -1), nb_ex, axis = 0, force_tuple = True) logits_list = [self.lin_out(F.concat((hxi, h), axis = 0)) for hxi, h in zip(hx_initial, xs_dec)] if verbose: print "logits:" for logits in logits_list: print logits.data print if use_gumbel: logits_list = [(logits + self.xp.random.gumbel(size = logits.data.shape)) for logits in logits_list] if temperature is not None: logits_list = [logits/temperature for logits in logits_list] losses = [F.softmax_cross_entropy(logits, tgt) for logits,tgt in zip(logits_list, dataset_as_var_with_eos)] loss = sum(losses)/nb_ex return loss
Example #25
Source File: training_chainer.py From knmt with GNU General Public License v3.0 | 5 votes |
def epoch_detail(self): remaining_sub_batch_lenth = sum(len(sub_b) for sub_b in self.sub_batches[self.index_in_sub_batch:]) if self.sub_batches is not None else 0 assert remaining_sub_batch_lenth >= 0 epoch_discount = remaining_sub_batch_lenth / float(len(self.dataset)) sub_epoch_detail = self.sub_iterator.epoch_detail epoch_detail = sub_epoch_detail - epoch_discount epoch_detail = max(epoch_detail, self.epoch) return epoch_detail # epoch and is_new_epoch are updated as soon as the end of the current # sub_batch has reached a new epoch.
Example #26
Source File: policy_output.py From async-rl with MIT License | 5 votes |
def entropy(self): return - F.sum(self.probs * self.log_probs, axis=1)
Example #27
Source File: main.py From qb with MIT License | 5 votes |
def __call__(self, xs): xs = self.embed(xs) batch_size, length, _ = xs.shape h = F.sum(xs, axis=1) / length h = F.tanh(self.linear1(h)) h = F.tanh(self.linear2(h)) return h
Example #28
Source File: invert_diff.py From ssai-cnn with MIT License | 5 votes |
def __call__(self, x): y = self.extract_feature(x) e = F.mean_squared_error(self.y0, y) tv = self.tv_norm(x) self.loss = (self.lambda_euc * float(self.y0.data.size) * e + self.args.lambda_tv * tv + self.args.lambda_lp * F.sum(x ** self.args.p)) return self.loss
Example #29
Source File: invert_diff.py From ssai-cnn with MIT License | 5 votes |
def tv_norm(self, x): diffh = self.tvh( F.reshape(x, (3, 1, self.args.in_size, self.args.in_size))) diffw = self.tvw( F.reshape(x, (3, 1, self.args.in_size, self.args.in_size))) tv = (F.sum(diffh ** 2) + F.sum(diffw ** 2)) ** (self.args.beta / 2.) return tv
Example #30
Source File: seq2seq.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, ys): xs = [x[::-1] for x in xs] eos = self.xp.array([EOS], numpy.int32) ys_in = [F.concat([eos, y], axis=0) for y in ys] ys_out = [F.concat([y, eos], axis=0) for y in ys] # Both xs and ys_in are lists of arrays. exs = sequence_embed(self.embed_x, xs) eys = sequence_embed(self.embed_y, ys_in) batch = len(xs) # None represents a zero vector in an encoder. hx, cx, _ = self.encoder(None, None, exs) _, _, os = self.decoder(hx, cx, eys) # It is faster to concatenate data before calculating loss # because only one matrix multiplication is called. concat_os = F.concat(os, axis=0) concat_ys_out = F.concat(ys_out, axis=0) loss = F.sum(F.softmax_cross_entropy( self.W(concat_os), concat_ys_out, reduce='no')) / batch chainer.report({'loss': loss.data}, self) n_words = concat_ys_out.shape[0] perp = self.xp.exp(loss.data * batch / n_words) chainer.report({'perp': perp}, self) return loss # from https://github.com/chainer/chainer/blob/master/examples/seq2seq/seq2seq.py