samyyc.dev
LOC::Macau, China

..UTC+8

samyyc.dev

-- BPM

Thu Jul 11 2024

What? Thinking about LSTM?

#110

/

samyyc

/

697 words

/

Updated 876 days ago

/

[中文]

At time t, an LSTM has three inputs (all vectors):

  • x_t (the input at the current time step)
  • h_{t-1} (the output from the previous time step)
  • c_{t-1} (the cell state from the previous time step, the important part of LSTM)

Two outputs:

  • h_t (the output at the current time step)
  • c_t (the cell state at the current time step)

Gates

A gate is essentially a fully connected layer: it takes a vector as input and outputs a real-valued vector in the range (0,1).

Roughly, it can be written as: g(x) = σ(Wx+b)

The job of a gate is roughly to control how much of the input vector can get through. The way it's done is by taking the dot product of the gate output vector and the original input vector. If the gate output vector is close to 0, less of the original input vector gets through; if it's close to 1, more gets through. That's why it's called a gate.

In the introduction to the three gates below, you'll see that every gate in LSTM is obtained by applying a linear transformation to the vector formed by concatenating h_{t-1} and x_t.

  • The forget gate controls how much of c_{t-1} is kept in c_t
  • The input gate controls how much of x_t is kept in c_t
  • The output gate controls how much of c_t is kept in h_t

Forget gate

The forget gate controls how much of c_{t-1} is kept in c_t.

Math expression: f_t = σ(W_f·Concat[h_{t-1}, x_t] + b_f). Note that f_t here is the forget gate; it's a vector, and after sigmoid activation its range is limited to (0,1).

Suppose the input dimension is d_x, the hidden layer dimension is d_h, and the cell state dimension is d_c (usually d_c = d_h, **written as **d_h below). Then the dimension of W_f is d_h × (d_h + d_x) (because the forget gate controls c_{t-1}, its dimension matches the cell state dimension).

But in fact W_f is made up of two matrices, which are fully connected separately and then concatenated:

W_{fx} = Matmul((d_h × d_x), (d_x × 1)) = (d_h × 1)

Input gate

The input gate controls how much of x_t is kept in c_t.

Calculation of the input gate: i_t = σ(W_i·Concat[h_{t-1},x_t]+b_i). Here i_t is the input gate.

Dimension of i_t: d_h × 1 (vector)

Next, compute the cell state for the current input, \widetilde{c}_t:

\widetilde{c}t = tanh(W_c·Concat[h{t-1}, x_t] + b_c)

Dimension of \widetilde{c}_t: d_h × 1 (vector)

This is computed purely from the current input and the previous layer's output.

Next, compute the current cell state c_t:

c_t = f_t ∘ c_{t-1} + i_t ∘ \widetilde{c}_t

Dimension of c_t: d_h × 1 (vector)

This is where the dot product between the gate vector and the original vector comes in.

At this point, the current cell state c_t has been computed.

Output gate

The output gate controls how much of c_t is kept in h_t.

o_t = σ(W_o · Concat[h_{t-1}, x_t] + b_o)

Dimension of o_t: d_h × 1 (vector)

Compute the final output h_t:

h_t = o_t ∘ tanh(c_t)

I don't even know what subtitle to give this part

c_t = f_t ∘ c_{t-1} + i_t ∘ \widetilde{c}_t

From this equation you can see that the cell state is essentially the previous cell state after passing through the forget gate, plus the input cell state. So the overall change won't be too big, which also fits the long-term nature of the cell state.

By contrast, h_t changes much more.

Tying it back to practice (Pytorch)

A few PyTorch parameters in practice:

  • input_size, i.e. the number of features in the input data, d_x above
  • hidden_size, i.e. the hidden layer dimension, d_h above. Note this only refers to the dimension of the LSTM output, not the final output. In practice you can add a fully connected layer or something after it.
  • num_layers, i.e. the number of recurrent neural network layers (stacked vertically), the hidden output vector of the previous layer corresponds to the input vector of the next layer

I stole a diagram; it might make things a bit more intuitive: