Skip to main content
 首页 » 编程设计

python之在 Tensorflow 中检索 LSTM 序列的最后一个值

2024年10月17日26unruledboy

我有不同长度的序列,我想在 Tensorflow 中使用 LSTM 对其进行分类。对于分类,我只需要每个序列最后一个时间步长的 LSTM 输出。

max_length = 10 
n_dims = 2 
layer_units = 5 
input = tf.placeholder(tf.float32, [None, max_length, n_dims]) 
lengths =  tf.placeholder(tf.int32, [None]) 
cell = tf.nn.rnn_cell.LSTMCell(num_units=layer_units, state_is_tuple=True) 
 
sequence_outputs, last_states = tf.nn.dynamic_rnn(cell, sequence_length=lengths, inputs=input) 

我想用 NumPy 表示法得到:output = sequence_outputs[:,lengths]

有什么方法或解决方法可以在 Tensorflow 中实现这种行为吗?

---更新---

关注这篇文章How to select rows from a 3-D Tensor in TensorFlow?似乎可以使用 tf.gather 和操作索引以有效的方式解决问题。唯一的要求是必须事先知道批量大小。这是对这个具体问题的引用帖子的改编:

max_length = 10 
n_dims = 2 
layer_units = 5 
batch_size = 2 
input = tf.placeholder(tf.float32, [batch_size, max_length, n_dims]) 
lengths =  tf.placeholder(tf.int32, [batch_size]) 
cell = tf.nn.rnn_cell.LSTMCell(num_units=layer_units, state_is_tuple=True) 
 
sequence_outputs, last_states = tf.nn.dynamic_rnn(cell, 
                                                  sequence_length=lengths, inputs=input) 
 
#Code adapted from @mrry response in StackOverflow: 
#https://stackoverflow.com/questions/36088277/how-to-select-rows-from-a-3-d-tensor-in-tensorflow 
rows_per_batch = tf.shape(input)[1] 
indices_per_batch = 1 
 
# Offset to add to each row in indices. We use `tf.expand_dims()` to make  
# this broadcast appropriately. 
offset = tf.range(0, batch_size) * rows_per_batch 
 
# Convert indices and logits into appropriate form for `tf.gather()`.  
flattened_indices = lengths - 1 + offset 
flattened_sequence_outputs = tf.reshape(self.sequence_outputs, tf.concat(0, [[-1], 
                             tf.shape(sequence_outputs)[2:]])) 
 
selected_rows = tf.gather(flattened_sequence_outputs, flattened_indices) 
last_output  = tf.reshape(selected_rows, 
                          tf.concat(0, [tf.pack([batch_size, indices_per_batch]), 
                          tf.shape(self.sequence_outputs)[2:]])) 

@petrux 选项 ( Get the last output of a dynamic_rnn in TensorFlow ) 似乎也有效,但在 for 循环中构建列表的需求可能没有那么优化,尽管我没有执行任何基准测试来支持此声明。

请您参考如下方法:

This可能是一个答案。我不认为有什么类似于你指出的 NumPy 符号,但效果是一样的。