rnn.py 64.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

F
Feiyu Chan 已提交
15 16 17 18 19 20 21 22 23
import copy
import collections
import itertools
import six
import math
import sys
import warnings
from functools import partial, reduce

24
import numpy as np
F
Feiyu Chan 已提交
25
import paddle
26
import paddle.fluid as fluid
F
Feiyu Chan 已提交
27
from paddle import framework
28
from paddle.device import get_device, get_cudnn_version
F
Feiyu Chan 已提交
29 30
from paddle.nn import functional as F
from paddle.nn import initializer as I
Z
zhiboniu 已提交
31
from paddle.nn import Layer, LayerList
F
Feiyu Chan 已提交
32 33 34
from paddle.fluid.layers import utils
from paddle.fluid.layers.utils import map_structure, flatten, pack_sequence_as
from paddle.fluid.data_feeder import convert_dtype
35
from paddle import _C_ops, _legacy_C_ops
Z
zhiboniu 已提交
36
from paddle import in_dynamic_mode
37
from paddle.fluid.framework import in_dygraph_mode
Z
zhiboniu 已提交
38 39 40
from paddle.framework import core
from paddle.static import default_startup_program
from paddle.static import program_guard
41 42 43 44
try:
    from collections.abc import Sequence
except:
    from collections import Sequence
Z
zhiboniu 已提交
45

46 47
__all__ = []

F
Feiyu Chan 已提交
48 49 50 51 52 53

def split_states(states, bidirectional=False, state_components=1):
    r"""
    Split states of RNN network into possibly nested list or tuple of
    states of each RNN cells of the RNN network.

54
    Parameters:
F
Feiyu Chan 已提交
55 56
        states (Tensor|tuple|list): the concatenated states for RNN network.
            When `state_components` is 1, states in a Tensor with shape
57 58 59 60 61 62 63 64 65 66 67
            `(L*D, N, C)` where `L` is the number of layers of the RNN
            network, `D` is the number of directions of the RNN network(1
            for unidirectional RNNs and 2 for bidirectional RNNs), `N` is
            the batch size of the input to the RNN network, `C` is the
            hidden size of the RNN network.

            When `state_components` is larger than 1, `states` is a tuple of
            `state_components` Tensors that meet the requirements described
            above.

            For SimpleRNNs and GRUs, `state_components` is 1, and for LSTMs,
F
Feiyu Chan 已提交
68
            `state_components` is 2.
69
        bidirectional (bool): whether the state is of a bidirectional RNN
F
Feiyu Chan 已提交
70 71 72
            network. Defaults to False.
        state_components (int): the number of the components of the states. see
            `states` above. Defaults to 1.
73

F
Feiyu Chan 已提交
74
    Returns:
75 76 77
        A nested list or tuple of RNN cell states.
        If `bidirectional` is True, it can be indexed twice to get an RNN
        cell state. The first index indicates the layer, the second index
F
Feiyu Chan 已提交
78 79 80 81
        indicates the direction.
        If `bidirectional` is False, it can be indexed once to get an RNN
        cell state. The index indicates the layer.
        Note that if `state_components` is larger than 1, an RNN cell state
82
        can be indexed one more time to get a tensor of shape(N, C), where
F
Feiyu Chan 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        `N` is the batch size of the input to the RNN cell, and `C` is the
        hidden size of the RNN cell.
    """
    if state_components == 1:
        states = paddle.unstack(states)
        if not bidirectional:
            return states
        else:
            return list(zip(states[::2], states[1::2]))
    else:
        assert len(states) == state_components
        states = tuple([paddle.unstack(item) for item in states])
        if not bidirectional:
            return list(zip(*states))
        else:
            states = list(zip(*states))
            return list(zip(states[::2], states[1::2]))


def concat_states(states, bidirectional=False, state_components=1):
    r"""
104
    Concatenate a possibly nested list or tuple of RNN cell states into a
F
Feiyu Chan 已提交
105 106
    compact form.

107
    Parameters:
108 109 110 111
        states (list|tuple): a possibly nested list or tuple of RNN cell
            states.
            If `bidirectional` is True, it can be indexed twice to get an
            RNN cell state. The first index indicates the layer, the second
F
Feiyu Chan 已提交
112 113 114
            index indicates the direction.
            If `bidirectional` is False, it can be indexed once to get an RNN
            cell state. The index indicates the layer.
115 116 117 118 119
            Note that if `state_components` is larger than 1, an RNN cell
            state can be indexed one more time to get a tensor of shape(N, C),
            where `N` is the batch size of the input to the RNN cell, and
            `C` is the hidden size of the RNN cell.
        bidirectional (bool): whether the state is of a bidirectional RNN
F
Feiyu Chan 已提交
120 121 122
            network. Defaults to False.
        state_components (int): the number of the components of the states. see
            `states` above. Defaults to 1.
123

F
Feiyu Chan 已提交
124 125 126
    Returns:
        Concatenated states for RNN network.
        When `state_components` is 1, states in a Tensor with shape
127 128 129 130
        `(L\*D, N, C)` where `L` is the number of layers of the RNN
        network, `D` is the number of directions of the RNN network(1 for
        unidirectional RNNs and 2 for bidirectional RNNs), `N` is the batch
        size of the input to the RNN network, `C` is the hidden size of the
F
Feiyu Chan 已提交
131
        RNN network.
132

F
Feiyu Chan 已提交
133 134 135 136 137 138 139 140
    """
    if state_components == 1:
        return paddle.stack(flatten(states))
    else:
        states = flatten(states)
        componnets = []
        for i in range(state_components):
            componnets.append(states[i::state_components])
141
        return tuple([paddle.stack(item) for item in componnets])
F
Feiyu Chan 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159


class RNNCellBase(Layer):
    r"""
    RNNCellBase is the base class for abstraction representing the calculations
    mapping the input and state to the output and new state. It is suitable to
    and mostly used in RNN.
    """

    def get_initial_states(self,
                           batch_ref,
                           shape=None,
                           dtype=None,
                           init_value=0.,
                           batch_dim_idx=0):
        r"""
        Generate initialized states according to provided shape, data type and
        value.
160 161

        Parameters:
162 163 164
            batch_ref (Tensor): A tensor, which shape would be used to
                determine the batch size, which is used to generate initial
                states. For `batch_ref`'s shape d, `d[batch_dim_idx]` is
F
Feiyu Chan 已提交
165
                treated as batch size.
166 167 168 169
            shape (list|tuple, optional): A (possibly nested structure of) shape[s],
                where a shape is a list/tuple of integer. `-1` (for batch size)
                will be automatically prepended if a shape does not starts with
                it. If None, property `state_shape` will be used. Defaults to
F
Feiyu Chan 已提交
170
                None.
171 172 173 174 175
            dtype (str|list|tuple, optional): A (possibly nested structure of)
                data type[s]. The structure must be same as that of `shape`,
                except when all tensors' in states has the same data type, a
                single data type can be used. If None and property `cell.state_shape`
                is not available, current default floating type of paddle is
F
Feiyu Chan 已提交
176
                used. Defaults to None.
177
            init_value (float, optional): A float value used to initialize states.
F
Feiyu Chan 已提交
178
                Defaults to 0.
179
            batch_dim_idx (int, optional): An integer indicating which
F
Feiyu Chan 已提交
180
                dimension of the of `batch_ref` represents batch. Defaults to 0.
181

F
Feiyu Chan 已提交
182
        Returns:
183
            init_states (Tensor|tuple|list): tensor of the provided shape and
F
Feiyu Chan 已提交
184 185 186 187 188 189 190 191 192 193
                dtype, or list of tensors that each satisfies the requirements,
                packed in the same structure as `shape` and `type` does.
        """
        # TODO: use inputs and batch_size
        batch_ref = flatten(batch_ref)[0]

        def _is_shape_sequence(seq):
            if sys.version_info < (3, ):
                integer_types = (
                    int,
194 195
                    long,
                )
F
Feiyu Chan 已提交
196 197 198 199 200 201 202 203 204 205
            else:
                integer_types = (int, )
            """For shape, list/tuple of integer is the finest-grained objection"""
            if (isinstance(seq, list) or isinstance(seq, tuple)):
                if reduce(lambda flag, x: isinstance(x, integer_types) and flag,
                          seq, True):
                    return False
            # TODO: Add check for the illegal
            if isinstance(seq, dict):
                return True
206 207
            return (isinstance(seq, Sequence)
                    and not isinstance(seq, six.string_types))
F
Feiyu Chan 已提交
208 209

        class Shape(object):
210

F
Feiyu Chan 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
            def __init__(self, shape):
                self.shape = shape if shape[0] == -1 else ([-1] + list(shape))

        # nested structure of shapes
        states_shapes = self.state_shape if shape is None else shape
        is_sequence_ori = utils.is_sequence
        utils.is_sequence = _is_shape_sequence
        states_shapes = map_structure(lambda shape: Shape(shape), states_shapes)
        utils.is_sequence = is_sequence_ori

        # nested structure of dtypes
        try:
            states_dtypes = self.state_dtype if dtype is None else dtype
        except NotImplementedError:
            states_dtypes = framework.get_default_dtype()
        if len(flatten(states_dtypes)) == 1:
            dtype = flatten(states_dtypes)[0]
            states_dtypes = map_structure(lambda shape: dtype, states_shapes)

        init_states = map_structure(
231 232 233 234 235 236 237
            lambda shape, dtype: paddle.fluid.layers.
            fill_constant_batch_size_like(input=batch_ref,
                                          shape=shape.shape,
                                          dtype=dtype,
                                          value=init_value,
                                          input_dim_idx=batch_dim_idx),
            states_shapes, states_dtypes)
F
Feiyu Chan 已提交
238 239 240 241 242 243 244
        return init_states

    @property
    def state_shape(self):
        r"""
        Abstract method (property).
        Used to initialize states.
245
        A (possiblely nested structure of) shape[s], where a shape is a
F
Feiyu Chan 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        list/tuple of integers (-1 for batch size would be automatically
        inserted into a shape if shape is not started with it).
        Not necessary to be implemented if states are not initialized by
        `get_initial_states` or the `shape` argument is provided when using
        `get_initial_states`.
        """
        raise NotImplementedError(
            "Please add implementaion for `state_shape` in the used cell.")

    @property
    def state_dtype(self):
        r"""
        Abstract method (property).
        Used to initialize states.
        A (possiblely nested structure of) data types[s]. The structure must be
        same as that of `shape`, except when all tensors' in states has the same
        data type, a signle data type can be used.
        Not necessary to be implemented if states are not initialized
        by `get_initial_states` or the `dtype` argument is provided when using
        `get_initial_states`.
        """
        raise NotImplementedError(
            "Please add implementaion for `state_dtype` in the used cell.")


class SimpleRNNCell(RNNCellBase):
    r"""
273
    Elman RNN (SimpleRNN) cell. Given the inputs and previous states, it
F
Feiyu Chan 已提交
274 275 276 277 278
    computes the outputs and updates states.

    The formula used is as follows:

    .. math::
279
        h_{t} & = act(W_{ih}x_{t} + b_{ih} + W_{hh}h_{t-1} + b_{hh})
280

F
Feiyu Chan 已提交
281
        y_{t} & = h_{t}
282

283
    where :math:`act` is for :attr:`activation`.
F
Feiyu Chan 已提交
284

285
    Please refer to `Finding Structure in Time
F
Feiyu Chan 已提交
286
    <https://crl.ucsd.edu/~elman/Papers/fsit.pdf>`_ for more details.
287

288
    Parameters:
F
Feiyu Chan 已提交
289 290
        input_size (int): The input size.
        hidden_size (int): The hidden size.
291
        activation (str, optional): The activation in the SimpleRNN cell.
F
Feiyu Chan 已提交
292
            It can be `tanh` or `relu`. Defaults to `tanh`.
293
        weight_ih_attr (ParamAttr, optional): The parameter attribute for
294
            :math:`weight_ih`. Default: None.
295
        weight_hh_attr(ParamAttr, optional): The parameter attribute for
296
            :math:`weight_hh`. Default: None.
297
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
298
            :math:`bias_ih`. Default: None.
299
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
300
            :math:`bias_hh`. Default: None.
301
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
302 303
            None). For more information, please refer to :ref:`api_guide_Name`.

304 305 306 307 308
    Variables:
        - **weight_ih** (Parameter): shape (hidden_size, input_size), input to hidden weight, corresponding to :math:`W_{ih}` in the formula.
        - **weight_hh** (Parameter): shape (hidden_size, hidden_size), hidden to hidden weight, corresponding to :math:`W_{hh}` in the formula.
        - **bias_ih** (Parameter): shape (hidden_size, ), input to hidden bias, corresponding to :math:`b_{ih}` in the formula.
        - **bias_hh** (Parameter): shape (hidden_size, ), hidden to hidden bias, corresponding to :math:`b_{hh}` in the formula.
309

F
Feiyu Chan 已提交
310
    Inputs:
311 312
        - **inputs** (Tensor): shape `[batch_size, input_size]`, the input, corresponding to :math:`x_{t}` in the formula.
        - **states** (Tensor, optional): shape `[batch_size, hidden_size]`, the previous hidden state, corresponding to :math:`h_{t-1}` in the formula. When states is None, zero state is used. Defaults to None.
F
Feiyu Chan 已提交
313 314

    Returns:
315 316
        - **outputs** (Tensor): shape `[batch_size, hidden_size]`, the output, corresponding to :math:`h_{t}` in the formula.
        - **states** (Tensor): shape `[batch_size, hidden_size]`, the new hidden state, corresponding to :math:`h_{t}` in the formula.
317

F
Feiyu Chan 已提交
318
    Notes:
319
        All the weights and bias are initialized with `Uniform(-std, std)` by default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more information about parameter initialization, please refer to :ref:`api_fluid_ParamAttr`.
F
Feiyu Chan 已提交
320 321 322 323 324 325 326 327 328 329 330 331

    Examples:

        .. code-block:: python

            import paddle

            x = paddle.randn((4, 16))
            prev_h = paddle.randn((4, 32))

            cell = paddle.nn.SimpleRNNCell(16, 32)
            y, h = cell(x, prev_h)
332 333 334
            print(y.shape)

            #[4,32]
F
Feiyu Chan 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347

    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 activation="tanh",
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
        super(SimpleRNNCell, self).__init__()
348 349
        if hidden_size <= 0:
            raise ValueError(
350 351
                "hidden_size of {} must be greater than 0, but now equals to {}"
                .format(self.__class__.__name__, hidden_size))
F
Feiyu Chan 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        std = 1.0 / math.sqrt(hidden_size)
        self.weight_ih = self.create_parameter(
            (hidden_size, input_size),
            weight_ih_attr,
            default_initializer=I.Uniform(-std, std))
        self.weight_hh = self.create_parameter(
            (hidden_size, hidden_size),
            weight_hh_attr,
            default_initializer=I.Uniform(-std, std))
        self.bias_ih = self.create_parameter(
            (hidden_size, ),
            bias_ih_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))
        self.bias_hh = self.create_parameter(
            (hidden_size, ),
            bias_hh_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))

        self.input_size = input_size
        self.hidden_size = hidden_size
        if activation not in ["tanh", "relu"]:
            raise ValueError(
                "activation for SimpleRNNCell should be tanh or relu, "
                "but get {}".format(activation))
        self.activation = activation
        self._activation_fn = paddle.tanh \
            if activation == "tanh" \
            else F.relu

    def forward(self, inputs, states=None):
        if states is None:
            states = self.get_initial_states(inputs, self.state_shape)
        pre_h = states
        i2h = paddle.matmul(inputs, self.weight_ih, transpose_y=True)
        if self.bias_ih is not None:
            i2h += self.bias_ih
        h2h = paddle.matmul(pre_h, self.weight_hh, transpose_y=True)
        if self.bias_hh is not None:
            h2h += self.bias_hh
        h = self._activation_fn(i2h + h2h)
        return h, h

    @property
    def state_shape(self):
        return (self.hidden_size, )

400 401
    def extra_repr(self):
        s = '{input_size}, {hidden_size}'
402
        if self.activation != "tanh":
403 404 405
            s += ', activation={activation}'
        return s.format(**self.__dict__)

F
Feiyu Chan 已提交
406 407 408

class LSTMCell(RNNCellBase):
    r"""
409
    Long-Short Term Memory(LSTM) RNN cell. Given the inputs and previous states,
F
Feiyu Chan 已提交
410 411 412 413 414 415
    it computes the outputs and updates states.

    The formula used is as follows:

    .. math::
        i_{t} & = \sigma(W_{ii}x_{t} + b_{ii} + W_{hi}h_{t-1} + b_{hi})
416

F
Feiyu Chan 已提交
417
        f_{t} & = \sigma(W_{if}x_{t} + b_{if} + W_{hf}h_{t-1} + b_{hf})
418

F
Feiyu Chan 已提交
419
        o_{t} & = \sigma(W_{io}x_{t} + b_{io} + W_{ho}h_{t-1} + b_{ho})
420 421 422 423 424 425 426

        \widetilde{c}_{t} & = \tanh (W_{ig}x_{t} + b_{ig} + W_{hg}h_{t-1} + b_{hg})

        c_{t} & = f_{t} * c_{t-1} + i_{t} * \widetilde{c}_{t}

        h_{t} & = o_{t} * \tanh(c_{t})

F
Feiyu Chan 已提交
427 428
        y_{t} & = h_{t}

429
    where :math:`\sigma` is the sigmoid fucntion, and * is the elemetwise
F
Feiyu Chan 已提交
430 431 432 433 434
    multiplication operator.

    Please refer to `An Empirical Exploration of Recurrent Network Architectures
    <http://proceedings.mlr.press/v37/jozefowicz15.pdf>`_ for more details.

435
    Parameters:
F
Feiyu Chan 已提交
436 437
        input_size (int): The input size.
        hidden_size (int): The hidden size.
438
        weight_ih_attr(ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
439
            `weight_ih`. Default: None.
440
        weight_hh_attr(ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
441
            `weight_hh`. Default: None.
442
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
443
            `bias_ih`. Default: None.
444
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
445
            `bias_hh`. Default: None.
446
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
447 448
            None). For more information, please refer to :ref:`api_guide_Name`.

449 450 451 452 453
    Variables:
        - **weight_ih** (Parameter): shape (4 * hidden_size, input_size), input to hidden weight, which corresponds to the concatenation of :math:`W_{ii}, W_{if}, W_{ig}, W_{io}` in the formula.
        - **weight_hh** (Parameter): shape (4 * hidden_size, hidden_size), hidden to hidden weight, which corresponds to the concatenation of :math:`W_{hi}, W_{hf}, W_{hg}, W_{ho}` in the formula.
        - **bias_ih** (Parameter): shape (4 * hidden_size, ), input to hidden bias, which corresponds to the concatenation of :math:`b_{ii}, b_{if}, b_{ig}, b_{io}` in the formula.
        - **bias_hh** (Parameter): shape (4 * hidden_size, ), hidden to hidden bias, swhich corresponds to the concatenation of :math:`b_{hi}, b_{hf}, b_{hg}, b_{ho}` in the formula.
F
Feiyu Chan 已提交
454 455

    Inputs:
456
        - **inputs** (Tensor): shape `[batch_size, input_size]`, the input, corresponding to :math:`x_t` in the formula.
457
        - **states** (list|tuple, optional): a list/tuple of two tensors, each of shape `[batch_size, hidden_size]`, the previous hidden state, corresponding to :math:`h_{t-1}, c_{t-1}` in the formula. When states is None, zero state is used. Defaults to None.
F
Feiyu Chan 已提交
458 459

    Returns:
460 461
        - **outputs** (Tensor): shape `[batch_size, hidden_size]`, the output, corresponding to :math:`h_{t}` in the formula.
        - **states** (tuple): a tuple of two tensors, each of shape `[batch_size, hidden_size]`, the new hidden states, corresponding to :math:`h_{t}, c_{t}` in the formula.
F
Feiyu Chan 已提交
462 463

    Notes:
464 465
        All the weights and bias are initialized with `Uniform(-std, std)` by
        default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more
466
        information about parameter initialization, please refer to :ref:`api_fluid_ParamAttr`.
F
Feiyu Chan 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480

    Examples:

        .. code-block:: python

            import paddle

            x = paddle.randn((4, 16))
            prev_h = paddle.randn((4, 32))
            prev_c = paddle.randn((4, 32))

            cell = paddle.nn.LSTMCell(16, 32)
            y, (h, c) = cell(x, (prev_h, prev_c))

481 482 483 484 485 486 487 488
            print(y.shape)
            print(h.shape)
            print(c.shape)

            #[4,32]
            #[4,32]
            #[4,32]

F
Feiyu Chan 已提交
489 490 491 492 493 494 495 496 497 498 499
    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
        super(LSTMCell, self).__init__()
500 501
        if hidden_size <= 0:
            raise ValueError(
502 503
                "hidden_size of {} must be greater than 0, but now equals to {}"
                .format(self.__class__.__name__, hidden_size))
F
Feiyu Chan 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        std = 1.0 / math.sqrt(hidden_size)
        self.weight_ih = self.create_parameter(
            (4 * hidden_size, input_size),
            weight_ih_attr,
            default_initializer=I.Uniform(-std, std))
        self.weight_hh = self.create_parameter(
            (4 * hidden_size, hidden_size),
            weight_hh_attr,
            default_initializer=I.Uniform(-std, std))
        self.bias_ih = self.create_parameter(
            (4 * hidden_size, ),
            bias_ih_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))
        self.bias_hh = self.create_parameter(
            (4 * hidden_size, ),
            bias_hh_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))

        self.hidden_size = hidden_size
        self.input_size = input_size
        self._gate_activation = F.sigmoid
        self._activation = paddle.tanh

    def forward(self, inputs, states=None):
        if states is None:
            states = self.get_initial_states(inputs, self.state_shape)
        pre_hidden, pre_cell = states
        gates = paddle.matmul(inputs, self.weight_ih, transpose_y=True)
        if self.bias_ih is not None:
            gates = gates + self.bias_ih
        gates += paddle.matmul(pre_hidden, self.weight_hh, transpose_y=True)
        if self.bias_hh is not None:
            gates = gates + self.bias_hh

        chunked_gates = paddle.split(gates, num_or_sections=4, axis=-1)

        i = self._gate_activation(chunked_gates[0])
        f = self._gate_activation(chunked_gates[1])
        o = self._gate_activation(chunked_gates[3])
        c = f * pre_cell + i * self._activation(chunked_gates[2])
        h = o * self._activation(c)

        return h, (h, c)

    @property
    def state_shape(self):
        r"""
553 554 555
        The `state_shape` of LSTMCell is a tuple with two shapes:
        `((hidden_size, ), (hidden_size,))`. (-1 for batch size would be
        automatically inserted into shape). These two shapes correspond
F
Feiyu Chan 已提交
556 557 558 559
        to :math:`h_{t-1}` and :math:`c_{t-1}` separately.
        """
        return ((self.hidden_size, ), (self.hidden_size, ))

560 561 562
    def extra_repr(self):
        return '{input_size}, {hidden_size}'.format(**self.__dict__)

F
Feiyu Chan 已提交
563 564 565

class GRUCell(RNNCellBase):
    r"""
566
    Gated Recurrent Unit (GRU) RNN cell. Given the inputs and previous states,
F
Feiyu Chan 已提交
567 568 569 570
    it computes the outputs and updates states.

    The formula for GRU used is as follows:

571
    ..  math::
F
Feiyu Chan 已提交
572

573
        r_{t} & = \sigma(W_{ir}x_{t} + b_{ir} + W_{hr}h_{t-1} + b_{hr})
574

575
        z_{t} & = \sigma(W_{iz}x_{t} + b_{iz} + W_{hz}h_{t-1} + b_{hz})
576

577
        \widetilde{h}_{t} & = \tanh(W_{ic}x_{t} + b_{ic} + r_{t} * (W_{hc}h_{t-1} + b_{hc}))
578 579 580

        h_{t} & = z_{t} * h_{t-1} + (1 - z_{t}) * \widetilde{h}_{t}

F
Feiyu Chan 已提交
581
        y_{t} & = h_{t}
582 583

    where :math:`\sigma` is the sigmoid fucntion, and * is the elemetwise
F
Feiyu Chan 已提交
584 585 586 587 588 589
    multiplication operator.

    Please refer to `An Empirical Exploration of Recurrent Network Architectures
    <http://proceedings.mlr.press/v37/jozefowicz15.pdf>`_ for more details.

    Parameters:
590
        input_size (int): The input size.
F
Feiyu Chan 已提交
591
        hidden_size (int): The hidden size.
592
        weight_ih_attr(ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
593
            `weight_ih`. Default: None.
594
        weight_hh_attr(ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
595
            `weight_hh`. Default: None.
596
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
597
            `bias_ih`. Default: None.
598
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
599
            `bias_hh`. Default: None.
600
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
601 602
            None). For more information, please refer to :ref:`api_guide_Name`.

603 604 605 606 607
    Variables:
        - **weight_ih** (Parameter): shape (3 * hidden_size, input_size), input to hidden weight, which corresponds to the concatenation of :math:`W_{ir}, W_{iz}, W_{ic}` in the formula.
        - **weight_hh** (Parameter): shape (3 * hidden_size, hidden_size), hidden to hidden weight, which corresponds to the concatenation of :math:`W_{hr}, W_{hz}, W_{hc}` in the formula.
        - **bias_ih** (Parameter): shape (3 * hidden_size, ), input to hidden bias, which corresponds to the concatenation of :math:`b_{ir}, b_{iz}, b_{ic}` in the formula.
        - **bias_hh** (Parameter): shape (3 * hidden_size, ), hidden to hidden bias, swhich corresponds to the concatenation of :math:`b_{hr}, b_{hz}, b_{hc}` in the formula.
F
Feiyu Chan 已提交
608 609

    Inputs:
610 611
        - **inputs** (Tensor): A tensor with shape `[batch_size, input_size]`, corresponding to :math:`x_t` in the formula.
        - **states** (Tensor): A tensor with shape `[batch_size, hidden_size]`, corresponding to :math:`h_{t-1}` in the formula.
F
Feiyu Chan 已提交
612 613

    Returns:
614 615
        - **outputs** (Tensor): shape `[batch_size, hidden_size]`, the output, corresponding to :math:`h_{t}` in the formula.
        - **states** (Tensor): shape `[batch_size, hidden_size]`, the new hidden state, corresponding to :math:`h_{t}` in the formula.
616

F
Feiyu Chan 已提交
617
    Notes:
618 619
        All the weights and bias are initialized with `Uniform(-std, std)` by
        default. Where std = :math:`\frac{1}{\sqrt{hidden\_size}}`. For more
620
        information about parameter initialization, please refer to s:ref:`api_fluid_ParamAttr`.
F
Feiyu Chan 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633

    Examples:

        .. code-block:: python

            import paddle

            x = paddle.randn((4, 16))
            prev_h = paddle.randn((4, 32))

            cell = paddle.nn.GRUCell(16, 32)
            y, h = cell(x, prev_h)

634 635 636 637 638 639
            print(y.shape)
            print(h.shape)

            #[4,32]
            #[4,32]

F
Feiyu Chan 已提交
640 641 642 643 644 645 646 647 648 649 650
    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
        super(GRUCell, self).__init__()
651 652
        if hidden_size <= 0:
            raise ValueError(
653 654
                "hidden_size of {} must be greater than 0, but now equals to {}"
                .format(self.__class__.__name__, hidden_size))
F
Feiyu Chan 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
        std = 1.0 / math.sqrt(hidden_size)
        self.weight_ih = self.create_parameter(
            (3 * hidden_size, input_size),
            weight_ih_attr,
            default_initializer=I.Uniform(-std, std))
        self.weight_hh = self.create_parameter(
            (3 * hidden_size, hidden_size),
            weight_hh_attr,
            default_initializer=I.Uniform(-std, std))
        self.bias_ih = self.create_parameter(
            (3 * hidden_size, ),
            bias_ih_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))
        self.bias_hh = self.create_parameter(
            (3 * hidden_size, ),
            bias_hh_attr,
            is_bias=True,
            default_initializer=I.Uniform(-std, std))

        self.hidden_size = hidden_size
        self.input_size = input_size
        self._gate_activation = F.sigmoid
        self._activation = paddle.tanh

    def forward(self, inputs, states=None):
        if states is None:
            states = self.get_initial_states(inputs, self.state_shape)

        pre_hidden = states
        x_gates = paddle.matmul(inputs, self.weight_ih, transpose_y=True)
        if self.bias_ih is not None:
            x_gates = x_gates + self.bias_ih
        h_gates = paddle.matmul(pre_hidden, self.weight_hh, transpose_y=True)
        if self.bias_hh is not None:
            h_gates = h_gates + self.bias_hh

        x_r, x_z, x_c = paddle.split(x_gates, num_or_sections=3, axis=1)
        h_r, h_z, h_c = paddle.split(h_gates, num_or_sections=3, axis=1)

        r = self._gate_activation(x_r + h_r)
        z = self._gate_activation(x_z + h_z)
        c = self._activation(x_c + r * h_c)  # apply reset gate after mm
        h = (pre_hidden - c) * z + c

        return h, h

    @property
    def state_shape(self):
        r"""
        The `state_shape` of GRUCell is a shape `[hidden_size]` (-1 for batch
        size would be automatically inserted into shape). The shape corresponds
        to the shape of :math:`h_{t-1}`.
        """
        return (self.hidden_size, )

711 712 713
    def extra_repr(self):
        return '{input_size}, {hidden_size}'.format(**self.__dict__)

F
Feiyu Chan 已提交
714 715 716

class RNN(Layer):
    r"""
717 718
    Wrapper for RNN, which creates a recurrent neural network with an RNN cell.
    It performs :code:`cell.forward()` repeatedly until reaches to the maximum
F
Feiyu Chan 已提交
719 720
    length of `inputs`.

721
    Parameters:
F
Feiyu Chan 已提交
722 723 724 725 726 727 728
        cell(RNNCellBase): An instance of `RNNCellBase`.
        is_reverse (bool, optional): Indicate whether to calculate in the reverse
            order of input sequences. Defaults to False.
        time_major (bool): Whether the first dimension of the input means the
            time steps. Defaults to False.

    Inputs:
729 730 731
        - **inputs** (Tensor): A (possibly nested structure of) tensor[s]. The input sequences. If time major is False, the shape is `[batch_size, time_steps, input_size]`. If time major is True, the shape is `[time_steps, batch_size, input_size]` where `input_size` is the input size of the cell.
        - **initial_states** (Tensor|list|tuple, optional): Tensor of a possibly nested structure of tensors, representing the initial state for the rnn cell. If not provided, `cell.get_initial_states` would be called to produce the initial states. 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.
732
        - **kwargs**: Additional keyword arguments to pass to `forward` of the cell.
F
Feiyu Chan 已提交
733 734

    Returns:
735 736
        - **outputs** (Tensor|list|tuple): the output sequences. If `time_major` is True, the shape is `[time_steps, batch_size, hidden_size]`, else `[batch_size, time_steps, hidden_size]`.
        - **final_states** (Tensor|list|tuple): final states of the cell. Tensor or a possibly nested structure of tensors which has the same structure with intial state. Each tensor in final states has the same shape and dtype as the corresponding tensor in initial states.
737

F
Feiyu Chan 已提交
738 739
    Notes:
        This class is a low level API for wrapping rnn cell into a RNN network.
740 741
        Users should take care of the state of the cell. If `initial_states` is
        passed to the `forward` method, make sure that it satisfies the
F
Feiyu Chan 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        requirements of the cell.

    Examples:

        .. code-block:: python

            import paddle

            inputs = paddle.rand((4, 23, 16))
            prev_h = paddle.randn((4, 32))

            cell = paddle.nn.SimpleRNNCell(16, 32)
            rnn = paddle.nn.RNN(cell)
            outputs, final_states = rnn(inputs, prev_h)

757 758 759 760 761 762
            print(outputs.shape)
            print(final_states.shape)

            #[4,23,32]
            #[4,32]

F
Feiyu Chan 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    """

    def __init__(self, cell, is_reverse=False, time_major=False):
        super(RNN, self).__init__()
        self.cell = cell
        if not hasattr(self.cell, "call"):
            # for non-dygraph mode, `rnn` api uses cell.call
            self.cell.call = self.cell.forward
        self.is_reverse = is_reverse
        self.time_major = time_major

    def forward(self,
                inputs,
                initial_states=None,
                sequence_length=None,
                **kwargs):
779 780 781 782 783 784 785 786
        final_outputs, final_states = paddle.fluid.layers.rnn(
            self.cell,
            inputs,
            initial_states=initial_states,
            sequence_length=sequence_length,
            time_major=self.time_major,
            is_reverse=self.is_reverse,
            **kwargs)
F
Feiyu Chan 已提交
787 788 789 790 791
        return final_outputs, final_states


class BiRNN(Layer):
    r"""
792 793 794
    Wrapper for bidirectional RNN, which builds a bidiretional RNN given the
    forward rnn cell and backward rnn cell. A BiRNN applies forward RNN and
    backward RNN with coresponding cells separately and concats the outputs
F
Feiyu Chan 已提交
795 796
    along the last axis.

797
    Parameters:
F
Feiyu Chan 已提交
798 799 800 801 802 803
        cell_fw (RNNCellBase): A RNNCellBase instance used for forward RNN.
        cell_bw (RNNCellBase): A RNNCellBase instance used for backward RNN.
        time_major (bool): Whether the first dimension of the input means the
            time steps. Defaults to False.

    Inputs:
804 805 806 807
        - **inputs** (Tensor): the input sequences of both RNN. If time_major is True, the shape of is `[time_steps, batch_size, input_size]`, else the shape is `[batch_size, time_steps, input_size]`, where input_size is the input size of both cells.
        - **initial_states** (list|tuple, optional): A tuple/list of the initial states of the forward cell and backward cell. Defaults to None. If not provided, `cell.get_initial_states` would be called to produce the initial states 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.
        - **kwargs**: Additional keyword arguments. Arguments passed to `forward` for each cell.
F
Feiyu Chan 已提交
808 809

    Outputs:
810
        - **outputs** (Tensor): the outputs of the bidirectional RNN. It is the 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`.
811
        - **final_states** (tuple): A tuple of the final states of the forward cell and backward cell.
F
Feiyu Chan 已提交
812 813

    Notes:
814 815 816
        This class is a low level API for wrapping rnn cells into a BiRNN
        network. Users should take care of the states of the cells.
        If `initial_states` is passed to the `forward` method, make sure that
F
Feiyu Chan 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
        it satisfies the requirements of the cells.

    Examples:

        .. code-block:: python

            import paddle

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

            inputs = paddle.rand((2, 23, 16))
            outputs, final_states = rnn(inputs)

832 833 834 835 836 837
            print(outputs.shape)
            print(final_states[0][0].shape,len(final_states),len(final_states[0]))

            #[4,23,64]
            #[2,32] 2 2

F
Feiyu Chan 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    """

    def __init__(self, cell_fw, cell_bw, time_major=False):
        super(BiRNN, self).__init__()
        self.cell_fw = cell_fw
        self.cell_bw = cell_bw
        if cell_fw.input_size != cell_bw.input_size:
            raise ValueError("input size of forward cell({}) does not equals"
                             "that of backward cell({})".format(
                                 cell_fw.input_size, cell_bw.input_size))
        for cell in [self.cell_fw, self.cell_bw]:
            if not hasattr(cell, "call"):
                # for non-dygraph mode, `rnn` api uses cell.call
                cell.call = cell.forward
        self.time_major = time_major

    def forward(self,
                inputs,
                initial_states=None,
                sequence_length=None,
                **kwargs):
        if isinstance(initial_states, (list, tuple)):
            assert len(initial_states) == 2, \
                "length of initial_states should be 2 when it is a list/tuple"

863 864 865
        outputs, final_states = paddle.fluid.layers.birnn(
            self.cell_fw, self.cell_bw, inputs, initial_states, sequence_length,
            self.time_major, **kwargs)
F
Feiyu Chan 已提交
866 867 868
        return outputs, final_states


869
class RNNBase(LayerList):
F
Feiyu Chan 已提交
870
    r"""
871 872
    RNNBase class for RNN networks. It provides `forward`, `flatten_parameters`
    and other common methods for SimpleRNN, LSTM and GRU.
F
Feiyu Chan 已提交
873 874
    """

875 876 877 878 879 880 881 882 883 884 885 886 887
    def __init__(self,
                 mode,
                 input_size,
                 hidden_size,
                 num_layers=1,
                 direction="forward",
                 time_major=False,
                 dropout=0.,
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None):
        super(RNNBase, self).__init__()
888
        bidirectional_list = ["bidirectional", "bidirect"]
889 890 891 892
        self.mode = mode
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.dropout = dropout
893
        self.num_directions = 2 if direction in bidirectional_list else 1
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
        self.time_major = time_major
        self.num_layers = num_layers
        self.state_components = 2 if mode == "LSTM" else 1

        kwargs = {
            "weight_ih_attr": weight_ih_attr,
            "weight_hh_attr": weight_hh_attr,
            "bias_ih_attr": bias_ih_attr,
            "bias_hh_attr": bias_hh_attr
        }

        if mode == "LSTM":
            rnn_cls = LSTMCell
        elif mode == "GRU":
            rnn_cls = GRUCell
        else:
            rnn_cls = SimpleRNNCell
            kwargs["activation"] = self.activation

913 914
        if direction in ["forward"]:
            is_reverse = False
915 916 917 918 919
            cell = rnn_cls(input_size, hidden_size, **kwargs)
            self.append(RNN(cell, is_reverse, time_major))
            for i in range(1, num_layers):
                cell = rnn_cls(hidden_size, hidden_size, **kwargs)
                self.append(RNN(cell, is_reverse, time_major))
920
        elif direction in bidirectional_list:
921 922 923 924 925 926 927 928 929
            cell_fw = rnn_cls(input_size, hidden_size, **kwargs)
            cell_bw = rnn_cls(input_size, hidden_size, **kwargs)
            self.append(BiRNN(cell_fw, cell_bw, time_major))
            for i in range(1, num_layers):
                cell_fw = rnn_cls(2 * hidden_size, hidden_size, **kwargs)
                cell_bw = rnn_cls(2 * hidden_size, hidden_size, **kwargs)
                self.append(BiRNN(cell_fw, cell_bw, time_major))
        else:
            raise ValueError(
930
                "direction should be forward or bidirect (or bidirectional), "
931 932
                "received direction = {}".format(direction))

933
        self.could_use_cudnn = True
934
        self.could_use_cudnn &= len(self.parameters()) == num_layers * 4 * (
935
            2 if direction in bidirectional_list else 1)
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

        # Expose params as RNN's attribute, which can make it compatible when
        # replacing small ops composed rnn with cpp rnn kernel.
        # Moreover, `jit.to_static` assumes params are added by current layer
        # and wouldn't include sublayer's params in current layer, which also
        # requires these params are added to current layer for `jit.save`.
        param_names = []
        for layer in range(self.num_layers):
            for direction in range(self.num_directions):
                suffix = '_reverse' if direction == 1 else ''
                param_names.extend(['weight_ih_l{}{}', 'weight_hh_l{}{}'])
                if bias_ih_attr != False: param_names.append('bias_ih_l{}{}')
                if bias_hh_attr != False: param_names.append('bias_hh_l{}{}')
                param_names = [x.format(layer, suffix) for x in param_names]
        for name, param in zip(param_names, self.parameters()):
            setattr(self, name, param)

        self.flatten_parameters()

    def flatten_parameters(self):
        """
        Resets parameter data pointer to address in continuous memory block for
        cudnn usage.
        """
        if self.could_use_cudnn:
            # layer.parameters() is depth first and ordered
            # for i in layer: for j in direct: w_ih, w_hh, b_ih, b_hh
            # need to reorganize to cudnn param layout:
            # all bias following all weights
            params = self.parameters(include_sublayers=False)
            shape = [np.prod(param.shape) for param in params]
            self._all_weights = [None] * len(params)
            for i, param in enumerate(params):
                offset = 0 if i % 4 < 2 else (2 * self.num_layers *
                                              self.num_directions)
                layer_idx = i // 4
                self._all_weights[offset + layer_idx * 2 + i % 2] = param
            # Wrap using a list to avoid registed into params and saving, maybe
            # need a better way to handle this later. Use `create_parameter` to
            # add both to main_program and startup_program for static-graph.
            # Use Constant initializer to avoid make effect on random generator.
            self._flat_weight = [
978 979 980
                self.create_parameter(shape=[np.sum(shape)],
                                      dtype=params[0].dtype,
                                      default_initializer=I.Constant(0.0))
981 982 983 984
            ]
            # dropout state may also can be hided and avoid saving
            # should dropout state be persistable for static-graph
            self._dropout_state = self.create_variable(
Z
zhiboniu 已提交
985 986
                dtype=core.VarDesc.VarType.UINT8)
            if in_dynamic_mode():
987
                with paddle.no_grad():
988 989 990 991 992 993
                    _legacy_C_ops.coalesce_tensor(self._all_weights,
                                                  self._all_weights,
                                                  self._flat_weight[0],
                                                  "copy_data", True,
                                                  "use_align", False, "dtype",
                                                  params[0].dtype)
994
                    return
995
            # for static-graph, append coalesce_tensor into startup program
Z
zhiboniu 已提交
996 997
            with program_guard(default_startup_program(),
                               default_startup_program()):
Z
zhiboniu 已提交
998
                with paddle.no_grad():
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
                    self._helper.append_op(type="coalesce_tensor",
                                           inputs={"Input": self._all_weights},
                                           outputs={
                                               "Output": self._all_weights,
                                               "FusedOutput": self._flat_weight
                                           },
                                           attrs={
                                               "copy_data": True,
                                               "use_align": False,
                                               "dtype": params[0].dtype
                                           })
1010 1011 1012 1013 1014

    def _cudnn_impl(self, inputs, initial_states, sequence_length):
        if not self.time_major:
            inputs = paddle.tensor.transpose(inputs, [1, 0, 2])

Z
zhiboniu 已提交
1015
        if in_dynamic_mode():
1016
            _, _, out, state = _legacy_C_ops.rnn(
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                inputs, initial_states, self._all_weights, sequence_length,
                self._dropout_state, self.state_components, 'dropout_prob',
                self.dropout, 'is_bidirec', self.num_directions == 2,
                'input_size', self.input_size, 'hidden_size', self.hidden_size,
                'num_layers', self.num_layers, 'mode', self.mode, 'is_test',
                not self.training)
        else:
            out = self._helper.create_variable_for_type_inference(inputs.dtype)
            state = [
                self._helper.create_variable_for_type_inference(inputs.dtype)
                for i in range(self.state_components)
            ]
            reserve = self._helper.create_variable_for_type_inference(
Z
zhiboniu 已提交
1030
                dtype=core.VarDesc.VarType.UINT8, stop_gradient=True)
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

            inputs = {
                'Input': inputs,
                'WeightList': self._all_weights,
                'PreState': initial_states,
                'SequenceLength': sequence_length
            }
            attrs = {
                'dropout_prob': self.dropout,
                'is_bidirec': self.num_directions == 2,
                'input_size': self.input_size,
                'hidden_size': self.hidden_size,
                'num_layers': self.num_layers,
                'mode': self.mode,
                'is_test': not self.training
            }

            outputs = {
                'Out': out,
                'State': state,
                'Reserve': reserve,
                'DropoutState': self._dropout_state,
            }

1055 1056 1057 1058
            self._helper.append_op(type="rnn",
                                   inputs=inputs,
                                   outputs=outputs,
                                   attrs=attrs)
1059 1060 1061

        out = paddle.tensor.transpose(out,
                                      [1, 0, 2]) if not self.time_major else out
G
Guo Sheng 已提交
1062
        return out, tuple(state) if len(state) > 1 else state[0]
1063

F
Feiyu Chan 已提交
1064 1065 1066 1067 1068 1069
    def forward(self, inputs, initial_states=None, sequence_length=None):
        batch_index = 1 if self.time_major else 0
        dtype = inputs.dtype
        if initial_states is None:
            state_shape = (self.num_layers * self.num_directions, -1,
                           self.hidden_size)
1070 1071
            initial_states = tuple([
                paddle.fluid.layers.fill_constant_batch_size_like(
F
Feiyu Chan 已提交
1072
                    inputs, state_shape, dtype, 0, batch_index, 1)
1073 1074 1075 1076
                for _ in range(self.state_components)
            ])
        else:
            initial_states = [initial_states] if isinstance(
Z
zhiboniu 已提交
1077
                initial_states, paddle.static.Variable) else initial_states
F
Feiyu Chan 已提交
1078

1079 1080
        if self.could_use_cudnn and (not paddle.device.is_compiled_with_rocm()
                                     or sequence_length is None):
1081 1082 1083
            # Add CPU kernel and dispatch in backend later
            return self._cudnn_impl(inputs, initial_states, sequence_length)

F
Feiyu Chan 已提交
1084 1085 1086 1087 1088 1089
        states = split_states(initial_states, self.num_directions == 2,
                              self.state_components)
        final_states = []

        for i, rnn_layer in enumerate(self):
            if i > 0:
1090 1091 1092 1093
                inputs = F.dropout(inputs,
                                   self.dropout,
                                   training=self.training,
                                   mode="upscale_in_train")
F
Feiyu Chan 已提交
1094 1095 1096 1097 1098 1099 1100 1101
            outputs, final_state = rnn_layer(inputs, states[i], sequence_length)
            final_states.append(final_state)
            inputs = outputs

        final_states = concat_states(final_states, self.num_directions == 2,
                                     self.state_components)
        return outputs, final_states

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    def extra_repr(self):
        main_str = '{input_size}, {hidden_size}'
        if self.num_layers != 1:
            main_str += ', num_layers={num_layers}'
        if self.time_major != False:
            main_str += ', time_major={time_major}'
        if self.dropout != 0:
            main_str += ', dropout={dropout}'
        return main_str.format(**self.__dict__)

F
Feiyu Chan 已提交
1112

1113
class SimpleRNN(RNNBase):
F
Feiyu Chan 已提交
1114
    r"""
1115
    Multilayer Elman network(SimpleRNN). It takes input sequences and initial
F
Feiyu Chan 已提交
1116 1117
    states as inputs, and returns the output sequences and the final states.

1118 1119 1120
    Each layer inside the SimpleRNN maps the input sequences and initial states
    to the output sequences and final states in the following manner: at each
    step, it takes step inputs(:math:`x_{t}`) and previous
F
Feiyu Chan 已提交
1121 1122 1123 1124 1125
    states(:math:`h_{t-1}`) as inputs, and returns step outputs(:math:`y_{t}`)
    and new states(:math:`h_{t}`).

    .. math::

1126
        h_{t} & = act(W_{ih}x_{t} + b_{ih} + W_{hh}h_{t-1} + b_{hh})
1127

F
Feiyu Chan 已提交
1128
        y_{t} & = h_{t}
1129

1130
    where :math:`act` is for :attr:`activation`.
1131 1132

    Using key word arguments to construct is recommended.
F
Feiyu Chan 已提交
1133

1134
    Parameters:
1135 1136 1137
        input_size (int): The input size of :math:`x` for the first layer's cell.
        hidden_size (int): The hidden size of :math:`h` for each layer's cell.
        num_layers (int, optional): Number of recurrent layers. Defaults to 1.
1138 1139
        direction (str, optional): The direction of the network. It can be "forward"
            or "bidirect"(or "bidirectional"). When "bidirect", the way to merge
1140
            outputs of forward and backward is concatenating. Defaults to "forward".
1141 1142
        time_major (bool, optional): Whether the first dimension of the input
            means the time steps. If time_major is True, the shape of Tensor is
1143 1144
            [time_steps,batch_size,input_size], otherwise [batch_size, time_steps,input_size].
            Defaults to False. `time_steps` means the length of input sequence.
1145 1146
        dropout (float, optional): The droput probability. Dropout is applied
            to the input of each layer except for the first layer. The range of
1147
            dropout from 0 to 1. Defaults to 0.
1148
        activation (str, optional): The activation in each SimpleRNN cell. It can be
1149
            `tanh` or `relu`. Defaults to `tanh`.
1150
        weight_ih_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1151
            `weight_ih` of each cell. Defaults to None.
1152
        weight_hh_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1153
            `weight_hh` of each cell. Defaults to None.
1154
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1155
            `bias_ih` of each cells. Defaults to None.
1156
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1157
            `bias_hh` of each cells. Defaults to None.
1158
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
1159 1160
            None). For more information, please refer to :ref:`api_guide_Name`.

1161
    Inputs:
1162
        - **inputs** (Tensor): the input sequence. If `time_major` is True, the shape is `[time_steps, batch_size, input_size]`, else, the shape is `[batch_size, time_steps, input_size]`. `time_steps` means the length of the input sequence.
1163 1164
        - **initial_states** (Tensor, optional): the initial state. The shape is `[num_layers * num_directions, batch_size, hidden_size]`. If initial_state is not given, zero initial states are used.
        - **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.
F
Feiyu Chan 已提交
1165 1166

    Returns:
1167

1168
        - **outputs** (Tensor): the output sequence. If `time_major` is True, the shape is `[time_steps, batch_size, num_directions * hidden_size]`, else, the shape is `[batch_size, time_steps, num_directions * hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" else 1. `time_steps` means the length of the output sequence.
1169

1170
        - **final_states** (Tensor): final states. The shape is `[num_layers * num_directions, batch_size, hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" (the index of forward states are 0, 2, 4, 6... and the index of backward states are 1, 3, 5, 7...), else 1.
1171 1172 1173 1174 1175 1176

    Variables:
        - **weight_ih_l[k]**: the learnable input-hidden weights of the k-th layer. If `k = 0`, the shape is `[hidden_size, input_size]`. Otherwise, the shape is `[hidden_size, num_directions * hidden_size]`.
        - **weight_hh_l[k]**: the learnable hidden-hidden weights of the k-th layer, with shape `[hidden_size, hidden_size]`.
        - **bias_ih_l[k]**: the learnable input-hidden bias of the k-th layer, with shape `[hidden_size]`.
        - **bias_hh_l[k]**: the learnable hidden-hidden bias of the k-th layer, with shape `[hidden_size]`.
1177

F
Feiyu Chan 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    Examples:

        .. code-block:: python

            import paddle

            rnn = paddle.nn.SimpleRNN(16, 32, 2)

            x = paddle.randn((4, 23, 16))
            prev_h = paddle.randn((2, 4, 32))
            y, h = rnn(x, prev_h)

1190 1191 1192 1193 1194 1195
            print(y.shape)
            print(h.shape)

            #[4,23,32]
            #[2,4,32]

F
Feiyu Chan 已提交
1196 1197 1198 1199 1200 1201 1202 1203
    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 num_layers=1,
                 direction="forward",
                 time_major=False,
1204 1205
                 dropout=0.,
                 activation="tanh",
F
Feiyu Chan 已提交
1206 1207 1208 1209 1210
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
1211 1212 1213 1214
        if activation == "tanh":
            mode = "RNN_TANH"
        elif activation == "relu":
            mode = "RNN_RELU"
F
Feiyu Chan 已提交
1215
        else:
1216 1217
            raise ValueError("Unknown activation '{}'".format(activation))
        self.activation = activation
1218 1219 1220 1221
        super(SimpleRNN,
              self).__init__(mode, input_size, hidden_size, num_layers,
                             direction, time_major, dropout, weight_ih_attr,
                             weight_hh_attr, bias_ih_attr, bias_hh_attr)
F
Feiyu Chan 已提交
1222 1223


1224
class LSTM(RNNBase):
F
Feiyu Chan 已提交
1225
    r"""
1226
    Multilayer LSTM. It takes a sequence and an initial state as inputs, and
F
Feiyu Chan 已提交
1227 1228
    returns the output sequences and the final states.

1229 1230 1231 1232
    Each layer inside the LSTM maps the input sequences and initial states
    to the output sequences and final states in the following manner: at each
    step, it takes step inputs(:math:`x_{t}`) and previous
    states(:math:`h_{t-1}, c_{t-1}`) as inputs, and returns step
F
Feiyu Chan 已提交
1233 1234 1235 1236 1237
    outputs(:math:`y_{t}`) and new states(:math:`h_{t}, c_{t}`).

    .. math::

        i_{t} & = \sigma(W_{ii}x_{t} + b_{ii} + W_{hi}h_{t-1} + b_{hi})
1238

F
Feiyu Chan 已提交
1239
        f_{t} & = \sigma(W_{if}x_{t} + b_{if} + W_{hf}h_{t-1} + b_{hf})
1240

F
Feiyu Chan 已提交
1241
        o_{t} & = \sigma(W_{io}x_{t} + b_{io} + W_{ho}h_{t-1} + b_{ho})
1242 1243 1244 1245 1246 1247 1248

        \widetilde{c}_{t} & = \tanh (W_{ig}x_{t} + b_{ig} + W_{hg}h_{t-1} + b_{hg})

        c_{t} & = f_{t} * c_{t-1} + i_{t} * \widetilde{c}_{t}

        h_{t} & = o_{t} * \tanh(c_{t})

F
Feiyu Chan 已提交
1249 1250
        y_{t} & = h_{t}

1251
    where :math:`\sigma` is the sigmoid fucntion, and * is the elemetwise
F
Feiyu Chan 已提交
1252 1253
    multiplication operator.

1254 1255
    Using key word arguments to construct is recommended.

1256
    Parameters:
1257 1258 1259
        input_size (int): The input size of :math:`x` for the first layer's cell.
        hidden_size (int): The hidden size of :math:`h` for each layer's cell.
        num_layers (int, optional): Number of recurrent layers. Defaults to 1.
1260 1261
        direction (str, optional): The direction of the network. It can be "forward"
            or "bidirect"(or "bidirectional"). When "bidirect", the way to merge
1262
            outputs of forward and backward is concatenating. Defaults to "forward".
1263 1264
        time_major (bool, optional): Whether the first dimension of the input
            means the time steps. If time_major is True, the shape of Tensor is
1265 1266
            [time_steps,batch_size,input_size], otherwise [batch_size, time_steps,input_size].
            Defaults to False. `time_steps` means the length of input sequence.
1267 1268
        dropout (float, optional): The droput probability. Dropout is applied
            to the input of each layer except for the first layer. The range of
1269
            dropout from 0 to 1. Defaults to 0.
1270
        weight_ih_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1271
            `weight_ih` of each cell. Default: None.
1272
        weight_hh_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1273
            `weight_hh` of each cell. Default: None.
1274
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1275
            `bias_ih` of each cells. Default: None.
1276
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1277
            `bias_hh` of each cells. Default: None.
1278
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
1279 1280 1281
            None). For more information, please refer to :ref:`api_guide_Name`.

    Inputs:
1282
        - **inputs** (Tensor): the input sequence. If `time_major` is True, the shape is `[time_steps, batch_size, input_size]`, else, the shape is `[batch_size, time_steps, input_size]`. `time_steps` means the length of the input sequence.
1283
        - **initial_states** (list|tuple, optional): the initial state, a list/tuple of (h, c), the shape of each is `[num_layers * num_directions, batch_size, hidden_size]`. If initial_state is not given, zero initial states are used.
1284
        - **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 whos time step index are not less than the valid length are treated as paddings.
F
Feiyu Chan 已提交
1285 1286

    Returns:
1287

1288
        - **outputs** (Tensor): the output sequence. If `time_major` is True, the shape is `[time_steps, batch_size, num_directions * hidden_size]`, If `time_major` is False, the shape is `[batch_size, time_steps, num_directions * hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" else 1. `time_steps` means the length of the output sequence.
1289

1290
        - **final_states** (tuple): the final state, a tuple of two tensors, h and c. The shape of each is `[num_layers * num_directions, batch_size, hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" (the index of forward states are 0, 2, 4, 6... and the index of backward states are 1, 3, 5, 7...), else 1.
1291 1292 1293 1294 1295 1296

    Variables:
        - **weight_ih_l[k]**: the learnable input-hidden weights of the k-th layer. If `k = 0`, the shape is `[hidden_size, input_size]`. Otherwise, the shape is `[hidden_size, num_directions * hidden_size]`.
        - **weight_hh_l[k]**: the learnable hidden-hidden weights of the k-th layer, with shape `[hidden_size, hidden_size]`.
        - **bias_ih_l[k]**: the learnable input-hidden bias of the k-th layer, with shape `[hidden_size]`.
        - **bias_hh_l[k]**: the learnable hidden-hidden bias of the k-th layer, swith shape `[hidden_size]`.
1297

F
Feiyu Chan 已提交
1298
    Examples:
1299

F
Feiyu Chan 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        .. code-block:: python

            import paddle

            rnn = paddle.nn.LSTM(16, 32, 2)

            x = paddle.randn((4, 23, 16))
            prev_h = paddle.randn((2, 4, 32))
            prev_c = paddle.randn((2, 4, 32))
            y, (h, c) = rnn(x, (prev_h, prev_c))

1311 1312 1313 1314 1315 1316 1317 1318
            print(y.shape)
            print(h.shape)
            print(c.shape)

            #[4,23,32]
            #[2,4,32]
            #[2,4,32]

F
Feiyu Chan 已提交
1319 1320 1321 1322 1323 1324 1325 1326
    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 num_layers=1,
                 direction="forward",
                 time_major=False,
1327
                 dropout=0.,
F
Feiyu Chan 已提交
1328 1329 1330 1331 1332
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
1333 1334 1335 1336
        super(LSTM,
              self).__init__("LSTM", input_size, hidden_size, num_layers,
                             direction, time_major, dropout, weight_ih_attr,
                             weight_hh_attr, bias_ih_attr, bias_hh_attr)
F
Feiyu Chan 已提交
1337 1338


1339
class GRU(RNNBase):
F
Feiyu Chan 已提交
1340
    r"""
1341
    Multilayer GRU. It takes input sequencse and initial states as inputs, and
F
Feiyu Chan 已提交
1342 1343
    returns the output sequences and the final states.

1344 1345 1346 1347
    Each layer inside the GRU maps the input sequences and initial states
    to the output sequences and final states in the following manner: at each
    step, it takes step inputs(:math:`x_{t}`) and previous
    states(:math:`h_{t-1}`) as inputs, and returns step outputs(:math:`y_{t}`)
F
Feiyu Chan 已提交
1348 1349 1350 1351
    and new states(:math:`h_{t}`).

    .. math::

1352
        r_{t} & = \sigma(W_{ir}x_{t} + b_{ir} + W_{hr}h_{t-1} + b_{hr})
1353

1354
        z_{t} & = \sigma(W_{iz}x_{t} + b_{iz} + W_{hz}h_{t-1} + b_{hz})
1355

1356
        \widetilde{h}_{t} & = \tanh(W_{ic}x_{t} + b_{ic} + r_{t} * (W_{hc}h_{t-1} + b_{hc}))
1357 1358 1359

        h_{t} & = z_{t} * h_{t-1} + (1 - z_{t}) * \widetilde{h}_{t}

F
Feiyu Chan 已提交
1360 1361
        y_{t} & = h_{t}

1362
    where :math:`\sigma` is the sigmoid fucntion, and * is the elemetwise
F
Feiyu Chan 已提交
1363 1364
    multiplication operator.

1365 1366
    Using key word arguments to construct is recommended.

1367
    Parameters:
1368 1369 1370
        input_size (int): The input size of :math:`x` for the first layer's cell.
        hidden_size (int): The hidden size of :math:`h` for each layer's cell.
        num_layers (int, optional): Number of recurrent layers. Defaults to 1.
1371 1372
        direction (str, optional): The direction of the network. It can be "forward"
            or "bidirect"(or "bidirectional"). When "bidirect", the way to merge
1373
            outputs of forward and backward is concatenating. Defaults to "forward".
1374 1375
        time_major (bool, optional): Whether the first dimension of the input
            means the time steps. If time_major is True, the shape of Tensor is
1376 1377
            [time_steps,batch_size,input_size], otherwise [batch_size, time_steps,input_size].
            Defaults to False. `time_steps` means the length of input sequence.
1378 1379
        dropout (float, optional): The droput probability. Dropout is applied
            to the input of each layer except for the first layer. The range of
1380
            dropout from 0 to 1. Defaults to 0.
1381
        weight_ih_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1382
            `weight_ih` of each cell. Default: None.
1383
        weight_hh_attr (ParamAttr, optional): The parameter attribute for
F
Feiyu Chan 已提交
1384
            `weight_hh` of each cell. Default: None.
1385
        bias_ih_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1386
            `bias_ih` of each cells. Default: None.
1387
        bias_hh_attr (ParamAttr, optional): The parameter attribute for the
F
Feiyu Chan 已提交
1388
            `bias_hh` of each cells. Default: None.
1389
        name (str, optional): Name for the operation (optional, default is
F
Feiyu Chan 已提交
1390 1391 1392
            None). For more information, please refer to :ref:`api_guide_Name`.

    Inputs:
1393
        - **inputs** (Tensor): the input sequence. If `time_major` is True, the shape is `[time_steps, batch_size, input_size]`, else, the shape is `[batch_size, time_steps, input_size]`. `time_steps` means the length of the input sequence.
1394 1395
        - **initial_states** (Tensor, optional): the initial state. The shape is `[num_layers * num_directions, batch_size, hidden_size]`. If initial_state is not given, zero initial states are used. 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 whos time step index are not less than the valid length are treated as paddings.
F
Feiyu Chan 已提交
1396 1397

    Returns:
1398

1399
        - **outputs** (Tensor): the output sequence. If `time_major` is True, the shape is `[time_steps, batch_size, num_directions * hidden_size]`, else, the shape is `[batch_size, time_steps, num_directions * hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" else 1. `time_steps` means the length of the output sequence.
1400

1401
        - **final_states** (Tensor): final states. The shape is `[num_layers * num_directions, batch_size, hidden_size]`. Note that `num_directions` is 2 if direction is "bidirectional" (the index of forward states are 0, 2, 4, 6... and the index of backward states are 1, 3, 5, 7...), else 1.
1402 1403 1404 1405 1406 1407

    Variables:
        - **weight_ih_l[k]**: the learnable input-hidden weights of the k-th layer. If `k = 0`, the shape is `[hidden_size, input_size]`. Otherwise, the shape is `[hidden_size, num_directions * hidden_size]`.
        - **weight_hh_l[k]**: the learnable hidden-hidden weights of the k-th layer, with shape `[hidden_size, hidden_size]`.
        - **bias_ih_l[k]**: the learnable input-hidden bias of the k-th layer, with shape `[hidden_size]`.
        - **bias_hh_l[k]**: the learnable hidden-hidden bias of the k-th layer, with shape `[hidden_size]`.
1408

F
Feiyu Chan 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    Examples:

        .. code-block:: python

            import paddle

            rnn = paddle.nn.GRU(16, 32, 2)

            x = paddle.randn((4, 23, 16))
            prev_h = paddle.randn((2, 4, 32))
            y, h = rnn(x, prev_h)

1421 1422 1423 1424 1425 1426
            print(y.shape)
            print(h.shape)

            #[4,23,32]
            #[2,4,32]

F
Feiyu Chan 已提交
1427 1428 1429 1430 1431 1432 1433 1434
    """

    def __init__(self,
                 input_size,
                 hidden_size,
                 num_layers=1,
                 direction="forward",
                 time_major=False,
1435
                 dropout=0.,
F
Feiyu Chan 已提交
1436 1437 1438 1439 1440
                 weight_ih_attr=None,
                 weight_hh_attr=None,
                 bias_ih_attr=None,
                 bias_hh_attr=None,
                 name=None):
1441 1442 1443 1444
        super(GRU,
              self).__init__("GRU", input_size, hidden_size, num_layers,
                             direction, time_major, dropout, weight_ih_attr,
                             weight_hh_attr, bias_ih_attr, bias_hh_attr)