Analysis of the output of the tf.nn.dynamic_rnn tensorflow function

I can not understand the result of the tf.nn.dynamic_rnn tensorflow function. The document simply indicates the size of the output file, but it does not say what each row / column means. From the documentation:

outputs : RNN Tensor output.

If time_major == False (default), it will be a Tensor the form: [batch_size, max_time, cell.output_size] .

If time_major == True, this will be Tensor : [max_time, batch_size, cell.output_size] .

Note that if cell.output_size is a (possibly nested) set of integers or TensorShape , then outputs will be a tuple having the same structure as cell.output_size containing tensors having shapes corresponding to the form data in cell.output_size .

State : final state. If cell.state_size is int, it will be of the form [batch_size, cell.state_size] . If this
TensorShape , this will take the form [batch_size] + cell.state_size .
If it is a (possibly nested) set of int or TensorShape , it will be a tuple that has the appropriate shapes.

The outputs tensor is a three-dimensional matrix, but what does each row / column represent?

+8
tensorflow
source share
1 answer

tf.dynamic_rnn provides two outputs: outputs and state .

  • outputs contains the output of the RNN cell at each point in time. Assuming the default time_major == False , suppose you have an input consisting of 10 examples with 7 time steps each and a function vector of size 5 for each time step. Then your input will be 10x7x5 ( batch_size x max_time x features ). Now you give this as an input to the RNN cell with output size 15. Conceptually, each step of each example is entered into the RNN, and you get a 15-long vector for each of them. So that outputs contains a tensor in this case of size 10x7x15 ( batch_size x max_time x cell.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 simply slice the time dimension to select only the last element (for example, outputs[:, -1, :] ).
  • state contains the state of the RNN after processing all the inputs. Please note that unlike outputs , this does not contain information about each time step, but only about the last (that is, the state after the last). Depending on your case, the condition may or may not be beneficial. For example, if you have very long sequences, you may not want / be able to process them in one batch, and you may need to break them down into several subsequences. If you ignore state , then whenever you give a new subsequence, it will look like you are starting a new one; if you remember the state, however (for example, by displaying it or storing it in a variable), you can submit it later (via the initial_state tf.nn.dynamic_rnn parameter) to correctly monitor the state of the RNN, and only reset it to its original state (usually all zeros) after completion of all sequences. The state form may vary depending on the RNN cell you are using, but as a rule, you have a specific state for each example (one or more tensors with the size batch_size x state_size , where state_size depends on the type and size of the cell.)
+15
source share

All Articles