Python chainer.functions.broadcast_to() Examples
The following are 30
code examples of chainer.functions.broadcast_to().
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: 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 #2
Source File: decoder.py From knmt with GNU General Public License v3.0 | 6 votes |
def get_initial_logits(self, mb_size = None): if mb_size is None: mb_size = self.src_mb_size else: assert self.src_mb_size == 1 assert mb_size is not None bos_encoding = F.broadcast_to(self.decoder_chain.bos_encoding, (mb_size, 1, self.decoder_chain.d_model)) cross_mask = self.decoder_chain.xp.broadcast_to(self.mask_input[:,0:1,0:1,:], (self.mask_input.shape[0], self.decoder_chain.n_heads, 1, self.mask_input.shape[3])) final_layer, prev_states = self.decoder_chain.encoding_layers.one_step(bos_encoding, None, self.src_encoding, cross_mask) logits = self.decoder_chain.logits_layer(F.reshape(final_layer, (mb_size, self.decoder_chain.d_model))) return logits, DecoderState(pos=-1, prev_states=prev_states)
Example #3
Source File: cnn.py From fpl with MIT License | 6 votes |
def __call__(self, inputs): pos_x, pos_y, offset_x, ego_x, ego_y, pose_x, pose_y = self._prepare_input(inputs) batch_size, past_len, _ = pos_x.shape h_pos = self.pos_encoder(pos_x) h_pose = self.pose_encoder(pose_x) h_ego = self.ego_encoder(ego_x) h = F.concat((h_pos, h_pose, h_ego), axis=1) # (B, C, 2) h = self.inter(h) h_pos = self.pos_decoder(h) pred_y = self.last(h_pos) # (B, 10, C+6+28) pred_y = F.swapaxes(pred_y, 1, 2) pred_y = pred_y[:, :pos_y.shape[1], :] loss = F.mean_squared_error(pred_y, pos_y) pred_y = pred_y + F.broadcast_to(F.expand_dims(offset_x, 1), pred_y.shape) pred_y = cuda.to_cpu(pred_y.data) * self._std + self._mean return loss, pred_y, None
Example #4
Source File: decoder.py From knmt with GNU General Public License v3.0 | 6 votes |
def compute_logits(self, seq_list, encoded_input, mask_input): mb_size = len(seq_list) max_length_1 = max(len(x) for x in seq_list) x, mask = self.make_batch(seq_list) # print "padded_data", x # print "mask", mask assert self.xp.all(mask_input == self.xp.broadcast_to(mask_input[:,0:1,0:1,:], mask_input.shape)) encoded = self.emb(x) encoded += self.get_pos_vect(mb_size, max_length_1) if self.dropout is not None: encoded = F.dropout(encoded, self.dropout) bos_plus_encoded = F.concat((F.broadcast_to(self.bos_encoding, (mb_size, 1, self.d_model)), encoded), axis=1) cross_mask = self.xp.broadcast_to(mask_input[:,0:1,0:1,:], (mask_input.shape[0], self.n_heads, bos_plus_encoded.data.shape[1], mask_input.shape[3])) final_layer = self.encoding_layers(bos_plus_encoded, encoded_input, mask, cross_mask) logits = apply_linear_layer_to_last_dims(final_layer, self.logits_layer) return logits
Example #5
Source File: cnn.py From fpl with MIT License | 6 votes |
def __call__(self, inputs): pos_x, pos_y, offset_x, ego_x, ego_y, pose_x, pose_y = self._prepare_input(inputs) batch_size, past_len, _ = pos_x.shape h_pos = self.pos_encoder(pos_x) h_pose = self.pose_encoder(pose_x) h = F.concat((h_pos, h_pose), axis=1) # (B, C, 2) h = self.inter(h) h_pos = self.pos_decoder(h) pred_y = self.last(h_pos) # (B, 10, C+6+28) pred_y = F.swapaxes(pred_y, 1, 2) pred_y = pred_y[:, :pos_y.shape[1], :] loss = F.mean_squared_error(pred_y, pos_y) pred_y = pred_y + F.broadcast_to(F.expand_dims(offset_x, 1), pred_y.shape) pred_y = cuda.to_cpu(pred_y.data) * self._std + self._mean return loss, pred_y, None
Example #6
Source File: net.py From convolutional_seq2seq with BSD 3-Clause "New" or "Revised" License | 6 votes |
def attend(self, query, key, value, mask, minfs=None): """ Input shapes: q=(b, units, dec_l), k=(b, units, enc_l), v=(b, units, dec_l, enc_l), m=(b, dec_l, enc_l) """ # Calculate Attention Scores with Mask for Zero-padded Areas pre_a = F.batch_matmul(query, key, transa=True) # (b, dec_l, enc_l) minfs = self.xp.full(pre_a.shape, -np.inf, pre_a.dtype) \ if minfs is None else minfs pre_a = F.where(mask, pre_a, minfs) a = F.softmax(pre_a, axis=2) # if values in axis=2 are all -inf, they become nan. thus do re-mask. a = F.where(self.xp.isnan(a.data), self.xp.zeros(a.shape, dtype=a.dtype), a) reshaped_a = a[:, None] # (b, 1, dec_xl, enc_l) # Calculate Weighted Sum pre_c = F.broadcast_to(reshaped_a, value.shape) * value c = F.sum(pre_c, axis=3, keepdims=True) # (b, units, dec_xl, 1) return c
Example #7
Source File: cnn.py From fpl with MIT License | 6 votes |
def __call__(self, inputs): pos_x, pos_y, offset_x, ego_x, ego_y, pose_x, pose_y = self._prepare_input(inputs) batch_size, past_len, _ = pos_x.shape h_pos = self.pos_encoder(pos_x) h_ego = self.ego_encoder(ego_x) h = F.concat((h_pos, h_ego), axis=1) # (B, C, 2) h = self.inter(h) h_pos = self.pos_decoder(h) pred_y = self.last(h_pos) # (B, 10, C+6+28) pred_y = F.swapaxes(pred_y, 1, 2) pred_y = pred_y[:, :pos_y.shape[1], :] loss = F.mean_squared_error(pred_y, pos_y) pred_y = pred_y + F.broadcast_to(F.expand_dims(offset_x, 1), pred_y.shape) pred_y = cuda.to_cpu(pred_y.data) * self._std + self._mean return loss, pred_y, None
Example #8
Source File: iqn.py From chainerrl with MIT License | 6 votes |
def _evaluate_psi_x_with_quantile_thresholds(psi_x, phi, f, taus): assert psi_x.ndim == 2 batch_size, hidden_size = psi_x.shape assert taus.ndim == 2 assert taus.shape[0] == batch_size n_taus = taus.shape[1] phi_taus = phi(taus) assert phi_taus.ndim == 3 assert phi_taus.shape == (batch_size, n_taus, hidden_size) psi_x_b = F.broadcast_to( F.expand_dims(psi_x, axis=1), phi_taus.shape) h = psi_x_b * phi_taus h = F.reshape(h, (-1, hidden_size)) assert h.shape == (batch_size * n_taus, hidden_size) h = f(h) assert h.ndim == 2 assert h.shape[0] == batch_size * n_taus n_actions = h.shape[-1] h = F.reshape(h, (batch_size, n_taus, n_actions)) return QuantileDiscreteActionValue(h)
Example #9
Source File: net.py From chainer with MIT License | 5 votes |
def __call__(self, x): q_z = self.encoder(x) z = q_z.sample(self.k) p_x = self.decoder(z) p_z = self.prior() reconstr = F.mean(p_x.log_prob( F.broadcast_to(x[None, :], (self.k,) + x.shape))) kl_penalty = F.mean(chainer.kl_divergence(q_z, p_z)) loss = - (reconstr - self.beta * kl_penalty) reporter.report({'loss': loss}, self) reporter.report({'reconstr': reconstr}, self) reporter.report({'kl_penalty': kl_penalty}, self) return loss
Example #10
Source File: perspective.py From neural_renderer with MIT License | 5 votes |
def perspective(vertices, angle=30.): assert (vertices.ndim == 3) xp = chainer.cuda.get_array_module(vertices) if isinstance(angle, float) or isinstance(angle, int): angle = chainer.Variable(xp.array(angle, 'float32')) angle = angle / 180. * 3.1416 angle = cf.broadcast_to(angle[None], (vertices.shape[0],)) width = cf.tan(angle) width = cf.broadcast_to(width[:, None], vertices.shape[:2]) z = vertices[:, :, 2] x = vertices[:, :, 0] / z / width y = vertices[:, :, 1] / z / width vertices = cf.concat((x[:, :, None], y[:, :, None], z[:, :, None]), axis=2) return vertices
Example #11
Source File: mesh.py From neural_renderer with MIT License | 5 votes |
def get_batch(self, batch_size): # broadcast for minibatch vertices = cf.broadcast_to(self.vertices, [batch_size] + list(self.vertices.shape)) faces = cf.broadcast_to(self.faces, [batch_size] + list(self.faces.shape)).data textures = cf.sigmoid(cf.broadcast_to(self.textures, [batch_size] + list(self.textures.shape))) return vertices, faces, textures
Example #12
Source File: look_at.py From neural_renderer with MIT License | 5 votes |
def look_at(vertices, eye, at=None, up=None): """ "Look at" transformation of vertices. """ assert (vertices.ndim == 3) xp = chainer.cuda.get_array_module(vertices) batch_size = vertices.shape[0] if at is None: at = xp.array([0, 0, 0], 'float32') if up is None: up = xp.array([0, 1, 0], 'float32') if isinstance(eye, list) or isinstance(eye, tuple): eye = xp.array(eye, 'float32') if eye.ndim == 1: eye = cf.tile(eye[None, :], (batch_size, 1)) if at.ndim == 1: at = cf.tile(at[None, :], (batch_size, 1)) if up.ndim == 1: up = cf.tile(up[None, :], (batch_size, 1)) # create new axes z_axis = cf.normalize(at - eye) x_axis = cf.normalize(neural_renderer.cross(up, z_axis)) y_axis = cf.normalize(neural_renderer.cross(z_axis, x_axis)) # create rotation matrix: [bs, 3, 3] r = cf.concat((x_axis[:, None, :], y_axis[:, None, :], z_axis[:, None, :]), axis=1) if r.shape[0] != vertices.shape[0]: r = cf.broadcast_to(r, (vertices.shape[0], 3, 3)) # apply # [bs, nv, 3] -> [bs, nv, 3] -> [bs, nv, 3] if vertices.shape != eye.shape: eye = cf.broadcast_to(eye[:, None, :], vertices.shape) vertices = vertices - eye vertices = cf.matmul(vertices, r, transb=True) return vertices
Example #13
Source File: mlp_decoder.py From models with MIT License | 5 votes |
def forward(self, inputs, rel_type, rel_rec, rel_send, pred_steps=1): # NOTE: Assumes that we have the same graph across all samples. # inputs: [batch_size, num_nodes, timesteps, feature_dim] inputs = inputs.transpose(0, 2, 1, 3) batch_size, timesteps = inputs.shape[:2] # inputs: [batch_size, timesteps, num_nodes, feature_dim] # rel_type: [batch_size, num_edges, edge_types] _, num_edges, edge_types = rel_type.shape # Repeat rel_type "timesteps" times rel_type = F.broadcast_to( rel_type[:, None, :, :], [batch_size, timesteps, num_edges, edge_types]) assert (pred_steps <= timesteps) # Only take n-th timesteps as starting points (n: pred_steps) last_pred = inputs[:, 0::pred_steps, :, :] curr_rel_type = rel_type[:, 0::pred_steps, :, :] _, num_sub_sequences, _, _ = last_pred.shape # NOTE: Assumes rel_type is constant (i.e. same across all time steps). preds = [[] for _ in range(num_sub_sequences)] # Run n prediction steps for _ in range(pred_steps): last_pred = self.single_step_forward(last_pred, rel_rec, rel_send, curr_rel_type) for seq_i in range(num_sub_sequences): preds[seq_i].append(last_pred[:, seq_i:seq_i + 1, :, :]) for seq_i in range(num_sub_sequences): preds[seq_i] = F.concat(preds[seq_i], axis=1) preds = F.concat(preds, axis=1) pred_all = preds[:, :(inputs.shape[1] - 1), :, :] return pred_all.transpose(0, 2, 1, 3)
Example #14
Source File: model_py.py From models with MIT License | 5 votes |
def __call__(self, x): # chainer requires explicit broadcast for avoiding latent bugs u = F.mean(x, -1, keepdims=True) u = F.broadcast_to(u, x.shape) s = F.mean((x - u) ** 2, -1, keepdims=True) s = F.broadcast_to(s, x.shape) x = (x - u) / F.sqrt(s + self.e) return F.bias(F.scale(x, self.g, axis=2), self.b, axis=2)
Example #15
Source File: lm_nets.py From models with MIT License | 5 votes |
def norm_by_freq(self, freq): word_embs = self.W mean = F.sum(freq * word_embs, axis=0, keepdims=True) mean = F.broadcast_to(mean, word_embs.shape) var = F.sum(freq * ((word_embs - mean) ** 2), axis=0, keepdims=True) var = F.broadcast_to(var, word_embs.shape) stddev = F.sqrt(1e-6 + var) word_embs_norm = (word_embs - mean) / stddev return word_embs_norm
Example #16
Source File: lm_nets.py From models with MIT License | 5 votes |
def __call__(self, x): out = F.linear(x, self.W, self.b) if self.scale is not None: out *= F.broadcast_to(self.scale[None], out.shape) return out
Example #17
Source File: positional_encoding.py From models with MIT License | 5 votes |
def __call__(self, x): positional_embedding = chainer.Variable(self.positional_embedding[:, :x.shape[1]], requires_grad=False) positional_embedding.to_device(self.device) positional_embedding = F.broadcast_to(positional_embedding, (len(x),) + positional_embedding.shape[1:]) x = x + positional_embedding return F.dropout(x, ratio=self.dropout_ratio)
Example #18
Source File: train_word2vec.py From chainer with MIT License | 5 votes |
def forward(self, x, contexts): e = self.embed(contexts) batch_size, n_context, n_units = e.shape x = F.broadcast_to(x[:, None], (batch_size, n_context)) e = F.reshape(e, (batch_size * n_context, n_units)) x = F.reshape(x, (batch_size * n_context,)) loss = self.loss_func(e, x) reporter.report({'loss': loss}, self) return loss
Example #19
Source File: word.py From vecto with Mozilla Public License 2.0 | 5 votes |
def __call__(self, x, context): context = context + 2 # plus 2 for OOV and end symbol. e = self.embed(context) shape = e.shape x = F.broadcast_to(x[:, None], (shape[0], shape[1])) e = F.reshape(e, (shape[0] * shape[1], shape[2])) x = F.reshape(x, (shape[0] * shape[1],)) loss = self.loss_func(e, x) # shouldn't we divide loss by batch size? reporter.report({'loss': loss}, self) return loss
Example #20
Source File: network.py From chainer-PGGAN with MIT License | 5 votes |
def __call__(self, x): h = x * self.c h = self.conv(h) if self.normalize: mean = F.mean(h*h, axis=1, keepdims=True) dom = my_rsqrt(mean + self.eps) dom = F.broadcast_to(dom, h.shape) h = h * dom return h
Example #21
Source File: subword.py From vecto with Mozilla Public License 2.0 | 5 votes |
def __call__(self, x, context, tokenIdsList_merged, tokenIdsList_merged_b, argsort, argsort_reverse, pList): # print(tokenIdsListListList.shape) # #batchs * #contexts * #ngrams * #tokens n_batch = x.shape[0] n_ngram = self.n_ngram n_context = int(len(tokenIdsList_merged) / n_ngram / n_batch) x = F.broadcast_to(x[:, None], (n_batch, n_context)) x = F.reshape(x, (n_batch * n_context,)) # print(x.shape) loss_total = 0 if self.f is not None: e = self.f(tokenIdsList_merged, tokenIdsList_merged_b, argsort, argsort_reverse, pList) # print(e.shape) loss = self.loss_func(e, x) reporter.report({'loss': loss}, self) loss_total += loss if self.word_embed is not None: context = context + 2 # plus 2 for OOV and end symbol. e = self.word_embed(context) e = F.reshape(e, (e.shape[0] * e.shape[1], e.shape[2])) # print(e.shape) loss = self.loss_func(e, x) reporter.report({'loss': loss}, self) loss_total += loss return loss_total
Example #22
Source File: BroadcastTo.py From chainer-compiler with MIT License | 5 votes |
def forward(self, x): x = self.l1(x) x = F.reshape(x, (6, 5, 1)) y = F.broadcast_to(x, (2, 6, 5, 3)) return y # ======================================
Example #23
Source File: sinet.py From imgclsmob with MIT License | 5 votes |
def __call__(self, x): w = F.average_pooling_2d(x, ksize=x.shape[2:]) w = self.fc1(w) if self.use_conv2: w = self.activ(w) w = self.fc2(w) w = self.sigmoid(w) w = F.broadcast_to(F.expand_dims(F.expand_dims(w, axis=2), axis=3), x.shape) x = x * w return x
Example #24
Source File: sinet.py From imgclsmob with MIT License | 5 votes |
def __call__(self, x, y): x = self.up(x) x = self.bn(x) w_conf = F.softmax(x) w_max = F.broadcast_to(F.expand_dims(F.max(w_conf, axis=1), axis=1), x.shape) x = y * (1 - w_max) + x return x
Example #25
Source File: bamresnet.py From imgclsmob with MIT License | 5 votes |
def __call__(self, x): input_shape = x.shape x = F.average_pooling_2d(x, ksize=x.shape[2:]) x = F.reshape(x, shape=(x.shape[0], -1)) x = self.init_fc(x) x = self.main_fcs(x) x = self.final_fc(x) x = F.broadcast_to(F.expand_dims(F.expand_dims(x, axis=2), axis=3), input_shape) return x
Example #26
Source File: bamresnet.py From imgclsmob with MIT License | 5 votes |
def __call__(self, x): input_shape = x.shape x = self.init_conv(x) x = self.dil_convs(x) x = self.final_conv(x) x = F.broadcast_to(x, input_shape) return x
Example #27
Source File: cnn.py From fpl with MIT License | 5 votes |
def __call__(self, inputs): pos_x, pos_y, offset_x, ego_x, ego_y, pose_x, pose_y = self._prepare_input(inputs) batch_size, past_len, _ = pos_x.shape h = self.pos_encoder(pos_x) h = self.inter(h) h = self.pos_decoder(h) pred_y = self.last(h) pred_y = F.swapaxes(pred_y, 1, 2) pred_y = pred_y[:, :pos_y.shape[1], :] loss = F.mean_squared_error(pred_y, pos_y) pred_y = pred_y + F.broadcast_to(F.expand_dims(offset_x, 1), pred_y.shape) pred_y = cuda.to_cpu(pred_y.data) * self._std + self._mean return loss, pred_y, None
Example #28
Source File: cbamresnet.py From imgclsmob with MIT License | 5 votes |
def __call__(self, x): att1 = F.average_pooling_2d(x, ksize=x.shape[2:]) att1 = self.mlp(att1) att2 = F.max_pooling_2d(x, ksize=x.shape[2:]) att2 = self.mlp(att2) att = att1 + att2 att = F.sigmoid(att) att = F.broadcast_to(F.expand_dims(F.expand_dims(att, axis=2), axis=3), x.shape) x = x * att return x
Example #29
Source File: cnn.py From fpl with MIT License | 5 votes |
def _prepare_input(self, inputs): pos_x, pos_y, poses, egomotions = inputs[:4] if pos_y.data.ndim == 2: pos_x = F.expand_dims(pos_x, 0) pos_y = F.expand_dims(pos_y, 0) if egomotions is not None: egomotions = F.expand_dims(egomotions, 0) poses = F.expand_dims(poses, 0) # Locations # Note: prediction target is displacement from last input x = (pos_x - F.broadcast_to(self.mean, pos_x.shape)) / F.broadcast_to(self.std, pos_x.shape) y = (pos_y - F.broadcast_to(self.mean, pos_y.shape)) / F.broadcast_to(self.std, pos_y.shape) y = y - F.broadcast_to(x[:, -1:, :], pos_y.shape) # Egomotions past_len = pos_x.shape[1] if egomotions is not None: ego_x = egomotions[:, :past_len, :] ego_y = egomotions[:, past_len:, :] # Poses poses = F.reshape(poses, (poses.shape[0], poses.shape[1], -1)) pose_x = poses[:, :past_len, :] pose_y = poses[:, past_len:, :] if egomotions is not None: return x, y, x[:, -1, :], ego_x, ego_y, pose_x, pose_y else: return x, y, x[:, -1, :], None, None, pose_x, pose_y
Example #30
Source File: network.py From chainer-PGGAN with MIT License | 5 votes |
def __call__(self, x): mean = F.mean(x, axis=0, keepdims=True) dev = x - F.broadcast_to(mean, x.shape) devdev = dev * dev var = F.mean(devdev, axis=0, keepdims=True) # using variance instead of stddev # stddev = my_sqrt(var + self.eps) # stddev_mean = F.mean(stddev) stddev_mean = F.mean(var) new_channel = F.broadcast_to(stddev_mean, (x.shape[0], 1, x.shape[2], x.shape[3])) h = F.concat((x, new_channel), axis=1) return h