GRU(Gated Recurrent Unit) and LSTM(Long Short Term Memory)

Vanishing Gradients

Let’s say we want to train language model. With input ‘The cats, which already ate plenty of delicious biscuits’, we want the model to decide whether ‘were’ or ‘was’ will likely follow the sentence.

To let the model learn this kind of thing, when training, the loss of the layer where ‘were’ or ‘was’ appear should be backpropagated properly to the layer where ‘cats’ appear.

However, because of vanishing gradients problem, the impact of that loss to the weight-update becomes smaller as it backpropagates to ‘cats’. Thus, the relationship between ‘cats’ and ‘were’ is hardly learned by the model when these two words are far apart.

In other words, when the noun(cats) and the verb(were) are far away, the information that ‘cats’ is plural diminishes when it travels to the layer where verb appears.

There are RNN architectures that seek to solve this vanishing gradients problem in RNN. (GRU and LSTM) GRU and LSTM basically adds a ‘highway’ that far-apart units can interact directly.

GRU (Gated Recurrent Unit)

A GRU layer first computes a ‘candidate’($\tilde{c}^{<t>}$) with $c^{<t-1>}$ and $x^{<t>}$. Then, it computes ‘memory cell’ with $c^{<t-1>}$ and $\tilde{c}^{<t>}$. The memory cell becomes $a^{<t>}$.

GRU introduces a concept ‘gate’. There are two gates; gate for computing candidate(remember gate) and gate for computing memory cell(update gate).

If remember gate is 0, impact of previous activation unit to the current candidate is 0 so current candidate is computed with only $x^{<t>}$.

If update gate is 1, candidate units of that layer is directly passed to the next layer and if it is 0, activation of the previous layer is directly passed to the next layer.

GRU layer

  1. Compute remember gate

  2. Compute candiate for computing memory cell

  3. Compute update gate

  4. Compute memory cell

  5. Compute activation unit

LSTM (Long Short Term Memory)

LSTM is an original version of GRU. The difference is that it has three gates.

GRU is computationally cheap, whereas LSTM is more flexible. We use LSTM as default.

LSTM layer

  1. Compute candidate

  2. Compute gate for emphasizing candidate when computing memory cell (‘update gate’)

  3. Compute gate for emphasizing previous memory cell (‘forget gate’)

  4. Compute gate for computing activation (‘output gate’)

  5. Compute memory cell

  6. Compute activation cell

LSTM highway

As you can see in the above image, LSTM can learn update gate to 0 and forget gate to 1 to keep memory cell intact to next layer.

Variation of LSTM: Peephole Connection

We can involve $c^{<t-1>}$ when computing gates like follows;

Leave a Comment