StaticRNN

class paddle.fluid.layers.StaticRNN(name=None)[source]

StaticRNN class.

The StaticRNN can process a batch of sequence data. The first dimension of inputs represents sequence length, the length of each input sequence must be equal. StaticRNN will unfold sequence into time steps, user needs to define how to process each time step during the with step.

Parameters

name (str, optional) – Please refer to Name, Default None.

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers

vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
    input=x,
    size=[vocab_size, hidden_size],
    dtype='float32',
    is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])

rnn = fluid.layers.StaticRNN()
with rnn.step():
    # mark created x_emb as input, each step process a word
    word = rnn.step_input(x_emb)
    # create prev memory parameter, batch size comes from word
    prev = rnn.memory(shape=[-1, hidden_size], batch_ref = word)
    hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
    # use hidden to update prev
    rnn.update_memory(prev, hidden)
    # mark hidden as output
    rnn.step_output(hidden)
# get StaticrNN final output
result = rnn()
step()

Define operators in each step. step is used in with block, OP in with block will be executed sequence_len times (sequence_len is the length of input)

memory(init=None, shape=None, batch_ref=None, init_value=0.0, init_batch_dim_idx=0, ref_batch_dim_idx=1)

Create a memory variable for static rnn. If the init is not None, memory will be initialized by this Variable. If the init is None, shape and batch_ref must be set, and this function will create a new variable with shape and batch_ref to initialize init Variable.

Parameters
  • init (Variable, optional) – Tensor used to init memory. If it is not set, shape and batch_ref must be provided. Default: None.

  • shape (list|tuple) – When init is None use this arg to initialize memory shape.

  • the shape does not contain batch_size. Default (NOTE) – None.

  • batch_ref (Variable, optional) – When init is None, memory’s batch size will

  • set as batch_ref's ref_batch_dim_idx value. Default (be) – None.

  • init_value (float, optional) – When init is None, used to init memory’s value. Default: 0.0.

  • init_batch_dim_idx (int, optional) – the batch_size axis of the init Variable. Default: 0.

  • ref_batch_dim_idx (int, optional) – the batch_size axis of the batch_ref Variable. Default: 1.

Returns

The memory variable.

Return type

Variable

Examples 1:
import paddle.fluid as fluid
import paddle.fluid.layers as layers

vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
        input=x,
        size=[vocab_size, hidden_size],
        dtype='float32',
        is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])

rnn = fluid.layers.StaticRNN()
with rnn.step():
        # mark created x_emb as input, each step process a word
        word = rnn.step_input(x_emb)
        # create prev memory parameter, batch size comes from word
        prev = rnn.memory(shape=[-1, hidden_size], batch_ref = word)
        hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
        # use hidden to update prev
        rnn.update_memory(prev, hidden)
Examples 2:
import paddle.fluid as fluid
import paddle.fluid.layers as layers
vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
        input=x,
        size=[vocab_size, hidden_size],
        dtype='float32',
        is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])
boot_memory = fluid.layers.data(name='boot', shape=[hidden_size], dtype='float32', lod_level=1)
rnn = fluid.layers.StaticRNN()
with rnn.step():
        # mark created x_emb as input, each step process a word
        word = rnn.step_input(x_emb)
        # init memory
        prev = rnn.memory(init=boot_memory)
        hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
        # update hidden with prev
        rnn.update_memory(prev, hidden)
step_input(x)

Mark a sequence as a StaticRNN input.

Parameters

x (Variable) – The input sequence, the shape of x should be [seq_len, …].

Returns

The current time step data in the input sequence.

Return type

Variable

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers

vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
        input=x,
        size=[vocab_size, hidden_size],
        dtype='float32',
        is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])

rnn = fluid.layers.StaticRNN()
with rnn.step():
        # mark created x_emb as input, each step process a word
        word = rnn.step_input(x_emb)
        # create prev memory parameter, batch size comes from word
        prev = rnn.memory(shape=[-1, hidden_size], batch_ref = word)
        hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
        # use hidden to update prev
        rnn.update_memory(prev, hidden)
step_output(o)

Mark a sequence as a StaticRNN output.

Parameters

o (Variable) – The output sequence.

Returns

None.

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers

vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
        input=x,
        size=[vocab_size, hidden_size],
        dtype='float32',
        is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])

rnn = fluid.layers.StaticRNN()
with rnn.step():
        # mark created x_emb as input, each step process a word
        word = rnn.step_input(x_emb)
        # create prev memory parameter, batch size comes from word
        prev = rnn.memory(shape=[-1, hidden_size], batch_ref = word)
        hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
        # use hidden to update prev
        rnn.update_memory(prev, hidden)
        rnn.step_output(hidden)

result = rnn()
output(*outputs)

Mark the StaticRNN output variables.

Parameters

outputs – The output Tensor, can mark multiple variables as output

Returns

None

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers

vocab_size, hidden_size=10000, 200
x = fluid.data(name="x", shape=[None, 1, 1], dtype='int64')
# create word sequence
x_emb = layers.embedding(
        input=x,
        size=[vocab_size, hidden_size],
        dtype='float32',
        is_sparse=False)
# transform batch size to dim 1
x_emb = layers.transpose(x_emb, perm=[1, 0, 2])

rnn = fluid.layers.StaticRNN()
with rnn.step():
        # mark created x_emb as input, each step process a word
        word = rnn.step_input(x_emb)
        # create prev memory parameter, batch size comes from word
        prev = rnn.memory(shape=[-1, hidden_size], batch_ref = word)
        hidden = fluid.layers.fc(input=[word, prev], size=hidden_size, act='relu')
        # use hidden to update prev
        rnn.update_memory(prev, hidden)
        # mark each step's hidden and word as output
        rnn.output(hidden, word)

result = rnn()
update_memory(mem, var)

Update the memory from mem to var.

Parameters
  • mem (Variable) – the memory variable.

  • var (Variable) – the plain variable generated in RNN block, used to update memory. var and mem should hava same dims and data type.

Returns

None