podqast
Machine Learning GuideOCDevelAll episodes

MLG 022 Deep NLP 1

49 min
Next episodeMLG 023 Deep NLP 243 min
Get its new episodes on Latest. No account

A recurrent neural network reads a sentence one word at a time, carrying a hidden state forward, and word2vec learns word vectors that place words used in similar contexts near each other. Also covers encoder-decoder translation, one-hot vs dense vectors, skip-gram and CBOW, GloVe and where to learn deep NLP.
Links • Notes and resources at ocdevel.com/mlg/22

Why deep learning for NLP? Words combine in ways a linear model can't add up: "not good" means something neither word means alone. A neural network learns these combinations from data (feature learning) instead of needing them engineered by hand, and each deeper layer combines the combinations below it. Deep models are also end-to-end: one network trained as a whole replaces the separately trained parsing, alignment and language models of classical machine translation (MLG 020).
How a recurrent neural network works A feed-forward network maps one input to one output. A recurrent neural network (RNN) reads a sequence one step at a time, reusing the same weights at every step, and keeps a hidden state: a vector summarizing what it has read so far. At each step it combines the current input, such as a word, with the previous hidden state to make the new hidden state, and can emit an output.
h_t = tanh(W·x_t + U·h_(t−1) + b) "Recurrent" names this recurrence, the hidden state fed back in at each time step. It isn't function recursion. For training, the network is unrolled through time and trained by backpropagation through time.
Emitting one output per input word, an RNN can tag parts of speech or named entities, jobs that hidden Markov models did in MLG 019, with the hidden state carrying what came earlier in the sentence.
Encoder-decoder (sequence to sequence) For translation, an encoder RNN reads the whole source sentence into its final hidden state, a fixed-size vector, and a decoder RNN generates the translation from that vector one word at a time. Sentiment analysis and classification are the one-output case: read the whole sequence, emit one label.
Squeezing a long sentence into one vector loses information. Attention (Bahdanau et al., 2014) let the decoder look back at every encoder state, and the transformer (Vaswani et al., 2017) dropped recurrence and kept attention. Transformers and the large language models built on them have since replaced RNNs for translation and most other NLP (MLG 033, MLG 034).
From one-hot vectors to word embeddings A one-hot vector represents a word as all zeros except a 1 at that word's index in the vocabulary. It carries no meaning: every word is equally far from every other. A word embedding is a dense vector, typically 100 to 300 numbers for word2vec and GloVe, learned so that words used in similar contexts sit near each other.
Closeness between embeddings is usually measured by cosine similarity, the angle between two vectors. Search uses the same measure on TF-IDF document vectors, ranking documents by highest cosine similarity to the query (MLG 019).
Vector arithmetic captures some relationships. In the word2vec paper, vector("King") − vector("Man") + vector("Woman") is closest to vector("Queen"). Because embeddings come from context, antonyms often land close together: "good" and "bad" both follow "the movie was".
word2vec and GloVe word2vec (Mikolov et al., 2013) is a shallow neural network trained to predict context, in one of two forms:

More from Machine Learning Guide