我有不同长度的序列,想在 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 中实现这种行为吗?
- -更新 - -
关注此帖子如何从 TensorFlow 中的 3-D 张量中选择行? https://stackoverflow.com/questions/36088277/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 选项(获取 TensorFlow 中动态_rnn 的最后输出 https://stackoverflow.com/questions/41273361/get-th-last-output-of-a-dynamic-rnn-in-tensorflow/41273737#41273737)似乎也可以工作,但在 for 循环中构建列表的需要可能不太优化,尽管我没有执行任何基准测试来支持此语句。