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)。

状态更新公式如下:

it=σ(Wiixt+bii+Whiht1+bhi)ft=σ(Wifxt+bif+Whfht1+bhf)ot=σ(Wioxt+bio+Whoht1+bho)gt=tanh(Wigxt+big+Whght1+bhg)ct=ftct1+itgtht=ottanh(ct)yt=ht

其中:

  • σ :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,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

变量

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

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

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

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

输入

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

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

输出

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

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

注解

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

代码示例

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))

print(y.shape)
print(h.shape)
print(c.shape)

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