paddle.fluid.layers. birnn ( cell_fw, cell_bw, inputs, initial_states=None, sequence_length=None, time_major=False, **kwargs ) [source]

birnn creates a bidirectional recurrent neural network specified by RNNCell cell_fw and cell_bw, which performs (for dygraph mode cell.forward) repeatedly until reaches to the maximum length of inputs and then concat the ouputs for both RNNs along the last axis.

  • cell_fw (RNNCellBase) – An instance of RNNCellBase.

  • cell_bw (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 (tuple, optional) – A tuple of initial states of cell_fw and cell_bw. If not provided, cell.get_initial_states would be called to produce initial state for each cell. 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.

  • **kwargs – Additional keyword arguments to pass to forward of each cell.


(outputs, final_states) outputs (Tensor): the outputs of the bidirectional RNN. It is the

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/ of paddle.fluid.layers.rnn.birnn, line 34)

Unexpected indentation.

concatenation of the outputs from the forward RNN and backward RNN along the last axis. If time major is True, the shape is [time_steps, batch_size, size], else the shape is [batch_size, time_steps, size], where size is cell_fw.hidden_size + cell_bw.hidden_size.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/ of paddle.fluid.layers.rnn.birnn, line 39)

Block quote ends without a blank line; unexpected unindent.

final_states (tuple): A tuple of the final states of the forward

cell and backward cell.


import paddle

cell_fw = paddle.nn.LSTMCell(16, 32)
cell_bw = paddle.nn.LSTMCell(16, 32)

inputs = paddle.rand((4, 23, 16))
hf, cf = paddle.rand((4, 32)), paddle.rand((4, 32))
hb, cb = paddle.rand((4, 32)), paddle.rand((4, 32))
initial_states = ((hf, cf), (hb, cb))
outputs, final_states = paddle.fluid.layers.birnn(
    cell_fw, cell_bw, inputs, initial_states)