Long short-term memory (LSTM) and gated recurrent unit (GRU) are two types of recurrent neural networks (RNNs) that are commonly used in sequence modeling tasks such as language modeling, speech recognition, and text generation. Both LSTM and GRU networks are designed to address the issue of vanishing gradients in traditional RNNs by introducing gated mechanisms that allow the network to selectively learn and forget information from the past inputs.
The key differences between LSTM and GRU networks lie in their structure and number of gating mechanisms.
**LSTM**
LSTM networks were introduced in 1997 by Hochreiter and Schmidhuber. They have a more complex structure compared to traditional RNNs and consist of three gates: input gate, forget gate, and output gate.
The input gate it determines how much new information flows into the cell state Ct. It is defined as:
itβ=βΟ(Wxixtβ +β Whihtβ ββ 1β +β bi)
where xt is the input at time step t, htβ ββ 1 is the previous hidden state, Wxi, Whi, and bi are the corresponding weights and biases, and Ο is the sigmoid function. The input gate can decide which information to let through, by setting values closer to 1 and 0 for important and irrelevant information, respectively.
The forget gate ft controls how much of the previous cell state Ctβ ββ 1 should be kept or discarded. It is defined as:
ftβ=βΟ(Wxfxtβ +β Whfhtβ ββ 1β +β bf)
where Wxf, Whf, and bf are the corresponding weights and biases. The forget gate can be used to remember long-term dependencies by setting values closer to 1 and short-term dependencies by setting values closer to 0.
The cell state Ct is updated using the input gates and forget gates as:
Ctβ=βftβ *β Ctβ ββ 1β +β itβ *β tanhβ(Wxcxtβ +β Whchtβ ββ 1β +β bc)
where tanhβ is the hyperbolic tangent function.
The output gate ot controls how much information is output from the cell state to the next hidden state ht. It is defined as:
otβ=βΟ(Wxoxtβ +β Whohtβ ββ 1β +β bo)
The final hidden state is calculated as:
htβ=βotβ *β tanhβ(Ct)
**GRU**
GRU networks were introduced in 2014 by Chung et al. They have a simpler structure compared to LSTM networks and consist of two gates: reset gate and update gate.
The reset gate rt determines how much of the past information should be reset to the default state. It is defined as:
rtβ=βΟ(Wxrxtβ +β Whrhtβ ββ 1β +β br)
The update gate zt controls how much of the past information should be used in the current calculation. It is defined as:
ztβ=βΟ(Wxzxtβ +β Whzhtβ ββ 1β +β bz)
The candidate activation hΜt is computed as:
hΜtβ=βtanhβ(Wxhxtβ +β rtβ *β (Whhhtβ ββ 1β +β bh))
The final hidden state ht is computed as:
htβ=β(1β ββ zt)β *β htβ ββ 1β +β ztβ *β hΜt
GRU networks have fewer parameters than LSTM networks and have shown to perform well in several tasks such as machine translation and speech recognition. However, LSTM networks are still preferred in tasks where long-term dependencies need to be modeled more explicitly.
In summary, the main differences between LSTM and GRU networks are their structure and number of gating mechanisms. LSTM networks have three gates (input gate, forget gate, and output gate) while GRU networks have two gates (reset gate and update gate). LSTM networks are preferred in tasks that require modeling long-term dependencies while GRU networks are preferred in tasks that require faster training and fewer parameters.