LSTMCell

class paddle.nn. LSTMCell ( input_size, hidden_size, weight_ih_attr=None, weight_hh_attr=None, bias_ih_attr=None, bias_hh_attr=None, name=None ) [源代码]

长短期记忆网络单元

该OP是长短期记忆网络单元(LSTMCell),根据当前时刻输入x(t)和上一时刻状态h(t-1)计算当前时刻输出y(t)并更新状态h(t)。

状态更新公式如下:

LSTMCell - 图1

其中:

  • LSTMCell - 图2

    :sigmoid激活函数。

详情请参考论文 :An Empirical Exploration of Recurrent Network Architectures

参数:

  • input_size (int) - 输入的大小。

  • hidden_size (int) - 隐藏状态大小。

  • weight_ih_attr (ParamAttr,可选) - weight_ih的参数。默认为None。

  • weight_hh_attr (ParamAttr,可选) - weight_hh的参数。默认为None。

  • bias_ih_attr (ParamAttr,可选) - bias_ih的参数。默认为None。

  • bias_hh_attr (ParamAttr,可选) - bias_hh的参数。默认为None。

  • name (str, 可选): OP的名字。默认为None。详情请参考 Name

变量:

  • weight_ih (Parameter) - input到hidden的变换矩阵的权重。形状为(4 * hidden_size, input_size)。对应公式中的

    LSTMCell - 图3

  • weight_hh (Parameter) - hidden到hidden的变换矩阵的权重。形状为(4 * hidden_size, hidden_size)。对应公式中的

    LSTMCell - 图4

  • bias_ih (Parameter) - input到hidden的变换矩阵的偏置。形状为(4 * hidden_size, )。对应公式中的

    LSTMCell - 图5

  • bias_hh (Parameter) - hidden到hidden的变换矩阵的偏置。形状为(4 * hidden_size, )。对应公式中的

    LSTMCell - 图6

输入:

  • inputs (Tensor) - 输入。形状为[batch_size, input_size],对应公式中的 xtxt。

  • states (tuple,可选) - 一个包含两个Tensor的元组,每个Tensor的形状都为[batch_size, hidden_size],上一轮的隐藏状态。对应公式中的 ht−1,ct−1ht−1,ct−1。当state为None的时候,初始状态为全0矩阵。默认为None。

输出:

  • outputs (Tensor) - 输出。形状为[batch_size, hidden_size],对应公式中的 htht。

  • new_states (tuple) - 一个包含两个Tensor的元组,每个Tensor的形状都为[batch_size, hidden_size],新一轮的隐藏状态。形状为[batch_size, hidden_size],对应公式中的 ht,ctht,ct。

注解

所有的变换矩阵的权重和偏置都默认初始化为Uniform(-std, std),其中std = 1hidden_size√1hidden_size。对于参数初始化,详情请参考 ParamAttr

代码示例

  1. import paddle
  2. x = paddle.randn((4, 16))
  3. prev_h = paddle.randn((4, 32))
  4. prev_c = paddle.randn((4, 32))
  5. cell = paddle.nn.LSTMCell(16, 32)
  6. y, (h, c) = cell(x, (prev_h, prev_c))
  7. print(y.shape)
  8. print(h.shape)
  9. print(c.shape)
  10. #[4,32]
  11. #[4,32]
  12. #[4,32]