mindspore.nn.GRUCell

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

GRU(Gate Recurrent Unit)称为门控循环单元。

r=σ(Wirx+bir+Whrh+bhr)z=σ(Wizx+biz+Whzh+bhz)n=tanh(Winx+bin+r(Whnh+bhn))h=(1z)n+zh

这里 σ 是sigmoid激活函数, 是乘积。 W,b 是公式中输出和输入之间的可学习权重, h 是隐藏层状态(hidden state), r 是重置门(reset gate), z 是更新门(update gate), n 是第n层。 例如, Wir,bir 是用于将输入 x 转换为 r 的权重和偏置。详见论文 Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation

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

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

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

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

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

  • hx (Tensor) - 数据类型为mindspore.float32,shape为 (batch_size,hidden_size) 的Tensor。

输出:
  • hx' (Tensor) - shape为 (batch_size,hidden_size) 的Tensor。

异常:
  • TypeError - input_sizehidden_size 不是int。

  • TypeError - has_bias 不是bool值。

支持平台:

Ascend GPU CPU

样例:

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