Skip to content Skip to sidebar Skip to footer

Retrieving Last Value Of Lstm Sequence In Tensorflow

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. m

Solution 1:

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.

Solution 2:

Here's a solution, using gather_nd, where batch size does not need to be known ahead of time.

defextract_axis_1(data, ind):
    """
    Get specified elements along the first axis of tensor.
    :param data: Tensorflow tensor that will be subsetted.
    :param ind: Indices to take (one for each element along axis 0 of data).
    :return: Subsetted tensor.
    """

    batch_range = tf.range(tf.shape(data)[0])
    indices = tf.stack([batch_range, ind], axis=1)
    res = tf.gather_nd(data, indices)

    return res

output = extract_axis_1(sequence_outputs, lengths - 1)

Now output is a tensor of dimension [batch_size, num_cells].

Post a Comment for "Retrieving Last Value Of Lstm Sequence In Tensorflow"