# LSTM#

## LSTM - 14#

Version

• name: LSTM (GitHub)

• domain: main

• since_version: 14

• function: False

• support_level: SupportType.COMMON

• shape inference: True

This version of the operator has been available since version 14.

Summary

Computes an one-layer LSTM. This operator is usually supported via some custom implementation such as CuDNN.

Notations:

X - input tensor

i - input gate

o - output gate

f - forget gate

c - cell gate

t - time step (t-1 means previous time step)

W[iofc] - W parameter weight matrix for input, output, forget, and cell gates

R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates

Wb[iofc] - W bias vectors for input, output, forget, and cell gates

Rb[iofc] - R bias vectors for input, output, forget, and cell gates

P[iof] - P peephole weight vector for input, output, and forget gates

WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates

RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates

WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates

RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates

PB[iof] - P peephole weight vector for backward input, output, and forget gates

H - Hidden state

num_directions - 2 if direction == bidirectional else 1

Activation functions:

Relu(x) - max(0, x)

Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})

Sigmoid(x) - 1/(1 + e^{-x})

(NOTE: Below are optional)

Affine(x) - alpha*x + beta

LeakyRelu(x) - x if x >= 0 else alpha * x

ThresholdedRelu(x) - x if x >= alpha else 0

ScaledTanh(x) - alpha*Tanh(beta*x)

HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)

Elu(x) - x if x >= 0 else alpha*(e^x - 1)

Softsign(x) - x/(1 + |x|)

Softplus(x) - log(1 + e^x)

Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):

• it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)

• ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)

• ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)

• Ct = ft (.) Ct-1 + it (.) ct

• ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)

• Ht = ot (.) h(Ct)

This operator has optional inputs/outputs. See ONNX for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument’s name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.

Attributes

• activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.

• activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.

• activations: A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.

• clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.

• direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is `'forward'`.

• hidden_size: Number of neurons in the hidden layer

• input_forget: Couple the input and forget gates if 1. Default value is `0`.

• layout: The shape format of inputs X, initial_h, initial_c and outputs Y, Y_h, Y_c. If 0, the following shapes are expected: X.shape = [seq_length, batch_size, input_size], Y.shape = [seq_length, num_directions, batch_size, hidden_size], initial_h.shape = Y_h.shape = initial_c.shape = Y_c.shape = [num_directions, batch_size, hidden_size]. If 1, the following shapes are expected: X.shape = [batch_size, seq_length, input_size], Y.shape = [batch_size, seq_length, num_directions, hidden_size], initial_h.shape = Y_h.shape = initial_c.shape = Y_c.shape = [batch_size, num_directions, hidden_size]. Default value is `0`.

Inputs

Between 3 and 8 inputs.

• X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].

• W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[iofc] and WB[iofc] (if bidirectional) along dimension 0. The tensor has shape [num_directions, 4*hidden_size, input_size].

• R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[iofc] and RB[iofc] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 4*hidden_size, hidden_size].

• B (optional, heterogeneous) - T: The bias tensor for input gate. Concatenation of [Wb[iofc], Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 8*hidden_size]. Optional: If not specified - assumed to be 0.

• sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].

• initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• initial_c (optional, heterogeneous) - T: Optional initial value of the cell. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• P (optional, heterogeneous) - T: The weight tensor for peepholes. Concatenation of P[iof] and PB[iof] (if bidirectional) along dimension 0. It has shape [num_directions, 3*hidde_size]. Optional: If not specified - assumed to be 0.

Outputs

Between 0 and 3 outputs.

• Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size].

• Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].

• Y_c (optional, heterogeneous) - T: The last output value of the cell. It has shape [num_directions, batch_size, hidden_size].

Type Constraints

• T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.

• T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.

Examples

_defaults

```input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).astype(np.float32)

input_size = 2
hidden_size = 3
weight_scale = 0.1
number_of_gates = 4

node = onnx.helper.make_node(
"LSTM", inputs=["X", "W", "R"], outputs=["", "Y_h"], hidden_size=hidden_size
)

W = weight_scale * np.ones(
(1, number_of_gates * hidden_size, input_size)
).astype(np.float32)
R = weight_scale * np.ones(
(1, number_of_gates * hidden_size, hidden_size)
).astype(np.float32)

lstm = LSTM_Helper(X=input, W=W, R=R)
_, Y_h = lstm.step()
expect(
node,
inputs=[input, W, R],
outputs=[Y_h.astype(np.float32)],
name="test_lstm_defaults",
)
```

_initial_bias

```input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(
np.float32
)

input_size = 3
hidden_size = 4
weight_scale = 0.1
custom_bias = 0.1
number_of_gates = 4

node = onnx.helper.make_node(
"LSTM",
inputs=["X", "W", "R", "B"],
outputs=["", "Y_h"],
hidden_size=hidden_size,
)

W = weight_scale * np.ones(
(1, number_of_gates * hidden_size, input_size)
).astype(np.float32)
R = weight_scale * np.ones(
(1, number_of_gates * hidden_size, hidden_size)
).astype(np.float32)

W_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(
np.float32
)
R_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)
B = np.concatenate((W_B, R_B), 1)

lstm = LSTM_Helper(X=input, W=W, R=R, B=B)
_, Y_h = lstm.step()
expect(
node,
inputs=[input, W, R, B],
outputs=[Y_h.astype(np.float32)],
name="test_lstm_with_initial_bias",
)
```

_peepholes

```input = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]).astype(
np.float32
)

input_size = 4
hidden_size = 3
weight_scale = 0.1
number_of_gates = 4
number_of_peepholes = 3

node = onnx.helper.make_node(
"LSTM",
inputs=["X", "W", "R", "B", "sequence_lens", "initial_h", "initial_c", "P"],
outputs=["", "Y_h"],
hidden_size=hidden_size,
)

# Initializing Inputs
W = weight_scale * np.ones(
(1, number_of_gates * hidden_size, input_size)
).astype(np.float32)
R = weight_scale * np.ones(
(1, number_of_gates * hidden_size, hidden_size)
).astype(np.float32)
B = np.zeros((1, 2 * number_of_gates * hidden_size)).astype(np.float32)
seq_lens = np.repeat(input.shape[0], input.shape[1]).astype(np.int32)
init_h = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)
init_c = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)
P = weight_scale * np.ones((1, number_of_peepholes * hidden_size)).astype(
np.float32
)

lstm = LSTM_Helper(
X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h
)
_, Y_h = lstm.step()
expect(
node,
inputs=[input, W, R, B, seq_lens, init_h, init_c, P],
outputs=[Y_h.astype(np.float32)],
name="test_lstm_with_peepholes",
)
```

_batchwise

```input = np.array([[[1.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).astype(np.float32)

input_size = 2
hidden_size = 7
weight_scale = 0.3
number_of_gates = 4
layout = 1

node = onnx.helper.make_node(
"LSTM",
inputs=["X", "W", "R"],
outputs=["Y", "Y_h"],
hidden_size=hidden_size,
layout=layout,
)

W = weight_scale * np.ones(
(1, number_of_gates * hidden_size, input_size)
).astype(np.float32)
R = weight_scale * np.ones(
(1, number_of_gates * hidden_size, hidden_size)
).astype(np.float32)

lstm = LSTM_Helper(X=input, W=W, R=R, layout=layout)
Y, Y_h = lstm.step()
expect(
node,
inputs=[input, W, R],
outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],
name="test_lstm_batchwise",
)
```

Differences

 `0` `0` `Computes an one-layer LSTM. This operator is usually supported via some` `Computes an one-layer LSTM. This operator is usually supported via some` `1` `1` `custom implementation such as CuDNN.` `custom implementation such as CuDNN.` `2` `2` `3` `3` `Notations:` `Notations:` `4` `4` `5` `5` `X - input tensor` `X - input tensor` `6` `6` `7` `7` `i - input gate` `i - input gate` `8` `8` `9` `9` `o - output gate` `o - output gate` `10` `10` `11` `11` `f - forget gate` `f - forget gate` `12` `12` `13` `13` `c - cell gate` `c - cell gate` `14` `14` `15` `15` `t - time step (t-1 means previous time step)` `t - time step (t-1 means previous time step)` `16` `16` `17` `17` `W[iofc] - W parameter weight matrix for input, output, forget, and cell gates` `W[iofc] - W parameter weight matrix for input, output, forget, and cell gates` `18` `18` `19` `19` `R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates` `R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates` `20` `20` `21` `21` `Wb[iofc] - W bias vectors for input, output, forget, and cell gates` `Wb[iofc] - W bias vectors for input, output, forget, and cell gates` `22` `22` `23` `23` `Rb[iofc] - R bias vectors for input, output, forget, and cell gates` `Rb[iofc] - R bias vectors for input, output, forget, and cell gates` `24` `24` `25` `25` `P[iof] - P peephole weight vector for input, output, and forget gates` `P[iof] - P peephole weight vector for input, output, and forget gates` `26` `26` `27` `27` `WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates` `WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates` `28` `28` `29` `29` `RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates` `RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates` `30` `30` `31` `31` `WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates` `WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates` `32` `32` `33` `33` `RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates` `RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates` `34` `34` `35` `35` `PB[iof] - P peephole weight vector for backward input, output, and forget gates` `PB[iof] - P peephole weight vector for backward input, output, and forget gates` `36` `36` `37` `37` `H - Hidden state` `H - Hidden state` `38` `38` `39` `39` `num_directions - 2 if direction == bidirectional else 1` `num_directions - 2 if direction == bidirectional else 1` `40` `40` `41` `41` `Activation functions:` `Activation functions:` `42` `42` `43` `43` ` Relu(x) - max(0, x)` ` Relu(x) - max(0, x)` `44` `44` `45` `45` ` Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})` ` Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})` `46` `46` `47` `47` ` Sigmoid(x) - 1/(1 + e^{-x})` ` Sigmoid(x) - 1/(1 + e^{-x})` `48` `48` `49` `49` ` (NOTE: Below are optional)` ` (NOTE: Below are optional)` `50` `50` `51` `51` ` Affine(x) - alpha*x + beta` ` Affine(x) - alpha*x + beta` `52` `52` `53` `53` ` LeakyRelu(x) - x if x >= 0 else alpha * x` ` LeakyRelu(x) - x if x >= 0 else alpha * x` `54` `54` `55` `55` ` ThresholdedRelu(x) - x if x >= alpha else 0` ` ThresholdedRelu(x) - x if x >= alpha else 0` `56` `56` `57` `57` ` ScaledTanh(x) - alpha*Tanh(beta*x)` ` ScaledTanh(x) - alpha*Tanh(beta*x)` `58` `58` `59` `59` ` HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)` ` HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)` `60` `60` `61` `61` ` Elu(x) - x if x >= 0 else alpha*(e^x - 1)` ` Elu(x) - x if x >= 0 else alpha*(e^x - 1)` `62` `62` `63` `63` ` Softsign(x) - x/(1 + |x|)` ` Softsign(x) - x/(1 + |x|)` `64` `64` `65` `65` ` Softplus(x) - log(1 + e^x)` ` Softplus(x) - log(1 + e^x)` `66` `66` `67` `67` `Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):` `Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):` `68` `68` `69` `69` ` - it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)` ` - it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)` `70` `70` `71` `71` ` - ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)` ` - ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)` `72` `72` `73` `73` ` - ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)` ` - ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)` `74` `74` `75` `75` ` - Ct = ft (.) Ct-1 + it (.) ct` ` - Ct = ft (.) Ct-1 + it (.) ct` `76` `76` `77` `77` ` - ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)` ` - ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)` `78` `78` `79` `79` ` - Ht = ot (.) h(Ct)` ` - Ht = ot (.) h(Ct)` `80` `80` `This operator has **optional** inputs/outputs. See ONNX _ for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.` `This operator has **optional** inputs/outputs. See ONNX _ for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.` `81` `81` `82` `82` `**Attributes**` `**Attributes**` `83` `83` `84` `84` `* **activation_alpha**:` `* **activation_alpha**:` `85` `85` ` Optional scaling values used by some activation functions. The` ` Optional scaling values used by some activation functions. The` `86` `86` ` values are consumed in the order of activation functions, for` ` values are consumed in the order of activation functions, for` `87` `87` ` example (f, g, h) in LSTM. Default values are the same as of` ` example (f, g, h) in LSTM. Default values are the same as of` `88` `88` ` corresponding ONNX operators.For example with LeakyRelu, the default` ` corresponding ONNX operators.For example with LeakyRelu, the default` `89` `89` ` alpha is 0.01.` ` alpha is 0.01.` `90` `90` `* **activation_beta**:` `* **activation_beta**:` `91` `91` ` Optional scaling values used by some activation functions. The` ` Optional scaling values used by some activation functions. The` `92` `92` ` values are consumed in the order of activation functions, for` ` values are consumed in the order of activation functions, for` `93` `93` ` example (f, g, h) in LSTM. Default values are the same as of` ` example (f, g, h) in LSTM. Default values are the same as of` `94` `94` ` corresponding ONNX operators.` ` corresponding ONNX operators.` `95` `95` `* **activations**:` `* **activations**:` `96` `96` ` A list of 3 (or 6 if bidirectional) activation functions for input,` ` A list of 3 (or 6 if bidirectional) activation functions for input,` `97` `97` ` output, forget, cell, and hidden. The activation functions must be` ` output, forget, cell, and hidden. The activation functions must be` `98` `98` ` one of the activation functions specified above. Optional: See the` ` one of the activation functions specified above. Optional: See the` `99` `99` ` equations for default if not specified.` ` equations for default if not specified.` `100` `100` `* **clip**:` `* **clip**:` `101` `101` ` Cell clip threshold. Clipping bounds the elements of a tensor in the` ` Cell clip threshold. Clipping bounds the elements of a tensor in the` `102` `102` ` range of [-threshold, +threshold] and is applied to the input of` ` range of [-threshold, +threshold] and is applied to the input of` `103` `103` ` activations. No clip if not specified.` ` activations. No clip if not specified.` `104` `104` `* **direction**:` `* **direction**:` `105` `105` ` Specify if the RNN is forward, reverse, or bidirectional. Must be` ` Specify if the RNN is forward, reverse, or bidirectional. Must be` `106` `106` ` one of forward (default), reverse, or bidirectional. Default value is 'forward'.` ` one of forward (default), reverse, or bidirectional. Default value is 'forward'.` `107` `107` `* **hidden_size**:` `* **hidden_size**:` `108` `108` ` Number of neurons in the hidden layer` ` Number of neurons in the hidden layer` `109` `109` `* **input_forget**:` `* **input_forget**:` `110` `110` ` Couple the input and forget gates if 1. Default value is 0.` ` Couple the input and forget gates if 1. Default value is 0.` `111` `* **layout**:` `112` ` The shape format of inputs X, initial_h, initial_c and outputs Y,` `113` ` Y_h, Y_c. If 0, the following shapes are expected: X.shape =` `114` ` [seq_length, batch_size, input_size], Y.shape = [seq_length,` `115` ` num_directions, batch_size, hidden_size], initial_h.shape =` `116` ` Y_h.shape = initial_c.shape = Y_c.shape = [num_directions,` `117` ` batch_size, hidden_size]. If 1, the following shapes are expected:` `118` ` X.shape = [batch_size, seq_length, input_size], Y.shape =` `119` ` [batch_size, seq_length, num_directions, hidden_size],` `120` ` initial_h.shape = Y_h.shape = initial_c.shape = Y_c.shape =` `121` ` [batch_size, num_directions, hidden_size]. Default value is 0.` `111` `122` `112` `123` `**Inputs**` `**Inputs**` `113` `124` `114` `125` `Between 3 and 8 inputs.` `Between 3 and 8 inputs.` `115` `126` `116` `127` `* **X** (heterogeneous) - **T**:` `* **X** (heterogeneous) - **T**:` `117` `128` ` The input sequences packed (and potentially padded) into one 3-D` ` The input sequences packed (and potentially padded) into one 3-D` `118` `129` ` tensor with the shape of [seq_length, batch_size, input_size].` ` tensor with the shape of [seq_length, batch_size, input_size].` `119` `130` `* **W** (heterogeneous) - **T**:` `* **W** (heterogeneous) - **T**:` `120` `131` ` The weight tensor for the gates. Concatenation of W[iofc] and` ` The weight tensor for the gates. Concatenation of W[iofc] and` `121` `132` ` WB[iofc] (if bidirectional) along dimension 0. The tensor has` ` WB[iofc] (if bidirectional) along dimension 0. The tensor has` `122` `133` ` shape [num_directions, 4*hidden_size, input_size].` ` shape [num_directions, 4*hidden_size, input_size].` `123` `134` `* **R** (heterogeneous) - **T**:` `* **R** (heterogeneous) - **T**:` `124` `135` ` The recurrence weight tensor. Concatenation of R[iofc] and` ` The recurrence weight tensor. Concatenation of R[iofc] and` `125` `136` ` RB[iofc] (if bidirectional) along dimension 0. This tensor has` ` RB[iofc] (if bidirectional) along dimension 0. This tensor has` `126` `137` ` shape [num_directions, 4*hidden_size, hidden_size].` ` shape [num_directions, 4*hidden_size, hidden_size].` `127` `138` `* **B** (optional, heterogeneous) - **T**:` `* **B** (optional, heterogeneous) - **T**:` `128` `139` ` The bias tensor for input gate. Concatenation of [Wb[iofc],` ` The bias tensor for input gate. Concatenation of [Wb[iofc],` `129` `140` ` Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along` ` Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along` `130` `141` ` dimension 0. This tensor has shape [num_directions,` ` dimension 0. This tensor has shape [num_directions,` `131` `142` ` 8*hidden_size]. Optional: If not specified - assumed to be 0.` ` 8*hidden_size]. Optional: If not specified - assumed to be 0.` `132` `143` `* **sequence_lens** (optional, heterogeneous) - **T1**:` `* **sequence_lens** (optional, heterogeneous) - **T1**:` `133` `144` ` Optional tensor specifying lengths of the sequences in a batch. If` ` Optional tensor specifying lengths of the sequences in a batch. If` `134` `145` ` not specified - assumed all sequences in the batch to have length` ` not specified - assumed all sequences in the batch to have length` `135` `146` ` seq_length. It has shape [batch_size].` ` seq_length. It has shape [batch_size].` `136` `147` `* **initial_h** (optional, heterogeneous) - **T**:` `* **initial_h** (optional, heterogeneous) - **T**:` `137` `148` ` Optional initial value of the hidden. If not specified - assumed to` ` Optional initial value of the hidden. If not specified - assumed to` `138` `149` ` be 0. It has shape [num_directions, batch_size, hidden_size].` ` be 0. It has shape [num_directions, batch_size, hidden_size].` `139` `150` `* **initial_c** (optional, heterogeneous) - **T**:` `* **initial_c** (optional, heterogeneous) - **T**:` `140` `151` ` Optional initial value of the cell. If not specified - assumed to be` ` Optional initial value of the cell. If not specified - assumed to be` `141` `152` ` 0. It has shape [num_directions, batch_size, hidden_size].` ` 0. It has shape [num_directions, batch_size, hidden_size].` `142` `153` `* **P** (optional, heterogeneous) - **T**:` `* **P** (optional, heterogeneous) - **T**:` `143` `154` ` The weight tensor for peepholes. Concatenation of P[iof] and` ` The weight tensor for peepholes. Concatenation of P[iof] and` `144` `155` ` PB[iof] (if bidirectional) along dimension 0. It has shape` ` PB[iof] (if bidirectional) along dimension 0. It has shape` `145` `156` ` [num_directions, 3*hidde_size]. Optional: If not specified -` ` [num_directions, 3*hidde_size]. Optional: If not specified -` `146` `157` ` assumed to be 0.` ` assumed to be 0.` `147` `158` `148` `159` `**Outputs**` `**Outputs**` `149` `160` `150` `161` `Between 0 and 3 outputs.` `Between 0 and 3 outputs.` `151` `162` `152` `163` `* **Y** (optional, heterogeneous) - **T**:` `* **Y** (optional, heterogeneous) - **T**:` `153` `164` ` A tensor that concats all the intermediate output values of the` ` A tensor that concats all the intermediate output values of the` `154` `165` ` hidden. It has shape [seq_length, num_directions, batch_size,` ` hidden. It has shape [seq_length, num_directions, batch_size,` `155` `166` ` hidden_size].` ` hidden_size].` `156` `167` `* **Y_h** (optional, heterogeneous) - **T**:` `* **Y_h** (optional, heterogeneous) - **T**:` `157` `168` ` The last output value of the hidden. It has shape [num_directions,` ` The last output value of the hidden. It has shape [num_directions,` `158` `169` ` batch_size, hidden_size].` ` batch_size, hidden_size].` `159` `170` `* **Y_c** (optional, heterogeneous) - **T**:` `* **Y_c** (optional, heterogeneous) - **T**:` `160` `171` ` The last output value of the cell. It has shape [num_directions,` ` The last output value of the cell. It has shape [num_directions,` `161` `172` ` batch_size, hidden_size].` ` batch_size, hidden_size].` `162` `173` `163` `174` `**Type Constraints**` `**Type Constraints**` `164` `175` `165` `176` `* **T** in (` `* **T** in (` `166` `177` ` tensor(double),` ` tensor(double),` `167` `178` ` tensor(float),` ` tensor(float),` `168` `179` ` tensor(float16)` ` tensor(float16)` `169` `180` ` ):` ` ):` `170` `181` ` Constrain input and output types to float tensors.` ` Constrain input and output types to float tensors.` `171` `182` `* **T1** in (` `* **T1** in (` `172` `183` ` tensor(int32)` ` tensor(int32)` `173` `184` ` ):` ` ):` `174` `185` ` Constrain seq_lens to integer tensor.` ` Constrain seq_lens to integer tensor.`

## LSTM - 7#

Version

• name: LSTM (GitHub)

• domain: main

• since_version: 7

• function: False

• support_level: SupportType.COMMON

• shape inference: True

This version of the operator has been available since version 7.

Summary

Computes an one-layer LSTM. This operator is usually supported via some custom implementation such as CuDNN.

Notations:

X - input tensor

i - input gate

o - output gate

f - forget gate

c - cell gate

t - time step (t-1 means previous time step)

W[iofc] - W parameter weight matrix for input, output, forget, and cell gates

R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates

Wb[iofc] - W bias vectors for input, output, forget, and cell gates

Rb[iofc] - R bias vectors for input, output, forget, and cell gates

P[iof] - P peephole weight vector for input, output, and forget gates

WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates

RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates

WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates

RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates

PB[iof] - P peephole weight vector for backward input, output, and forget gates

H - Hidden state

num_directions - 2 if direction == bidirectional else 1

Activation functions:

Relu(x) - max(0, x)

Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})

Sigmoid(x) - 1/(1 + e^{-x})

(NOTE: Below are optional)

Affine(x) - alpha*x + beta

LeakyRelu(x) - x if x >= 0 else alpha * x

ThresholdedRelu(x) - x if x >= alpha else 0

ScaledTanh(x) - alpha*Tanh(beta*x)

HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)

Elu(x) - x if x >= 0 else alpha*(e^x - 1)

Softsign(x) - x/(1 + |x|)

Softplus(x) - log(1 + e^x)

Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):

• it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)

• ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)

• ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)

• Ct = ft (.) Ct-1 + it (.) ct

• ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)

• Ht = ot (.) h(Ct)

This operator has optional inputs/outputs. See ONNX for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument’s name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.

Attributes

• activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.

• activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.

• activations: A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.

• clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.

• direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is `'forward'`.

• hidden_size: Number of neurons in the hidden layer

• input_forget: Couple the input and forget gates if 1. Default value is `0`.

Inputs

Between 3 and 8 inputs.

• X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].

• W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[iofc] and WB[iofc] (if bidirectional) along dimension 0. The tensor has shape [num_directions, 4*hidden_size, input_size].

• R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[iofc] and RB[iofc] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 4*hidden_size, hidden_size].

• B (optional, heterogeneous) - T: The bias tensor for input gate. Concatenation of [Wb[iofc], Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 8*hidden_size]. Optional: If not specified - assumed to be 0.

• sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].

• initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• initial_c (optional, heterogeneous) - T: Optional initial value of the cell. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• P (optional, heterogeneous) - T: The weight tensor for peepholes. Concatenation of P[iof] and PB[iof] (if bidirectional) along dimension 0. It has shape [num_directions, 3*hidde_size]. Optional: If not specified - assumed to be 0.

Outputs

Between 0 and 3 outputs.

• Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size].

• Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].

• Y_c (optional, heterogeneous) - T: The last output value of the cell. It has shape [num_directions, batch_size, hidden_size].

Type Constraints

• T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.

• T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.

Differences

 `0` `0` `Computes an one-layer LSTM. This operator is usually supported via some` `Computes an one-layer LSTM. This operator is usually supported via some` `1` `1` `custom implementation such as CuDNN.` `custom implementation such as CuDNN.` `2` `2` `3` `3` `Notations:` `Notations:` `4` `4` `5` `5` `X - input tensor` `X - input tensor` `6` `6` `7` `7` `i - input gate` `i - input gate` `8` `8` `9` `9` `o - output gate` `o - output gate` `10` `10` `11` `11` `f - forget gate` `f - forget gate` `12` `12` `13` `13` `c - cell gate` `c - cell gate` `14` `14` `15` `15` `t - time step (t-1 means previous time step)` `t - time step (t-1 means previous time step)` `16` `16` `17` `17` `W[iofc] - W parameter weight matrix for input, output, forget, and cell gates` `W[iofc] - W parameter weight matrix for input, output, forget, and cell gates` `18` `18` `19` `19` `R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates` `R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates` `20` `20` `21` `21` `Wb[iofc] - W bias vectors for input, output, forget, and cell gates` `Wb[iofc] - W bias vectors for input, output, forget, and cell gates` `22` `22` `23` `23` `Rb[iofc] - R bias vectors for input, output, forget, and cell gates` `Rb[iofc] - R bias vectors for input, output, forget, and cell gates` `24` `24` `25` `25` `P[iof] - P peephole weight vector for input, output, and forget gates` `P[iof] - P peephole weight vector for input, output, and forget gates` `26` `26` `27` `27` `WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates` `WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates` `28` `28` `29` `29` `RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates` `RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates` `30` `30` `31` `31` `WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates` `WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates` `32` `32` `33` `33` `RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates` `RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates` `34` `34` `35` `35` `PB[iof] - P peephole weight vector for backward input, output, and forget gates` `PB[iof] - P peephole weight vector for backward input, output, and forget gates` `36` `36` `37` `37` `H - Hidden state` `H - Hidden state` `38` `38` `39` `39` `num_directions - 2 if direction == bidirectional else 1` `num_directions - 2 if direction == bidirectional else 1` `40` `40` `41` `41` `Activation functions:` `Activation functions:` `42` `42` `43` `43` ` Relu(x) - max(0, x)` ` Relu(x) - max(0, x)` `44` `44` `45` `45` ` Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})` ` Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})` `46` `46` `47` `47` ` Sigmoid(x) - 1/(1 + e^{-x})` ` Sigmoid(x) - 1/(1 + e^{-x})` `48` `48` `49` `49` ` (NOTE: Below are optional)` ` (NOTE: Below are optional)` `50` `50` `51` `51` ` Affine(x) - alpha*x + beta` ` Affine(x) - alpha*x + beta` `52` `52` `53` `53` ` LeakyRelu(x) - x if x >= 0 else alpha * x` ` LeakyRelu(x) - x if x >= 0 else alpha * x` `54` `54` `55` `55` ` ThresholdedRelu(x) - x if x >= alpha else 0` ` ThresholdedRelu(x) - x if x >= alpha else 0` `56` `56` `57` `57` ` ScaledTanh(x) - alpha*Tanh(beta*x)` ` ScaledTanh(x) - alpha*Tanh(beta*x)` `58` `58` `59` `59` ` HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)` ` HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)` `60` `60` `61` `61` ` Elu(x) - x if x >= 0 else alpha*(e^x - 1)` ` Elu(x) - x if x >= 0 else alpha*(e^x - 1)` `62` `62` `63` `63` ` Softsign(x) - x/(1 + |x|)` ` Softsign(x) - x/(1 + |x|)` `64` `64` `65` `65` ` Softplus(x) - log(1 + e^x)` ` Softplus(x) - log(1 + e^x)` `66` `66` `67` `67` `Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):` `Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):` `68` `68` `69` `69` ` - it = f(Xt*(Wi^T) + Ht-1*Ri + Pi (.) Ct-1 + Wbi + Rbi)` ` - it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)` `70` `70` `71` `71` ` - ft = f(Xt*(Wf^T) + Ht-1*Rf + Pf (.) Ct-1 + Wbf + Rbf)` ` - ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)` `72` `72` `73` `73` ` - ct = g(Xt*(Wc^T) + Ht-1*Rc + Wbc + Rbc)` ` - ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)` `74` `74` `75` `75` ` - Ct = ft (.) Ct-1 + it (.) ct` ` - Ct = ft (.) Ct-1 + it (.) ct` `76` `76` `77` `77` ` - ot = f(Xt*(Wo^T) + Ht-1*Ro + Po (.) Ct + Wbo + Rbo)` ` - ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)` `78` `78` `79` ` - Ht = ot (.) h(Ct)` `79` `80` ` - Ht = ot (.) h(Ct)` `This operator has **optional** inputs/outputs. See ONNX _ for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.` `80` `81` `81` `82` `**Attributes**` `**Attributes**` `82` `83` `83` `84` `* **activation_alpha**:` `* **activation_alpha**:` `84` `85` ` Optional scaling values used by some activation functions. The` ` Optional scaling values used by some activation functions. The` `85` `86` ` values are consumed in the order of activation functions, for` ` values are consumed in the order of activation functions, for` `86` `87` ` example (f, g, h) in LSTM. Default values are the same as of` ` example (f, g, h) in LSTM. Default values are the same as of` `87` `88` ` corresponding ONNX operators.For example with LeakyRelu, the default` ` corresponding ONNX operators.For example with LeakyRelu, the default` `88` `89` ` alpha is 0.01.` ` alpha is 0.01.` `89` `90` `* **activation_beta**:` `* **activation_beta**:` `90` `91` ` Optional scaling values used by some activation functions. The` ` Optional scaling values used by some activation functions. The` `91` `92` ` values are consumed in the order of activation functions, for` ` values are consumed in the order of activation functions, for` `92` `93` ` example (f, g, h) in LSTM. Default values are the same as of` ` example (f, g, h) in LSTM. Default values are the same as of` `93` `94` ` corresponding ONNX operators.` ` corresponding ONNX operators.` `94` `95` `* **activations**:` `* **activations**:` `95` `96` ` A list of 3 (or 6 if bidirectional) activation functions for input,` ` A list of 3 (or 6 if bidirectional) activation functions for input,` `96` `97` ` output, forget, cell, and hidden. The activation functions must be` ` output, forget, cell, and hidden. The activation functions must be` `97` `98` ` one of the activation functions specified above. Optional: See the` ` one of the activation functions specified above. Optional: See the` `98` `99` ` equations for default if not specified.` ` equations for default if not specified.` `99` `100` `* **clip**:` `* **clip**:` `100` `101` ` Cell clip threshold. Clipping bounds the elements of a tensor in the` ` Cell clip threshold. Clipping bounds the elements of a tensor in the` `101` `102` ` range of [-threshold, +threshold] and is applied to the input of` ` range of [-threshold, +threshold] and is applied to the input of` `102` `103` ` activations. No clip if not specified.` ` activations. No clip if not specified.` `103` `104` `* **direction**:` `* **direction**:` `104` `105` ` Specify if the RNN is forward, reverse, or bidirectional. Must be` ` Specify if the RNN is forward, reverse, or bidirectional. Must be` `105` `106` ` one of forward (default), reverse, or bidirectional. Default value is 'forward'.` ` one of forward (default), reverse, or bidirectional. Default value is 'forward'.` `106` `107` `* **hidden_size**:` `* **hidden_size**:` `107` `108` ` Number of neurons in the hidden layer` ` Number of neurons in the hidden layer` `108` `109` `* **input_forget**:` `* **input_forget**:` `109` `110` ` Couple the input and forget gates if 1, default 0. Default value is 0.` ` Couple the input and forget gates if 1. Default value is 0.` `110` `* **output_sequence**:` `111` ` The sequence output for the hidden is optional if 0. Default 0. Default value is 0.` `112` `111` `113` `112` `**Inputs**` `**Inputs**` `114` `113` `115` `114` `Between 3 and 8 inputs.` `Between 3 and 8 inputs.` `116` `115` `117` `116` `* **X** (heterogeneous) - **T**:` `* **X** (heterogeneous) - **T**:` `118` `117` ` The input sequences packed (and potentially padded) into one 3-D` ` The input sequences packed (and potentially padded) into one 3-D` `119` `118` ` tensor with the shape of [seq_length, batch_size, input_size].` ` tensor with the shape of [seq_length, batch_size, input_size].` `120` `119` `* **W** (heterogeneous) - **T**:` `* **W** (heterogeneous) - **T**:` `121` `120` ` The weight tensor for the gates. Concatenation of W[iofc] and` ` The weight tensor for the gates. Concatenation of W[iofc] and` `122` `121` ` WB[iofc] (if bidirectional) along dimension 0. The tensor has` ` WB[iofc] (if bidirectional) along dimension 0. The tensor has` `123` `122` ` shape [num_directions, 4*hidden_size, input_size].` ` shape [num_directions, 4*hidden_size, input_size].` `124` `123` `* **R** (heterogeneous) - **T**:` `* **R** (heterogeneous) - **T**:` `125` `124` ` The recurrence weight tensor. Concatenation of R[iofc] and` ` The recurrence weight tensor. Concatenation of R[iofc] and` `126` `125` ` RB[iofc] (if bidirectional) along dimension 0. This tensor has` ` RB[iofc] (if bidirectional) along dimension 0. This tensor has` `127` `126` ` shape [num_directions, 4*hidden_size, hidden_size].` ` shape [num_directions, 4*hidden_size, hidden_size].` `128` `127` `* **B** (optional, heterogeneous) - **T**:` `* **B** (optional, heterogeneous) - **T**:` `129` `128` ` The bias tensor for input gate. Concatenation of [Wb[iofc],` ` The bias tensor for input gate. Concatenation of [Wb[iofc],` `130` `129` ` Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along` ` Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along` `131` `130` ` dimension 0. This tensor has shape [num_directions,` ` dimension 0. This tensor has shape [num_directions,` `132` `131` ` 8*hidden_size]. Optional: If not specified - assumed to be 0.` ` 8*hidden_size]. Optional: If not specified - assumed to be 0.` `133` `132` `* **sequence_lens** (optional, heterogeneous) - **T1**:` `* **sequence_lens** (optional, heterogeneous) - **T1**:` `134` `133` ` Optional tensor specifying lengths of the sequences in a batch. If` ` Optional tensor specifying lengths of the sequences in a batch. If` `135` `134` ` not specified - assumed all sequences in the batch to have length` ` not specified - assumed all sequences in the batch to have length` `136` `135` ` seq_length. It has shape [batch_size].` ` seq_length. It has shape [batch_size].` `137` `136` `* **initial_h** (optional, heterogeneous) - **T**:` `* **initial_h** (optional, heterogeneous) - **T**:` `138` `137` ` Optional initial value of the hidden. If not specified - assumed to` ` Optional initial value of the hidden. If not specified - assumed to` `139` `138` ` be 0. It has shape [num_directions, batch_size, hidden_size].` ` be 0. It has shape [num_directions, batch_size, hidden_size].` `140` `139` `* **initial_c** (optional, heterogeneous) - **T**:` `* **initial_c** (optional, heterogeneous) - **T**:` `141` `140` ` Optional initial value of the cell. If not specified - assumed to be` ` Optional initial value of the cell. If not specified - assumed to be` `142` `141` ` 0. It has shape [num_directions, batch_size, hidden_size].` ` 0. It has shape [num_directions, batch_size, hidden_size].` `143` `142` `* **P** (optional, heterogeneous) - **T**:` `* **P** (optional, heterogeneous) - **T**:` `144` `143` ` The weight tensor for peepholes. Concatenation of P[iof] and` ` The weight tensor for peepholes. Concatenation of P[iof] and` `145` `144` ` PB[iof] (if bidirectional) along dimension 0. It has shape` ` PB[iof] (if bidirectional) along dimension 0. It has shape` `146` `145` ` [num_directions, 3*hidde_size]. Optional: If not specified -` ` [num_directions, 3*hidde_size]. Optional: If not specified -` `147` `146` ` assumed to be 0.` ` assumed to be 0.` `148` `147` `149` `148` `**Outputs**` `**Outputs**` `150` `149` `151` `150` `Between 0 and 3 outputs.` `Between 0 and 3 outputs.` `152` `151` `153` `152` `* **Y** (optional, heterogeneous) - **T**:` `* **Y** (optional, heterogeneous) - **T**:` `154` `153` ` A tensor that concats all the intermediate output values of the` ` A tensor that concats all the intermediate output values of the` `155` `154` ` hidden. It has shape [seq_length, num_directions, batch_size,` ` hidden. It has shape [seq_length, num_directions, batch_size,` `156` `155` ` hidden_size]. It is optional if output_sequence is 0.` ` hidden_size].` `157` `156` `* **Y_h** (optional, heterogeneous) - **T**:` `* **Y_h** (optional, heterogeneous) - **T**:` `158` `157` ` The last output value of the hidden. It has shape [num_directions,` ` The last output value of the hidden. It has shape [num_directions,` `159` `158` ` batch_size, hidden_size].` ` batch_size, hidden_size].` `160` `159` `* **Y_c** (optional, heterogeneous) - **T**:` `* **Y_c** (optional, heterogeneous) - **T**:` `161` `160` ` The last output value of the cell. It has shape [num_directions,` ` The last output value of the cell. It has shape [num_directions,` `162` `161` ` batch_size, hidden_size].` ` batch_size, hidden_size].` `163` `162` `164` `163` `**Type Constraints**` `**Type Constraints**` `165` `164` `166` `165` `* **T** in (` `* **T** in (` `167` `166` ` tensor(double),` ` tensor(double),` `168` `167` ` tensor(float),` ` tensor(float),` `169` `168` ` tensor(float16)` ` tensor(float16)` `170` `169` ` ):` ` ):` `171` `170` ` Constrain input and output types to float tensors.` ` Constrain input and output types to float tensors.` `172` `171` `* **T1** in (` `* **T1** in (` `173` `172` ` tensor(int32)` ` tensor(int32)` `174` `173` ` ):` ` ):` `175` `174` ` Constrain seq_lens to integer tensor.` ` Constrain seq_lens to integer tensor.`

## LSTM - 1#

Version

• name: LSTM (GitHub)

• domain: main

• since_version: 1

• function: False

• support_level: SupportType.COMMON

• shape inference: True

This version of the operator has been available since version 1.

Summary

Computes an one-layer LSTM. This operator is usually supported via some custom implementation such as CuDNN.

Notations:

X - input tensor

i - input gate

o - output gate

f - forget gate

c - cell gate

t - time step (t-1 means previous time step)

W[iofc] - W parameter weight matrix for input, output, forget, and cell gates

R[iofc] - R recurrence weight matrix for input, output, forget, and cell gates

Wb[iofc] - W bias vectors for input, output, forget, and cell gates

Rb[iofc] - R bias vectors for input, output, forget, and cell gates

P[iof] - P peephole weight vector for input, output, and forget gates

WB[iofc] - W parameter weight matrix for backward input, output, forget, and cell gates

RB[iofc] - R recurrence weight matrix for backward input, output, forget, and cell gates

WBb[iofc] - W bias vectors for backward input, output, forget, and cell gates

RBb[iofc] - R bias vectors for backward input, output, forget, and cell gates

PB[iof] - P peephole weight vector for backward input, output, and forget gates

H - Hidden state

num_directions - 2 if direction == bidirectional else 1

Activation functions:

Relu(x) - max(0, x)

Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})

Sigmoid(x) - 1/(1 + e^{-x})

(NOTE: Below are optional)

Affine(x) - alpha*x + beta

LeakyRelu(x) - x if x >= 0 else alpha * x

ThresholdedRelu(x) - x if x >= alpha else 0

ScaledTanh(x) - alpha*Tanh(beta*x)

HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)

Elu(x) - x if x >= 0 else alpha*(e^x - 1)

Softsign(x) - x/(1 + |x|)

Softplus(x) - log(1 + e^x)

Equations (Default: f=Sigmoid, g=Tanh, h=Tanh):

• it = f(Xt*(Wi^T) + Ht-1*Ri + Pi (.) Ct-1 + Wbi + Rbi)

• ft = f(Xt*(Wf^T) + Ht-1*Rf + Pf (.) Ct-1 + Wbf + Rbf)

• ct = g(Xt*(Wc^T) + Ht-1*Rc + Wbc + Rbc)

• Ct = ft (.) Ct-1 + it (.) ct

• ot = f(Xt*(Wo^T) + Ht-1*Ro + Po (.) Ct + Wbo + Rbo)

• Ht = ot (.) h(Ct)

Attributes

• activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.

• activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.

• activations: A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.

• clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.

• direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is `'forward'`.

• hidden_size: Number of neurons in the hidden layer

• input_forget: Couple the input and forget gates if 1, default 0. Default value is `0`.

• output_sequence: The sequence output for the hidden is optional if 0. Default 0. Default value is `0`.

Inputs

Between 3 and 8 inputs.

• X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].

• W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[iofc] and WB[iofc] (if bidirectional) along dimension 0. The tensor has shape [num_directions, 4*hidden_size, input_size].

• R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[iofc] and RB[iofc] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 4*hidden_size, hidden_size].

• B (optional, heterogeneous) - T: The bias tensor for input gate. Concatenation of [Wb[iofc], Rb[iofc]], and [WBb[iofc], RBb[iofc]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 8*hidden_size]. Optional: If not specified - assumed to be 0.

• sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].

• initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• initial_c (optional, heterogeneous) - T: Optional initial value of the cell. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].

• P (optional, heterogeneous) - T: The weight tensor for peepholes. Concatenation of P[iof] and PB[iof] (if bidirectional) along dimension 0. It has shape [num_directions, 3*hidde_size]. Optional: If not specified - assumed to be 0.

Outputs

Between 0 and 3 outputs.

• Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size]. It is optional if output_sequence is 0.

• Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].

• Y_c (optional, heterogeneous) - T: The last output value of the cell. It has shape [num_directions, batch_size, hidden_size].

Type Constraints

• T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.

• T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.