GRUCell

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

门控循环单元

该OP是门控循环单元(GRUCell),根据当前时刻输入x(t)和上一时刻状态h(t-1)计算当前时刻输出y(t)并更新状态h(t)。

状态更新公式如下:

GRUCell - 图1

其中:

  • GRUCell - 图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的变换矩阵的权重。形状为(3 * hidden_size, input_size)。对应公式中的

    GRUCell - 图3

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

    GRUCell - 图4

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

    GRUCell - 图5

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

    GRUCell - 图6

输入:

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

  • states (Tensor,可选) - 上一轮的隐藏状态。对应公式中的 ht−1ht−1。当state为None的时候,初始状态为全0矩阵。默认为None。

输出:

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

  • new_states (Tensor) - 新一轮的隐藏状态。形状为[batch_size, hidden_size],对应公式中的 htht。

注解

所有的变换矩阵的权重和偏置都默认初始化为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. cell = paddle.nn.GRUCell(16, 32)
  5. y, h = cell(x, prev_h)
  6. print(y.shape)
  7. print(h.shape)
  8. #[4,32]
  9. #[4,32]