mindspore.ops.LSTM
- class mindspore.ops.LSTM(input_size, hidden_size, num_layers, has_bias, bidirectional, dropout, proj_size=0)[source]
Performs the Long Short-Term Memory (LSTM) on the input.
For more information, please refer to
mindspore.nn.LSTM
.- Parameters
input_size (int) – Number of features of input.
hidden_size (int) – Number of features of hidden layer.
num_layers (int) – Number of layers of stacked LSTM, , which is only support 1 on CPU.
has_bias (bool) – Whether the cell has bias b_ih and b_hh , which is only support False on CPU.
bidirectional (bool) – Specifies whether it is a bidirectional LSTM, , which is only support False on CPU.
dropout (float) – If not 0, append Dropout layer on the outputs of each LSTM layer except the last layer. The range of dropout is [0.0, 1.0].
proj_size (int) – If proj_size > 0, a projection of the corresponding size will be used, which is only supported on CPU now. Default:
0
.
- Inputs:
input (Tensor) - Tensor of shape \((seq\_len, batch\_size, input\_size)\) or \((batch\_size, seq\_len, input\_size)\).
h (Tensor) - Tensor of shape \((num\_directions * num\_layers, batch\_size, real\_hidden\_size)\).
c (Tensor) - Tensor of shape \((num\_directions * num\_layers, batch\_size, hidden\_size)\).
w (Tensor) - A weight Tensor.
If \(proj\_size > 0\) , \(real\_hidden\_size = proj\_size\) , otherwise \(real\_hidden\_size = hidden\_size\) .
- Outputs:
Tuple, a tuple contains (output, h_n, c_n, reserve, state).
output (Tensor) - Tensor of shape \((seq\_len, batch\_size, num\_directions * real\_hidden\_size)\).
h_n (Tensor) - Tensor of shape \((num\_directions * num\_layers, batch\_size, real\_hidden\_size)\).
c_n (Tensor) - Tensor of shape \((num\_directions * num\_layers, batch\_size, hidden\_size)\).
reserve (Tensor) - Tensor of shape \((r, 1)\).
state (Tensor) - Random number generator state and its shape is \((s, 1)\).
- Raises
TypeError – If input_size, hidden_size or num_layers is not an int.
TypeError – If has_bias or bidirectional is not a bool.
TypeError – If dropout is not a float.
ValueError – If dropout is not in range [0.0, 1.0].
ValueError – If proj_size is not in range [0, hidden_size).
- Supported Platforms:
GPU
CPU
Examples
>>> import numpy as np >>> from mindspore import Tensor, ops >>> input_size = 10 >>> hidden_size = 2 >>> num_layers = 1 >>> seq_len = 5 >>> batch_size = 2 >>> >>> net = ops.LSTM(input_size, hidden_size, num_layers, True, False, 0.0) >>> input_tensor = Tensor(np.ones([seq_len, batch_size, input_size]).astype(np.float32)) >>> h0 = Tensor(np.ones([num_layers, batch_size, hidden_size]).astype(np.float32)) >>> c0 = Tensor(np.ones([num_layers, batch_size, hidden_size]).astype(np.float32)) >>> w = Tensor(np.ones([112, 1, 1]).astype(np.float32)) >>> output, hn, cn, _, _ = net(input_tensor, h0, c0, w) >>> print(output) [[[0.9640267 0.9640267 ] [0.9640267 0.9640267 ]] [[0.9950539 0.9950539 ] [0.9950539 0.9950539 ]] [[0.99932843 0.99932843] [0.99932843 0.99932843]] [[0.9999084 0.9999084 ] [0.9999084 0.9999084 ]] [[0.9999869 0.9999869 ] [0.9999869 0.9999869 ]]]