Home/AI/ML Notes/Deep Learning and Machine Learning/RNN, LSTM, GRU & Beam Search
⌂ Main Page
Deep Learning and Machine Learning

RNN, LSTM, GRU & Beam Search

Recurrent architectures, gated units, and beam-search decoding.

On this page

RNN - information cycles through a loop, network remembers output from previous time step(s). RNNs have two common issues: vanishing or exploding gradients during backpropagation, making it difficult to train model.

A diagram of a network AI-generated content may be incorrect.

Used for sequence labeling (POS), NER (IOB), classification w/softmax. Stacked & bidirectional RNNs

LSTM

A diagram of a computer process Description automatically generated

LSTMs - used for seq2seq tasks (translation, text generation, etc.), are more efficient than RNNs at handling sequence data, and are designed to address the vanishing gradient problem of RNNs by using a series of gates to retain relevant and forget irrelevant info which remember long-term dependencies in data and control the flow of info across time steps:

RNNs and LSTMs struggle with capturing long-range dependencies in input data due to their sequential nature - makes it difficult to learn complex patterns over long sequences, especially significantly long ones.

Gated recurrent units (GRUs)

Simplify calc by dispensing separate context vector, and reducing gates to 2 - reset & update gate.

Additional reading on LLM Fundamentals

Source: https://cameronrwolfe.substack.com/p/llama-2-from-the-ground-up

Beam search
Used for seq2seq models like RNNs or LSTMs, especially in translation; based on conditional probabilities

Example: breadth-first-search w/heuristic filter. A variant of Viterbi decoding that maintains only a fraction of high scoring states rather than all states during decoding - prunes the search space to stay within a fixed-size memory footprint

Beam search considers multiple best options based on beamwidth using conditional probability, which is better than the Greedy search (considers only one best option).

Illustrative example:

Seq2seq model for machine translation. “Voy a visitar mis padres” => translation

We will select the beam width =3; our vocabulary of English words is 10,000.

Step 1: Find the top 3 words with the highest probability given the input sentence: decoder applies softmax f(x) to all 10,000 words in vocab and selects top 3 most probable words (I, My, We), store them in memory.

Step 2: Find the three best pairs for the first and second words based on conditional probability.

Take the first word “I”, apply the softmax to all 10,000 words in vocabulary. Same for the other two words: “My: and “We”

Run 30,000 different combinations to choose the top 3 pairs of the first and the second words => “I am”, “My parents”, “I will” (dropped “We” – no bigram with high probability there)

Step 3. Find the three best pairs for the first, second and third word based on the input sentence and the chosen first and the second word and so on
Finally, pick three sentences with the highest probability. We finally pick the output of the decoder as the sentence with the highest probability
A higher beam width will give a better translation, but would use a lot more memory and computational power