Skip to content
MLG 022 Deep NLP 1 episode artwork

Jul 29, 2017 49 min

MLG 022 Deep NLP 1

from Machine Learning Guide

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 Join the free MLG group: listen to my other AI shows, ask me questions, and talk with other listeners 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: Skip-gram predicts the surrounding words from the center word. CBOW (continuous bag of words) predicts the center word from the surrounding words. Predicting over the whole vocabulary at every step is expensive, so training uses hierarchical softmax or negative sampling, a simplified form of noise-contrastive estimation that teaches the model to tell real context words from random ones. The learned weights form the embedding matrix, one row per word. Latent semantic analysis (LSA) factorizes a term-document count matrix with singular value decomposition (SVD); other count-based methods start from word-by-word co-occurrence counts. GloVe (Pennington, Socher and Manning, 2014) also works from global co-occurrence counts, but learns vectors by gradient descent so that their dot products fit the logarithm of those counts. Both give static embeddings: one vector per word, whatever the sentence. Contextual embeddings from transformers, such as BERT (2018), give a word a different vector in each sentence, and most current text-embedding models are transformers. Plotting embeddings To see embeddings in 2-D or 3-D you project them down. t-SNE is nonlinear and keeps each point's nearest neighbors close, so clusters of related words show up. PCA is linear: it projects onto the directions of greatest variance. UMAP is a common nonlinear alternative to t-SNE. Vanishing gradients, LSTM and GRU A vanilla RNN's units typically use tanh. Backpropagating through many time steps makes gradients shrink toward zero (vanishing) or blow up (exploding). LSTM and GRU cells, covered in MLG 023, use gates to carry information across long spans and ease vanishing gradients; gradient clipping caps exploding ones. Where to learn deep NLP Andrej Karpathy's The Unreasonable Effectiveness of Recurrent Neural Networks (2015), a visual, conceptual read with character-level LSTM examples. Stanford's CS224N: NLP with Deep Learning, taught by Christopher Manning, which absorbed Richard Socher's CS224D in 2017. Its latest public lectures on YouTube are from Spring 2024. The lectures lean on slides, so watch rather than listen. Dan Jurafsky and James H. Martin's Speech and Language Processing, the book from the earlier NLP episodes, free online as a third-edition draft: chapter 5 covers embeddings and chapter 14 RNNs and LSTMs. Manning taught Stanford's earlier NLP lectures with Jurafsky but isn't an author of this book. For RNNs within deep learning as a whole, Deep Learning by Goodfellow, Bengio and Courville covers them in chapter 10. Next: MLG 023, on LSTM and GRU cells, bidirectional RNNs and the vanishing-gradient problem.

Episode details come from the podcast's RSS feed.

Embed this episode

Ready to play

MLG 022 Deep NLP 1

0:00 49:25

No transcript yet.

Requests help us decide what to transcribe next.

Frequently asked questions

How long is this episode of Machine Learning Guide?

This episode is 49 minutes long.

When was this Machine Learning Guide episode published?

This episode was published on July 29, 2017.

Can I download this Machine Learning Guide episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.