RNN & LSTM
Lecture 5
Language Models Recap
Jurafsky & Martin chapter 3
Ngram Language Models
Naive first attempt:
wab = sequence wa, wa+1, wa+2, …, wb-1, wb
We measure P(wi | w1i-1) by counting the number of times the sequence w1i-1 happens in the training set, and measuring the percent of times it was followed by wi
What is wrong with this?
Naive example
P(“Please turn your homework in”)
= P(sequence starts with “Please”)*P(second word is “turn” | first word is “Please”)
* P(third word is “your” | first two words are “Please turn”)�* P(fourth word is “homework” | first three words are “Please turn your”)�* P(fifth word is “in” | first four words are “Please turn your homework”)
By the end of the document, the P will be conditioned on the entire document, which only happens once. So it will just memorize that one document
Ngram Language Models
Rather than using the entire previous sequence, we use only the last n terms
The probability of the sequence is:
We calculate these conditional probabilities from the training set:
Ngram Language Model Example
In this example, N=2
We use the Berkeley Restaurant Project dataset, which looks like this:
Ngram Language Model Example
P(<s> i want english food </s>)
= P(i|<s>) P(want|i) P(english|want) P(food|english) P(</s>|food)
= .25 × .33 × .0011 × 0.5 × 0.68
= .000031
P(<s> i want chinese food </s>)
= P(i|<s>) P(want|i) P(chinese|want) P(food|chinese) P(</s>|food)
= .25 × .33 × .0065 × 0.5 × 0.52
= 0.000139
Ngram Language Models: Practical Considerations
Ngram Language Models: Generating Sentences
Perplexity
Neural Networks & Deep Learning
Jurafsky & Martin chapters 7 & 9
Neural Networks
The network that we saw for Word2Vec is an example of a fully connected neural network with 1 hidden layer
Input layer
Output layer
Hidden layer
Building Block: Perceptron
1
(bias)
> 0?
Neural unit: Perceptron + activation function
Multi-layer Perceptrons (MLP)
U = W[2]
Each g is an activation function
including g[2], which is softmax
Training a Neural Network
Training a neural net: Backpropagation
Activation functions
Activation functions
Activation functions
More useful if you don’t just want {0,1} output, and you want numbers between 0 and 1 (for example, if you have a classifier with multiple classes and want to compare numbers for each)
Activation functions
Scaled version of sigmoid. It just has a “stronger” derivative.
Note:
Activation functions
Since the range is [0, inf), it can “blow up” the activation.
Not as easy for classification as the functions with range [0,1].
Since the derivative in negative section is 0, it can cause neurons to “die” (no update to weights). Solution: make the horizontal line into a very slightly tilted line → “leaky ReLu”
Since negative values → 0, ReLu is less costly and overtrains less.
Hyperparameters
Naive Neural Language Model: Sliding Window
Naive Neural LM: Sliding Window
Naive Neural Language Model: Sliding Window
Sequences
y = f(x(t))
y = f(s(t)) = F(x(t), x(t-1), …, x(1))
Solution: Recurrent Neural Network
Training a Recurrent Neural Network
Structure
Recurrent Neural Language Models
Generation with Recurrent Neural Language Models
Sequence Labeling with RNNs
Sequence Classification with RNNs
Stacked RNNs
Bidirectional RNNs
Bidirectional RNN
Bidirectional RNN for classification
Further reading
Long Short-Term Memory (LSTM)
Long Short-Term Memory
Forget gate
Previous context
Previous hidden state
Current input
Current hidden state
Current context
Add gate
Output gate
sigmoid
sigmoid
g is the part that came from RNN (with a tanh activation function)
f, i, and o are sigmoided and used as the masks for the forget, add, and output gates, respectively
Output gate decides what is important for current output as opposed to later
sigmoid
Gated Recurrent Unit (GRU)
weights
RNN layer)
Subwords
Downsides to representing entire words as embeddings:
Approaches to addressing this:
RNNs with Subwords
The lower Bi-RNN processes sequences of characters, and the upper RNN processes sequences of words.
This way you can still get output per word.
The backprop goes from the output task all the way back to the character level.
RNNs can be any variation (LSTM, Bi-RNN, …)
Encoder-Decoder Models
Attention in Encoder-Decoder Models