Detailed explanation of tf.nn.dynamic_rnn in tensorflow

Function prototype

tf.nn.dynamic_rnn(

    cell,

    inputs,

    sequence_length=None,

    initial_state=None,

    dtype=None,

    parallel_iterations=None,

    swap_memory=False,

    time_major=False,

    scope=None
    )

Parameter explanation

  • cell: An instance of RNNCell.

  • inputs: RNN input. If time_major == False (default), it is a Tensor with shape [batch_size, max_time, input_size], or a nested tuple of these elements. If time_major == True, it is a Tensor with shape [max_time, batch_size, input_size], or a nested tuple of these elements.

  • sequence_length: (Optional) The size is [batch_size], and the data type is int32/int64 vector. If the index of the current time step exceeds the actual length of the sequence, the time step is not calculated, the state of the RNN is copied from the previous time step, and the output of this time step is all zero.

  • initial_state: (optional) the initial state of the RNN. If cell.state_size (RNNCell of a layer) is an integer, then it must be a tensor of appropriate type and shape [batch_size, cell.state_size]. If cell.state_size is a tuple (multi-layer RNNCell, such as MultiRNNCell), then it should be a tensor tuple, and the shape of each element is [batch_size, s] for s in cell.state_size.

  • time_major: The shape format of inputs and outputs tensor. If True, these tensors should all be (all will be) [max_time, batch_size, depth]. If false, then these tensors should all be (all will be) [batch_size, max_time, depth]. time_major=true means that the first dimension of the input and output tensor is max_time. Otherwise, it is batch_size. Using time_major=True is more efficient because it avoids transposition at the beginning and end of RNN calculation. However, most TensorFlow data is batch-major, so by default, this function accepts input and emits output in batch-major form .

return value

A pair (outputs, state), where:

  • outputs: RNN output Tensor. If time_major == False (default), this will be a Tensor with shape [batch_size, max_time, cell.output_size]. If time_major == True, this will be a Tensor with shape [max_time, batch_size, cell. output_size] Tensor.

  • state: The final state. In general, the shape of state is [batch_size, cell.output_size]. If the cell is LSTMCells, the state will be a tuple containing the LSTMStateTuple of each cell, and the shape of the state will be [2, batch_size, cell.output_size]

Guess you like

Origin blog.csdn.net/m0_47220500/article/details/108240753