I am not able to understand the output from tf.nn.dynamic_rnn
tensorflow function. The document just tells about the size of the output, but it doesn't tell what does each row/column means. From the documentation:
outputs: The RNN output
Tensor
.If time_major == False (default), this will be a
Tensor
shaped:[batch_size, max_time, cell.output_size]
.If time_major == True, this will be a
Tensor
shaped:[max_time, batch_size, cell.output_size]
.Note, if
cell.output_size
is a (possibly nested) tuple of integers orTensorShape
objects, thenoutputs
will be a tuple having the
same structure ascell.output_size
, containing Tensors having shapes corresponding to the shape data incell.output_size
.state: The final state. If
cell.state_size
is an int, this will be shaped[batch_size, cell.state_size]
. If it is aTensorShape
, this will be shaped[batch_size] + cell.state_size
.
If it is a (possibly nested) tuple of ints orTensorShape
, this will be a tuple having the corresponding shapes.
The outputs
tensor is a 3-D matrix but what does each row/column represent?
tf.dynamic_rnn
provides two outputs, outputs
and state
.
outputs
contains the output of the RNN cell at every time instant. Assuming the default time_major == False
, let's say you have an input composed of 10 examples with 7 time steps each and a feature vector of size 5 for every time step. Then your input would be 10x7x5 (batch_size
xmax_time
xfeatures
). Now you give this as an input to a RNN cell with output size 15. Conceptually, each time step of each example is input to the RNN, and you would get a 15-long vector for each of those. So that is what outputs
contains, a tensor in this case of size 10x7x15 (batch_size
xmax_time
xcell.output_size
) with the output of the RNN cell at each time step. If you are only interested in the last output of the cell, you can just slice the time dimension to pick just the last element (e.g. outputs[:, -1, :]
).state
contains the state of the RNN after processing all the inputs. Note that, unlike outputs
, this doesn't contain information about every time step, but only about the last one (that is, the state after the last one). Depending on your case, the state may or may not be useful. For example, if you have very long sequences, you may not want/be able to processes them in a single batch, and you may need to split them into several subsequences. If you ignore the state
, then whenever you give a new subsequence it will be as if you are beginning a new one; if you remember the state, however (e.g. outputting it or storing it in a variable), you can feed it back later (through the initial_state
parameter of tf.nn.dynamic_rnn
) in order to correctly keep track of the state of the RNN, and only reset it to the initial state (generally all zeros) after you have completed the whole sequences. The shape of state
can vary depending on the RNN cell that you are using, but, in general, you have some state for each of the examples (one or more tensors with size batch_size
xstate_size
, where state_size
depends on the cell type and size).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With