def forward(self, sequences_batch, sequences_lengths): """ Args: sequences_batch: A batch of variable length sequences of vectors. The batch is assumed to be of size (batch, sequence, vector_dim). sequences_lengths: A 1D tensor containing the sizes of the sequences in the input batch. Returns: reordered_outputs: The outputs (hidden states) of the encoder for the sequences in the input batch, in the same order. """ sorted_batch, sorted_lengths, _, restoration_idx =\ sort_by_seq_lens(sequences_batch, sequences_lengths) packed_batch = nn.utils.rnn.pack_padded_sequence(sorted_batch, sorted_lengths, batch_first=True) outputs, _ = self._encoder(packed_batch, None) outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs, batch_first=True) reordered_outputs = outputs.index_select(0, restoration_idx) return reordered_outputs
def forward(self, sequence_batch, sequence_lengths): sorted_batch, sorted_seq_lens, _, restoration_index = sort_by_seq_lens( sequence_batch, sequence_lengths) packed_batch = nn.utils.rnn.pack_padded_sequence(sorted_batch, sorted_seq_lens, batch_first=True) output, _ = self.encoder(packed_batch) output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True) return output.index_select(0, restoration_index)
def forward(self, sequences_batch, sequnces_lengths): # 用于处理 padding token max_len = sequences_batch.shape[1] sorted_batch, sorted_length, _, restoration_idx = sort_by_seq_lens(sequences_batch, sequnces_lengths) #长度排序 packed_batch = nn.utils.rnn.pack_padded_sequence(sorted_batch, sorted_length, batch_first=True) #压缩 outputs, _ = self._encoder(packed_batch) # LSTM outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs, batch_first=True, total_length=max_len) #还原 [batch_size, max_len, dim] # restore order reorder_outputs = outputs.index_select(0, restoration_idx) #还原顺序 return reorder_outputs
def forward(self, sequences_batch, sequences_lengths): # 对batch中的序列进行排序,得到排序后的batch和恢复序列 sorted_batch, sorted_lengths, _, restoration_idx = sort_by_seq_lens(sequences_batch, sequences_lengths) # 排序 packed_batch = nn.utils.rnn.pack_padded_sequence(sorted_batch, sorted_lengths, batch_first=True) # pack outputs, _ = self.encoder(packed_batch, None) # LSTM 运算 outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs, batch_first=True) # pad数据,恢复成padding等长的结构 # for linux # 恢复其索引序列(先前不是排序了嘛,现在需要恢复回去) reordered_outputs = outputs.index_select(0, restoration_idx) """ for win10 reordered_outputs = outputs.index_select(0, restoration_idx.long()) """ return reordered_outputs
def forward(self, sequences_batch, sequences_lengths): sorted_batch, sorted_lengths, _, restoration_idx = sort_by_seq_lens( sequences_batch, sequences_lengths) packed_batch = nn.utils.rnn.pack_padded_sequence(sorted_batch, sorted_lengths, batch_first=True) outputs, _ = self.encoder(packed_batch, None) outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs, batch_first=True) # for linux if platform == "linux" or platform == "linux2": reordered_outputs = outputs.index_select(0, restoration_idx) # for win10 else: reordered_outputs = outputs.index_select(0, restoration_idx.long()) return reordered_outputs