Previous Approaches for NLP:
1. Recurrent Neural Networks

A Recurrent Neural Network (RNN) processes a sequence one token at a time while maintaining a hidden state that acts a summary of the preceding context.
For a token sequence the basics RNN recurrence is:
: representation of the current token
: hidden state at time
: input to hidden weight matrix
: recurrent/hidden-to-hidden weight matrix
: nonlinear activation function (tanh,ReLU etc.)
For language modeling, we map the hidden vocabulary to a distribution over the vocabulary:
The same recurrent parameters are reused at every time step. This is what allows the model to process sequence of arbitrary length.
Problems with RNNs:
-
Vanishing Gradient
During backpropagation, the gradient from a later time step has to propagate through many recurrent transformations which makes it very difficult for the information from distant tokens/words to influence the parameters through gradient updates. Due to this model weights are updated with only near/local relations and not the long-term ones.
-
Exploding Gradient
If the recurrent Jacobians have magnitude greater than 1 then repeated multiplication can then make the gradients grow rapidly: . This can be fixed with gradient clipping
-
Long-distance Dependencies
An RNN has a theoretical recurrent memory extending over the entire sequence - Its ability to use distant context is limited because all previous information must be compressed into a fixed-dimensional state and the learning signal must pass through many recurrent steps.
-
Lack of Parallelizability
Because we cannot directly compute it unless we know and hence we have dependency: which can not be computed in parallel rather only sequentially.
2. Long Short-Term Memory (LSTM)
LSTM is a recurrent neural network architecture primarily addressed to overcome the vanishing-gradient problem of vanilla RNNs and to improve performance on long-term dependencies.
Modification in LSTM is that it maintains 2 states: and
The cell/memory state provides a relatively direct path through time while gates control what information is forgotten, added and exposed.
(not going in depth for now - [TODO])
Attention
Bahdanu Attention
[TODO]
Self-Attention
It is called self-attention because the queries, keys and values originate from the same sequence.
Query - What kind of information am I (token) looking for from the other tokens?
Key - What kind of information I (token) can provide
Value - The actual information retrieved
We use Query and Key to find the relevance between two tokens, and based on that relevancy score we do weighted sum of the value vectors for that token.
and is the dimensionality of the key/query vectors
measures how strongly token query relates to token key.
-th row of gives us the relevancy of each token w.r.t -th token.
We apply over the calculated attention scores before multiplying them with : Output for 1 token: where
Question
Why divide by ?
Assuming components of and are independent and have: and then a single product will be: with and
Now the dot product is: For independent terms the will add up and therefore , hence its standard deviation:Dividing by the standard deviation makes the logits independent of the key/query dimension.
Also, large scores makes distribution of attention extremely peaked and a only few (or even just one) token might get the whole score which may lead to small gradient updates and unstable optimization. (softmax saturation)
Problems with Self-Attention:
- No word position- self-attention is positional invariant (add positional encoding - Positional Embedding)
- No Non-linearity (add FFN - FFNN)
- Looks at future words (add Causal Masking - Decoder)
Transformer

Gains:
- Parallel Processing
- Semi-supervised Learning
- Transfer Learning
- Knowledge-stores
- Reasoning
Drawbacks:
- Quadratic computation in self-attention
- Position Representation
