mindspore.ops.LSTM

class mindspore.ops.LSTM(input_size, hidden_size, num_layers, has_bias, bidirectional, dropout)[source]

Performs the Long Short-Term Memory (LSTM) on the input.

For detailed 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.

  • has_bias (bool) – Whether the cell has bias b_ih and b_hh.

  • bidirectional (bool) – Specifies whether it is a bidirectional LSTM.

  • 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].

Inputs:
  • input (Tensor) - Tensor of shape (seq_len, batch_size, input_size) or (batch_size, seq_len, input_size).

  • h (tuple) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

  • c (tuple) - Tensor of shape (num_directions * num_layers, batch_size, hidden_size).

  • w (Tensor) - A weight Tensor.

Outputs:

Tuple, a tuple contains (output, h_n, c_n, reserve, state).

  • output (Tensor) - Tensor of shape (seq_len, batch_size, num_directions * hidden_size).

  • h_n (Tensor) - Tensor of shape (num_directions * num_layers, batch_size, 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].

Supported Platforms:

GPU CPU

Examples

>>> 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 ]]]