declarative programming (static graph)

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() repeatedly until reaches to the maximum length of inputs.

  • cell (RNNCell) – An instance of RNNCell.

  • inputs (Variable) – A (possibly nested structure of) tensor variable[s]. The shape of tensor should be [batch_size, sequence_length, …] for time_major == False or [sequence_length, batch_size, …] for time_major == True. It represents the inputs to be unrolled in RNN.

  • initial_states (Variable, optional) – A (possibly nested structure of) tensor variable[s], representing the initial state for RNN. If not provided, cell.get_initial_states would be used to produce the initial state. Default None.

  • sequence_length (Variable, optional) – A tensor with shape [batch_size]. It stores real length of each instance, thus enables users to extract the last valid state when past a batch element’s sequence length for correctness. If not provided, the paddings would be treated same as non-padding inputs. Default None.

  • time_major (bool, optional) – Indicate the data layout of Tensor included in input and output tensors. If False, the data layout would be batch major with shape [batch_size, sequence_length, …]. If True, the data layout would be time major with shape [sequence_length, batch_size, …]. Default: False.

  • is_reverse (bool, optional) – Indicate whether to calculate in the reverse order of input sequences. Default: False.

  • **kwargs – Additional keyword arguments. Arguments passed to cell.call.


A tuple( (final_outputs, final_states) ) including the final outputs and states, both are Tensor or nested structure of Tensor. final_outputs has the same structure and data types as the returned outputs of cell.call , and each Tenser in final_outputs stacks all time steps’ counterpart in outputs thus has shape [batch_size, sequence_length, …] for time_major == False or [sequence_length, batch_size, …] for time_major == True. final_states is the counterpart at last time step of initial states, thus has the same structure with it and has tensors with same shapes and data types.

Return type



import paddle.fluid as fluid

inputs = fluid.data(name="inputs",
                    shape=[-1, 32, 128],
cell = fluid.layers.GRUCell(hidden_size=128)
outputs = fluid.layers.rnn(cell=cell, inputs=inputs)