rnn¶
- paddle.fluid.layers. rnn ( cell, inputs, initial_states=None, sequence_length=None, time_major=False, is_reverse=False, **kwargs ) [source]
-
rnn creates a recurrent neural network specified by RNNCell cell, which performs
cell.call()
(for dygraph modecell.forward
) repeatedly until reaches to the maximum length of inputs.- Parameters
-
cell (RNNCellBase) – An instance of RNNCellBase.
inputs (Tensor) – the input sequences. If time_major is True, the shape is [time_steps, batch_size, input_size] else the shape is [batch_size, time_steps, input_size].
initial_states (Tensor|tuple|list, optional) – the initial state of the rnn cell. Tensor or a possibly nested structure of tensors. If not provided, cell.get_initial_states would be called to produce the initial state. Defaults to None.
sequence_length (Tensor, optional) – shape [batch_size], dtype: int64 or int32. The valid lengths of input sequences. Defaults to None. If sequence_length is not None, the inputs are treated as padded sequences. In each input sequence, elements whose time step index are not less than the valid length are treated as paddings.
time_major (bool) – Whether the first dimension of the input means the time steps. Defaults to False.
is_reverse (bool, optional) – Indicate whether to calculate in the reverse order of input sequences. Defaults to False.
**kwargs – Additional keyword arguments to pass to forward of the cell.
- Returns
-
(outputs, final_states) outputs (Tensor|list|tuple): the output sequence. Tensor or nested
structure of Tensors. If time_major is True, the shape of each tensor in outpus is [time_steps, batch_size, hidden_size], else [batch_size, time_steps, hidden_size].
- final_states (Tensor|list|tuple): final states. A (possibly nested structure of)
-
tensor[s], representing the final state for RNN. It has the same structure of intial state. Each tensor in final states has the same shape and dtype as the corresponding tensor in initial states.
Examples
import paddle paddle.disable_static() cell = paddle.nn.SimpleRNNCell(16, 32) inputs = paddle.rand((4, 23, 16)) prev_h = paddle.randn((4, 32)) outputs, final_states = paddle.fluid.layers.rnn(cell, inputs, prev_h)