mindspore.nn.LSTMCell

查看源文件
class mindspore.nn.LSTMCell(input_size: int, hidden_size: int, has_bias: bool = True, dtype=mstype.float32)[源代码]

长短期记忆网络单元(LSTMCell)。

公式如下:

it=σ(Wixxt+bix+Wihh(t1)+bih)ft=σ(Wfxxt+bfx+Wfhh(t1)+bfh)c~t=tanh(Wcxxt+bcx+Wchh(t1)+bch)ot=σ(Woxxt+box+Wohh(t1)+boh)ct=ftc(t1)+itc~tht=ottanh(ct)

其中 σ 是sigmoid函数, 是乘积。 W,b 是公式中输出和输入之间的可学习权重。例如,Wix,bix 是表示从输入 x 转换为 i 的权重和偏置。详见论文 LONG SHORT-TERM MEMORYLong Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling

nn层封装的LSTMCell可以简化为如下公式:

h,c=LSTMCell(x,(h0,c0))
参数:
  • input_size (int) - 输入的大小。

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

  • has_bias (bool) - cell是否有偏置 b_ihb_hh 。默认值: True

  • dtype (mindspore.dtype) - Parameters的dtype。默认值: mstype.float32

输入:
  • x (Tensor) - shape为 (batch_size,input_size) 的Tensor。

  • hx (tuple) - 两个Tensor(h_0,c_0)的元组,其数据类型为mindspore.float32,shape为 (batch_size,hidden_size)

输出:
  • hx' (Tensor) - 两个Tensor(h', c')的元组,其shape为 (batch_size,hidden_size)

异常:
  • TypeError - input_sizehidden_size 不是整数。

  • TypeError - has_bias 不是bool。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore as ms
>>> import numpy as np
>>> net = ms.nn.LSTMCell(10, 16)
>>> x = ms.Tensor(np.ones([5, 3, 10]).astype(np.float32))
>>> h = ms.Tensor(np.ones([3, 16]).astype(np.float32))
>>> c = ms.Tensor(np.ones([3, 16]).astype(np.float32))
>>> output = []
>>> for i in range(5):
...     hx = net(x[i], (h, c))
...     output.append(hx)
>>> print(output[0][0].shape)
(3, 16)