I train the LSTM cell on batches of sequences that have different lengths. tf.nn.rnn
has a very convenient sequence_length
parameter, but after calling it, I donโt know how to select the output lines corresponding to the last time step of each element in the package.
My code basically looks like this:
lstm_cell = tf.nn.rnn_cell.LSTMCell(num_lstm_units, input_size) lstm_outputs, state = tf.nn.rnn(lstm_cell, input_list, dtype=tf.float32, sequence_length=sequence_lengths)
lstm_outputs
is a list with LSTM output at each time step. However, each element in my batch has a different length, so I would like to create a tensor containing the last LSTM output valid for each element in my batch.
If I could use numpy indexing, I would just do something like this:
all_outputs = tf.pack(lstm_outputs) last_outputs = all_outputs[sequence_lengths, tf.range(batch_size), :]
But it turns out that for starters begin shadoworflow does not support it (I know the function request ).
So how can I get these values?
source share