mindspore.nn.RNNCell
- class mindspore.nn.RNNCell(input_size: int, hidden_size: int, has_bias: bool = True, nonlinearity: str = 'tanh', dtype=mstype.float32)[源代码]
循环神经网络单元,激活函数是tanh或relu。
其中
是在 t 时刻的隐藏状态, 是在 t 时刻的输入, 是在 时刻的隐藏状态,或初始隐藏状态。- 参数:
input_size (int) - 输入层输入的特征向量维度。
hidden_size (int) - 隐藏层输出的特征向量维度。
has_bias (bool) - Cell是否有偏置项
和 。默认值:True
。nonlinearity (str) - 用于选择非线性激活函数。取值可以是'tanh'或'relu'。默认值:
'tanh'
。dtype (
mindspore.dtype
) - Parameters的dtype。默认值:mstype.float32
。
- 输入:
x (Tensor) - 输入Tensor,其shape为
。hx (Tensor) - 输入Tensor,其数据类型为mindspore.float32及shape为
。
- 输出:
hx' (Tensor) - shape为
的Tensor。
- 异常:
TypeError - input_size 或 hidden_size 不是int或不大于0。
TypeError - has_bias 不是bool。
ValueError - nonlinearity 不在['tanh', 'relu']中。
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore as ms >>> import numpy as np >>> net = ms.nn.RNNCell(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)