Python chainer.functions.pad_sequence() Examples
The following are 30
code examples of chainer.functions.pad_sequence().
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: training.py From espnet with Apache License 2.0 | 6 votes |
def __call__(self, batch, device): """Perform subsampling. Args: batch (list): Batch that will be sabsampled. device (chainer.backend.Device): CPU or GPU device. Returns: chainer.Variable: xp.array that are padded and subsampled from batch. xp.array: xp.array of the length of the mini-batches. chainer.Variable: xp.array that are padded and subsampled from batch. """ # For transformer, data is processed in CPU. # batch should be located in list assert len(batch) == 1 xs, ys = batch[0] xs = F.pad_sequence(xs, padding=-1).data # get batch of lengths of input sequences ilens = np.array([x.shape[0] for x in xs], dtype=np.int32) return xs, ilens, ys
Example #2
Source File: nets.py From contextual_augmentation with MIT License | 6 votes |
def embed_xs_with_prediction(self, xs, labels=None, batch='concat'): predicted_exs = self.bilm.predict_embed( xs, self.embed.W, labels=labels, dropout=self.config['dropout'], mode=self.config['mode'], temp=self.config['temp'], word_lower_bound=self.config['word_lower_bound'], gold_lower_bound=self.config['gold_lower_bound'], gumbel=self.config['gumbel'], residual=self.config['residual'], wordwise=self.config['wordwise'], add_original=self.config['add_original'], augment_ratio=self.config['augment_ratio']) if batch == 'concat': predicted_ex_block = F.pad_sequence(predicted_exs, padding=0.) predicted_ex_block = F.transpose( predicted_ex_block, (0, 2, 1))[:, :, :, None] return predicted_ex_block elif batch == 'list': return predicted_exs else: raise NotImplementedError
Example #3
Source File: extract_features.py From models with MIT License | 6 votes |
def make_batch(features, gpu): """Creates a concatenated batch from a list of data and to_gpu.""" all_input_ids = [] all_input_mask = [] all_input_type_ids = [] for feature in features: all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_input_type_ids.append(feature.input_type_ids) def stack_and_to_gpu(data_list): sdata = F.pad_sequence( data_list, length=None, padding=0).array return chainer.dataset.to_device(gpu, sdata) batch_input_ids = stack_and_to_gpu(all_input_ids).astype('i') batch_input_mask = stack_and_to_gpu(all_input_mask).astype('f') batch_input_type_ids = stack_and_to_gpu(all_input_type_ids).astype('i') return {'input_ids': batch_input_ids, 'input_mask': batch_input_mask, 'input_type_ids': batch_input_type_ids, }
Example #4
Source File: run_classifier.py From models with MIT License | 5 votes |
def make_batch(self, features, gpu): """Creates a concatenated batch from a list of data and to_gpu.""" all_input_ids = [] all_input_mask = [] all_segment_ids = [] all_label_ids = [] for feature in features: all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_segment_ids.append(feature.segment_ids) all_label_ids.append(feature.label_id) def stack_and_to_gpu(data_list): sdata = F.pad_sequence( data_list, length=None, padding=0).array return chainer.dataset.to_device(gpu, sdata) batch_input_ids = stack_and_to_gpu(all_input_ids).astype('i') batch_input_mask = stack_and_to_gpu(all_input_mask).astype('f') batch_input_segment_ids = stack_and_to_gpu(all_segment_ids).astype('i') batch_input_label_ids = stack_and_to_gpu( all_label_ids).astype('i')[:, 0] # shape should be (batch_size, ) return (batch_input_ids, batch_input_mask, batch_input_segment_ids, batch_input_label_ids)
Example #5
Source File: PadSequence.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs): y1 = F.pad_sequence(xs, padding=-1) return y1
Example #6
Source File: PadSequence.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs): y1 = F.pad_sequence(xs) y2 = y1[:, 0] return y2 # ======================================
Example #7
Source File: models.py From EEND with MIT License | 5 votes |
def forward(self, xs, activation=None): ilens = [x.shape[0] for x in xs] # xs: (B, T, F) xs = F.pad_sequence(xs, padding=-1) pad_shape = xs.shape # emb: (B*T, E) emb = self.enc(xs) # ys: (B*T, C) ys = self.linear(emb) if activation: ys = activation(ys) # ys: [(T, C), ...] ys = F.separate(ys.reshape(pad_shape[0], pad_shape[1], -1), axis=0) ys = [F.get_item(y, slice(0, ilen)) for y, ilen in zip(ys, ilens)] return ys
Example #8
Source File: test_pad_sequence.py From chainer with MIT License | 5 votes |
def check_forward(self, xs): # Non-finite values does not work for integer values. if not numpy.isfinite(self.pad) and \ numpy.dtype(self.dtype).kind != 'f': return with disable_debug_mode_if(self.can_include_nan): y = functions.pad_sequence( xs, length=self.length, padding=self.pad) self.assertEqual(y.shape, self.y_shape) for i, (length, x) in enumerate(six.moves.zip(self.lengths, self.xs)): testing.assert_allclose(y.data[i, 0:length], x) testing.assert_allclose( y.data[i, length:], self.dtype(self.pad))
Example #9
Source File: test_pad_sequence.py From chainer with MIT License | 5 votes |
def check_backward(self, xs, gy): # Numerical gradient dos not work with non-finite values. # Gradients for integer values are not defined. if not numpy.isfinite(self.pad) or numpy.dtype(self.dtype).kind != 'f': return def f(*xs): return functions.pad_sequence( xs, length=self.length, padding=self.pad) gradient_check.check_backward(f, xs, gy, dtype=numpy.float64)
Example #10
Source File: test_pad_sequence.py From chainer with MIT License | 5 votes |
def check_double_backward(self, xs, gy, ggxs): if not numpy.isfinite(self.pad) or numpy.dtype(self.dtype).kind != 'f': return def f(*xs): return functions.pad_sequence( xs, length=self.length, padding=self.pad) gradient_check.check_double_backward( f, xs, gy, ggxs, dtype=numpy.float64, **self.check_double_backward_options)
Example #11
Source File: run_squad.py From models with MIT License | 5 votes |
def make_batch(self, features, gpu): """Creates a concatenated batch from a list of data and to_gpu.""" all_unique_ids = [] all_input_ids = [] all_input_mask = [] all_segment_ids = [] all_start_positions = [] all_end_positions = [] for feature in features: all_unique_ids.append(feature.unique_id) all_input_ids.append(feature.input_ids) all_input_mask.append(feature.input_mask) all_segment_ids.append(feature.segment_ids) if self.is_training: all_start_positions.append(feature.start_position) all_end_positions.append(feature.end_position) def stack_and_to_gpu(data_list): sdata = F.pad_sequence(data_list, length=None, padding=0).array return chainer.dataset.to_device(gpu, sdata) batch_unique_ids = stack_and_to_gpu(all_unique_ids).astype('i') batch_input_ids = stack_and_to_gpu(all_input_ids).astype('i') batch_input_mask = stack_and_to_gpu(all_input_mask).astype('f') batch_input_segment_ids = stack_and_to_gpu(all_segment_ids).astype('i') if self.is_training: batch_start_positions = stack_and_to_gpu( all_start_positions).astype('i')[:, 0] # shape should be (batch_size, ) batch_end_positions = stack_and_to_gpu( all_end_positions).astype('i')[:, 0] # shape should be (batch_size, ) return (batch_input_ids, batch_input_mask, batch_input_segment_ids, batch_start_positions, batch_end_positions) else: return (batch_input_ids, batch_input_mask, batch_input_segment_ids, batch_unique_ids)
Example #12
Source File: megnet.py From chainer-chemistry with MIT License | 5 votes |
def reshaped_feat(feat, idx): """Convert node stack pattern into pad pattern This method is converting from node stack pattern to pad pattern about node and edge feature. This is because the current set2set implementation is only focus on pad pattern feature. """ xp = get_array_module(idx) max_idx = int(xp.max(idx)) vec_list = [feat[idx == i] for i in range(max_idx+1)] return functions.pad_sequence(vec_list)
Example #13
Source File: attentions.py From espnet with Apache License 2.0 | 5 votes |
def __call__(self, enc_hs, dec_z, att_prev): """Compute NoAtt forward layer. Args: enc_hs (chainer.Variable | N-dimensional array): Input variable from encoders. dec_z: Dummy. att_prev (chainer.Variable | None): Attention weight. Returns: chainer.Variable: Sum over flames. chainer.Variable: Attention weight. """ # pre-compute all h outside the decoder loop if self.pre_compute_enc_h is None: self.enc_h = F.pad_sequence(enc_hs) # utt x frame x hdim self.h_length = self.enc_h.shape[1] # initialize attention weight with uniform dist. if att_prev is None: att_prev = [ self.xp.full(hh.shape[0], 1.0 / hh.shape[0], dtype=np.float32) for hh in enc_hs ] att_prev = [chainer.Variable(att) for att in att_prev] att_prev = F.pad_sequence(att_prev) self.c = F.sum( self.enc_h * F.broadcast_to(F.expand_dims(att_prev, 2), self.enc_h.shape), axis=1, ) return self.c, att_prev
Example #14
Source File: ctc.py From espnet with Apache License 2.0 | 5 votes |
def __call__(self, hs, ys): """Core function of the Warp-CTC layer. Args: hs (iterable of chainer.Variable | N-dimention array): Input variable from encoder. ys (iterable of chainer.Variable | N-dimension array): Input variable of decoder. Returns: chainer.Variable: A variable holding a scalar value of the CTC loss. """ self.loss = None ilens = [x.shape[0] for x in hs] olens = [x.shape[0] for x in ys] # zero padding for hs y_hat = self.ctc_lo( F.dropout(F.pad_sequence(hs), ratio=self.dropout_rate), n_batch_axes=2 ) y_hat = y_hat.transpose(1, 0, 2) # batch x frames x hdim # get length info logging.info(self.__class__.__name__ + " input lengths: " + str(ilens)) logging.info(self.__class__.__name__ + " output lengths: " + str(olens)) # get ctc loss from chainer_ctc.warpctc import ctc as warp_ctc self.loss = warp_ctc(y_hat, ilens, [cuda.to_cpu(y.data) for y in ys])[0] logging.info("ctc loss:" + str(self.loss.data)) return self.loss
Example #15
Source File: ctc.py From espnet with Apache License 2.0 | 5 votes |
def log_softmax(self, hs): """Log_softmax of frame activations. Args: hs (list of chainer.Variable | N-dimension array): Input variable from encoder. Returns: chainer.Variable: A n-dimension float array. """ y_hat = self.ctc_lo(F.pad_sequence(hs), n_batch_axes=2) return F.log_softmax(y_hat.reshape(-1, y_hat.shape[-1])).reshape(y_hat.shape)
Example #16
Source File: ctc.py From espnet with Apache License 2.0 | 5 votes |
def argmax(self, hs_pad): """argmax of frame activations :param chainer variable hs_pad: 3d tensor (B, Tmax, eprojs) :return: argmax applied 2d tensor (B, Tmax) :rtype: chainer.Variable """ return F.argmax(self.ctc_lo(F.pad_sequence(hs_pad), n_batch_axes=2), axis=-1)
Example #17
Source File: decoder.py From espnet with Apache License 2.0 | 5 votes |
def forward(self, ys_pad, source, x_mask): """Forward decoder. :param xp.array e: input token ids, int64 (batch, maxlen_out) :param xp.array yy_mask: input token mask, uint8 (batch, maxlen_out) :param xp.array source: encoded memory, float32 (batch, maxlen_in, feat) :param xp.array xy_mask: encoded memory mask, uint8 (batch, maxlen_in) :return e: decoded token score before softmax (batch, maxlen_out, token) :rtype: chainer.Variable """ xp = self.xp sos = np.array([self.sos], np.int32) ys = [np.concatenate([sos, y], axis=0) for y in ys_pad] e = F.pad_sequence(ys, padding=self.eos).data e = xp.array(e) # mask preparation xy_mask = self.make_attention_mask(e, xp.array(x_mask)) yy_mask = self.make_attention_mask(e, e) yy_mask *= make_history_mask(xp, e) e = self.pe(self.embed(e)) batch, length, dims = e.shape e = e.reshape(-1, dims) source = source.reshape(-1, dims) for i in range(self.n_layers): e = self["decoders." + str(i)](e, source, xy_mask, yy_mask, batch) return self.output_layer(self.output_norm(e)).reshape(batch, length, -1)
Example #18
Source File: ctc.py From espnet with Apache License 2.0 | 5 votes |
def log_softmax(self, hs): """Log_softmax of frame activations. Args: hs (list of chainer.Variable | N-dimension array): Input variable from encoder. Returns: chainer.Variable: A n-dimension float array. """ y_hat = self.ctc_lo(F.pad_sequence(hs), n_batch_axes=2) return F.log_softmax(y_hat.reshape(-1, y_hat.shape[-1])).reshape(y_hat.shape)
Example #19
Source File: ctc.py From espnet with Apache License 2.0 | 5 votes |
def argmax(self, hs_pad): """Argmax of frame activations. :param chainer variable hs_pad: 3d tensor (B, Tmax, eprojs) :return: argmax applied 2d tensor (B, Tmax) :rtype: chainer.Variable. """ return F.argmax(self.ctc_lo(F.pad_sequence(hs_pad), n_batch_axes=2), axis=-1)
Example #20
Source File: For.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, l): inputs = F.pad_sequence(xs) h = inputs[:, 0] for time in range(l): h = inputs[:, time] return h
Example #21
Source File: Linear.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, h): inputs = F.pad_sequence(xs) gate = self.l(F.concat((inputs[:, 0], h), axis=1)) return gate
Example #22
Source File: EspNet_BLSTM.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, ilens): xs, ilens = self.blstm(xs, ilens) return F.pad_sequence(xs)
Example #23
Source File: MyLSTM.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, h, c, mask): batch_size = len(xs) lens = [x.shape[0] for x in xs] #max_len = max(lens) max_len = self.sequence_length #mask = (np.expand_dims(np.arange(max_len), 0) < # np.expand_dims(lens, 1)).astype(np.float) #h = np.zeros((batch_size, self.num_hidden), dtype=np.float32) #c = np.zeros((batch_size, self.num_hidden), dtype=np.float32) #h = self.initial_h #c = self.initial_c inputs = F.pad_sequence(xs) for time in range(max_len): x = inputs[:, time] input = F.concat((x, h), axis=1) gate = self.l(input) i = gate[:, 0:self.num_hidden] o = gate[:, self.num_hidden:self.num_hidden*2] f = gate[:, self.num_hidden*2:self.num_hidden*3] nc = gate[:, self.num_hidden*3:self.num_hidden*4] #i, o, f, nc = F.split_axis(gate, 4, axis=1) i = F.sigmoid(i) o = F.sigmoid(o) f = F.sigmoid(f) nc = F.tanh(nc) nc = f * c + i * nc nh = o * F.tanh(nc) m = mask[:, time] pmask = F.reshape(m, (self.batch_size,)) pmask = F.broadcast_to(F.expand_dims(pmask, axis=1), (self.batch_size, self.num_hidden)) nmask = 1.0 - pmask h = nh * pmask + h * nmask return h # from https://github.com/chainer/chainer/blob/master/examples/seq2seq/seq2seq.py
Example #24
Source File: EspNet_AttDot.py From chainer-compiler with MIT License | 5 votes |
def original(self, enc_hs, dec_z, att_prev, scaling=2.0): '''AttDot forward :param enc_hs: :param dec_z: :param scaling: :return: ''' batch = len(enc_hs) # pre-compute all h outside the decoder loop if self.pre_compute_enc_h is None: self.enc_h = F.pad_sequence(enc_hs) # utt x frame x hdim self.h_length = self.enc_h.shape[1] # utt x frame x att_dim self.pre_compute_enc_h = F.tanh( linear_tensor(self.mlp_enc, self.enc_h)) if dec_z is None: dec_z = chainer.Variable(self.xp.zeros( (batch, self.dunits), dtype=np.float32)) else: dec_z = F.reshape(dec_z, (batch, self.dunits)) # <phi (h_t), psi (s)> for all t u = F.broadcast_to(F.expand_dims(F.tanh(self.mlp_dec(dec_z)), 1), self.pre_compute_enc_h.shape) e = F.sum(self.pre_compute_enc_h * u, axis=2) # utt x frame # Applying a minus-large-number filter to make a probability value zero for a padded area # simply degrades the performance, and I gave up this implementation # Apply a scaling to make an attention sharp w = F.softmax(scaling * e) # weighted sum over flames # utt x hdim c = F.sum(self.enc_h * F.broadcast_to(F.expand_dims(w, 2), self.enc_h.shape), axis=1) return c, w
Example #25
Source File: EspNet_VGG2L.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, ilens): '''VGG2L forward :param xs: :param ilens: :return: ''' logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens)) # x: utt x frame x dim xs = F.pad_sequence(xs) # x: utt x 1 (input channel num) x frame x dim xs = F.swapaxes(F.reshape( xs, (xs.shape[0], xs.shape[1], self.in_channel, xs.shape[2] // self.in_channel)), 1, 2) xs = F.relu(self.conv1_1(xs)) xs = F.relu(self.conv1_2(xs)) xs = F.max_pooling_2d(xs, 2, stride=2) xs = F.relu(self.conv2_1(xs)) xs = F.relu(self.conv2_2(xs)) xs = F.max_pooling_2d(xs, 2, stride=2) # change ilens accordingly # EDIT(hamaji): ChxVM puts int32 on GPU and it hurts the performance. # TODO(hamaji): Fix device assignment to get rid of this change. ilens = (ilens + 1) // 2 ilens = (ilens + 1) // 2 # ilens = self.xp.array(self.xp.ceil(self.xp.array( # ilens, dtype=np.float32) / 2), dtype=np.int32) # ilens = self.xp.array(self.xp.ceil(self.xp.array( # ilens, dtype=np.float32) / 2), dtype=np.int32) # x: utt_list of frame (remove zeropaded frames) x (input channel num x dim) xs = F.swapaxes(xs, 1, 2) xs = F.reshape( xs, (xs.shape[0], xs.shape[1], xs.shape[2] * xs.shape[3])) xs = [xs[i, :ilens[i], :] for i in range(len(ilens))] return xs, ilens
Example #26
Source File: EspNet_VGG2L.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, ilens): xs, ilens = self.vgg(xs, ilens) return F.pad_sequence(xs)
Example #27
Source File: PadSequence.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs): y1 = F.pad_sequence(xs) return y1
Example #28
Source File: PadSequence.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs): y1 = F.pad_sequence(xs, length=20) return y1
Example #29
Source File: For.py From chainer-compiler with MIT License | 5 votes |
def forward(self, xs, l): inputs = F.pad_sequence(xs) h = inputs[:, 0] for time in range(l): h = inputs[:, time] return h
Example #30
Source File: EspNet_AttDot.py From chainer-compiler with MIT License | 5 votes |
def original(self, enc_hs, dec_z, att_prev, scaling=2.0): '''AttDot forward :param enc_hs: :param dec_z: :param scaling: :return: ''' batch = len(enc_hs) # pre-compute all h outside the decoder loop if self.pre_compute_enc_h is None: self.enc_h = F.pad_sequence(enc_hs) # utt x frame x hdim self.h_length = self.enc_h.shape[1] # utt x frame x att_dim self.pre_compute_enc_h = F.tanh( linear_tensor(self.mlp_enc, self.enc_h)) if dec_z is None: dec_z = chainer.Variable(self.xp.zeros( (batch, self.dunits), dtype=np.float32)) else: dec_z = F.reshape(dec_z, (batch, self.dunits)) # <phi (h_t), psi (s)> for all t u = F.broadcast_to(F.expand_dims(F.tanh(self.mlp_dec(dec_z)), 1), self.pre_compute_enc_h.shape) e = F.sum(self.pre_compute_enc_h * u, axis=2) # utt x frame # Applying a minus-large-number filter to make a probability value zero for a padded area # simply degrades the performance, and I gave up this implementation # Apply a scaling to make an attention sharp w = F.softmax(scaling * e) # weighted sum over flames # utt x hdim c = F.sum(self.enc_h * F.broadcast_to(F.expand_dims(w, 2), self.enc_h.shape), axis=1) return c, w