LSTM

Recurrent · node type · LSTM

A recurrent layer with a separate memory cell and three gates that decide what to keep, write and read.

Also called Long short-term memory, LSTM cell, BiLSTM

Figure 1. LSTM inside Continue a melody, a real model in WeightSpace. The 2 outlined nodes are LSTM; select any node for its shapes and parameters.

What it does

An LSTM reads a sequence one step at a time and carries two vectors forward: a hidden state h and a cell state c. At every step it computes three sigmoid gates from the current input and the previous hidden state. The forget gate scales down parts of the old cell, the input gate decides how much of a new tanh candidate to add, and the output gate decides how much of the cell to expose as the new hidden state. Because the cell is updated by addition rather than repeated multiplication, information and gradients can survive across many steps, which plain recurrent layers struggle with. A bidirectional LSTM runs a second copy from the end of the sequence backward.

Formula

it = σ(Wi xt + Ri ht−1 + bi), ft = σ(Wf xt + Rf ht−1 + bf)ot = σ(Wo xt + Ro ht−1 + bo), gt = tanh(Wg xt + Rg ht−1 + bg)ct = ft ⊙ ct−1 + it ⊙ gtht = ot ⊙ tanh(ct)

Shapes and parameters

Shapes
Input [T, N, I] (time, batch, features); hidden and cell states [D, N, H]; output of every step [T, D, N, H].
Parameters
4 gates × (H × I input weights + H × H recurrent weights + biases). ONNX stores two bias vectors, so 4H × (I + H) + 8H per direction. With I = 38 and H = 512: 1,126,400 + 4,096 = 1,130,496.
Used for
SketchRNN drawing, MelodyRNN music, CRNN text recognition, voice activity detection, and older translation and speech models.

Figure 2. The typed ports of LSTM, in the same format as every model drawing. Letters such as N or C stand for any size.

In WeightSpace

Read-only in WeightSpace today. SketchRNN and MelodyRNN are edited through the dense layer that reads the LSTM’s hidden state, so a bias change there acts at every step of the generated sequence.