CS 162: Natural Language Processing
Saadia Gabriel
Lecture 7:
Language Modeling Part 4
Announcements
Quiz #2
https://forms.gle/PCgrGS1jizC9LELQ6
Demo Recap
Part 1: Build a bigram model
Demo Recap
Part 1: Build a bigram model
(1) What’s the probability of “I am Sam”?
(2) What’s the probability of “Sam am I do”?
.111
0
Demo Recap
Part 2: Shakespeare model
Demo Recap
Part 3: Curse of zero probabilities
Demo Recap
Part 3: Curse of zero probabilities
Demo Recap
Part 4: Perplexity
Demo Recap
Full demo solution is shared here:
https://drive.google.com/file/d/1ZN6q1T5cI733XCAyySdImRjCv-TpQ0ly/view?usp=sharing
Q&A
No, because the inputs change! A decoder RNN takes the previous step’s output and the previous hidden state as the new inputs.
Sometimes they are still used, e.g. RNNs for long sequences. But transformers are standard in many cases.
Q&A
One of today’s topics: the transformer!
We wouldn’t normally see a feed-forward NN on its own, but they are still used today as layers within transformer models!
Last time:
We started discussing RNNs
Today we talk about adding attention mechanisms, these are central to transformers!
A specific kind of RNN design, the long-short term memory model or LSTM
Example of RNN classifier (courtesy of MDPI)
Today’s Goals (according to Gemini):
Today’s Goals:
RNN Recap
The 3 Gates in a LSTM:
3. Output gate
2. Forget gate
1. Input gate
Encoder-Decoder Architectures
Example from Dive into Deep Learning
Encoder calculations
Use final hidden state from encoder to initialize decoder hidden states
Decoder calculations
Vanishing Gradients are still a problem…
Enter the Attention Mechanism
Slide courtesy of Mohit Iyyer
Seq2Seq with Attention
Basics of Attention
Slide courtesy of Mohit Iyyer
In practice, we scale attention weights to stabilize gradients during training
Basics of Attention
Slide courtesy of Mohit Iyyer
Basics of Attention
Slide courtesy of Mohit Iyyer
t=2
Basics of Attention
Slide courtesy of Mohit Iyyer
Keep in mind, these attention weights are specific to each decoder timestep.
Attention distribution from t=1
Attention Mechanism
Attention scores calculated as a function of the current decoder hidden state and encoder state for a specific input position.
e.g scaled dot product
Attention Summary
Slide courtesy of Mohit Iyyer
Advanced NLP topics:
Is attention explanation?
*** Discuss ***
Advanced NLP topics:
Is attention explanation?
XAI Background
Image from MathWorks
Attention weights can be interpreted as a local explanation method
Many types of explanation methods have been proposed, including text-based and feature-based.
Limitations of Attention
But…is attention all you need to model language?
Transformers
Transformers
Transformers
Structures of each
encoder and decoder
What is the difference between these two?
The input to the first encoder is word embeddings
The subsequent encoders get the output of the previous encoder
2 linear transformations,
ReLU activation
New terminology!
Self-attention
Self-attention allows it to look at other positions in the input sequence for clues that can help lead to a better encoding for this word.
https://nlp.seas.harvard.edu/2018/04/03/attention.html
This is how we can learn locally contextual embeddings like shown in homework 1.
Self-attention
Step 1: create three vectors
They are the query, key and value vectors.
Self-attention
Step 2: calculate the attention
Self-attention
Step 5: Multiply each value vector by the attention score
Step 6: sum up the weighted value vectors
Self-attention
Self-attention
Packing embeddings into matrix
Self-attention
Self-attention Summary
We got rid of recurrence.
How do we capture positional information?
*** Discuss ***
A few more details…
In RNNs, we captured positional information with recurrence. In the absence of recurrence, we using positional embeddings.
Positional Encoding�t is position, i is dimension index, d is the vector size
Byte Pair Encoding
Slide courtesy of Mohit Iyyer
Byte Pair Encoding
Slide courtesy of Mohit Iyyer
Byte Pair Encoding
Byte Pair Encoding
Byte Pair Encoding
Byte Pair Encoding
There are even more details, but we’ll discuss next time.
BertViz (Vig, 2019)
https://colab.research.google.com/drive/1hXIQ77A4TYS4y3UthWF-Ci7V7vVUoxmQ?usp=sharing
Take-aways
Next Time…
Pretrained Transformers