mindspore.nn.RNNCell
- class mindspore.nn.RNNCell(input_size, hidden_size, has_bias=True, nonlinearity='tanh', dtype=mstype.float32)[源代码]
- 循环神经网络单元,激活函数是tanh或relu。 \[h_t = \tanh(W_{ih} x_t + b_{ih} + W_{hh} h_{(t-1)} + b_{hh})\]- 其中 \(h_t\) 是在 t 时刻的隐藏状态, \(x_t\) 是在 t 时刻的输入, \(h_{(t-1)}\) 是在 \(t-1\) 时刻的隐藏状态,或初始隐藏状态。 - 参数:
- input_size (int) - 输入层输入的特征向量维度。 
- hidden_size (int) - 隐藏层输出的特征向量维度。 
- has_bias (bool) - Cell是否有偏置项 \(b_{ih}\) 和 \(b_{hh}\) 。默认值: - True。
- nonlinearity (str) - 用于选择非线性激活函数。取值可以是'tanh'或'relu'。默认值: - 'tanh'。
- dtype ( - mindspore.dtype) - Parameters的dtype。默认值:- mstype.float32。
 
- 输入:
- x (Tensor) - 输入Tensor,其shape为 \((batch\_size, input\_size)\) 。 
- hx (Tensor) - 输入Tensor,其数据类型为mindspore.float32及shape为 \((batch\_size, hidden\_size)\) 。 
 
- 输出:
- hx' (Tensor) - shape为 \((batch\_size, hidden\_size)\) 的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)