检索Tensorflow中LSTM序列的最后一个值 [英] Retrieving last value of LSTM sequence in Tensorflow

查看:93
本文介绍了检索Tensorflow中LSTM序列的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have sequences of different lengths that I want to classify using LSTMs in Tensorflow. For the classification I just need the LSTM output of the last timestep of each sequence.

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]

I would like to get, in NumPy notation: output = sequence_outputs[:,lengths]

是否有任何方法或解决方法在Tensorflow中获得这种行为?

Is there any way or workaround to get this behaviour in Tensorflow?

-更新---

此帖子后面如何选择行从TensorFlow中的3-D Tensor中获得?似乎可以通过tf.gather和操纵索引来有效地解决问题.唯一的要求是必须预先知道批次大小.这是引荐帖子针对此具体问题的改编:

Following this post How to select rows from a 3-D Tensor in TensorFlow? it seems that is possible to solve the problem in an efficient manner with tf.gather and manipulating the indices. The only requirement is that the batch size must be known in advance. Here is the adaptation of the referred post to this concrete problem:

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中的dynamic_rnn的最后输出)似乎也可以工作,但是在for循环中构建列表的需求可能没有得到优化,尽管我没有执行任何基准来支持该语句.

@petrux option (Get the last output of a dynamic_rnn in TensorFlow) seems also to work but the need of building a list within a for loop may be less optimized, although I did not perform any benchmark to support this statement.

推荐答案

可能是一个答案.我认为您所指出的NumPy符号没有类似之处,但是效果是相同的.

This could be an answer. I don't think there is anything similar to the NumPy notation you pointed out, but the effect is the same.

这篇关于检索Tensorflow中LSTM序列的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆