34 min total

Deep Learning

Word embeddings

Word embeddings
Word embeddings — diagram

This is fine but one hot representation doesn’t captures one word's relationship to another word

Dense word representations

Word embeddings — diagram

We want to learn these embedding of words (numbers)

Word embeddings: representing words using featureful vectors

Embedding dimensions

With a vocabulary of 10,000 words, a one-hot vector has 10,000 entries. A 300 × 10,000 embedding matrix maps it to a dense vector with 300 entries.

We can view embeddings in 2D/3D dimensions using t-SNE algorithm

Transfer learning and word embeddings

  1. Learn word embeddings from large text corpus (1-100B words) or download pre-trained embedding online
  2. Transfer embedding to new task with smaller training set
  3. (Optional) finetune word embeddings with new data

Cosine similarity

Word embeddings — diagram

Embedding matrix

Word embeddings — diagram

We np.dot embedding matrix against one hot vector to fetch Embedding vector

When predicting next word in the sequence, we need to feed model the context

Apart from using the last few words as context, there are alternative ways to define context:

  1. Using both preceding and succeeding words.
  2. Using just the immediate previous word.
  3. Using a random nearby word (Skip-Gram model).

Skip-gram model

The skip-gram model creates supervised learning problems using context-to-target pairs from sentences.

Rather than using a fixed number of words as context, a random word is selected from the sentence.

Another random word, within a specified window (e.g., +/- 5 or 10 words), is selected as the target.

Calculating the softmax requires summing over the entire vocabulary, which is computationally expensive, especially for large vocabularies.

Hierarchical softmax classifier: Instead of classifying among all words at once, a binary tree of classifiers is used to narrow down choices.

This reduces the computational cost from linear to logarithmic with respect to vocabulary size.

For training, context words aren’t always chosen uniformly from the corpus.

Some words appear more frequently than others, and sampling uniformly might bias the training towards these common words.

Various heuristics balance sampling common and less common words to ensure that embeddings are learned for both.

Skip gramCBOW
Predicts surrounding words given single context wordPredicts target word from set of context words
Word embeddings — diagram

Negative sampling

Reducing the cost of word prediction

Negative Sampling presents a new supervised learning problem. For any given pair of words (like “orange” and “juice”), aiming to predict if a given pair of words are contextually related (1) or not (0).“Orange” and “juice” would be a positive example (labeled 1), while “orange” and “king” is considered a negative example (labeled 0).

Visualized as a neural network, instead of a massive Softmax layer, it employs multiple logistic regression classifiers. Only a subset of these classifiers are trained in each iteration, keeping computational costs low.

For choosing negative examples

Rather than just randomly picking words, or picking based on empirical frequency (which would over-represent common words like “the”, “and”, etc.), the authors proposed a heuristic: sample words proportionally to the frequency of a word raised to the power of 3/4.

After training ends, we will take the model weights and pass it text pairs in the future to test if two words are related or not

Word embeddings — diagram

GloVe word vectors

"Global vectors"

Word embeddings — diagram

Actually word embedding vectors are not human understandable

Sentiment classification

A simple sentiment classification algorithm

Word embeddings — diagram

Since soo many 'good' words, it may classify it as positive review

Alternatively we may use RNN for sentiment classification

Word embeddings — diagram

Debiasing word embeddings

Word embeddings — diagram
Word embeddings — diagram

How to reduce bias in word embeddings?

  1. Identify bias direction
Word embeddings — diagram
  1. Neutralize, for every word that is not definitional, project to get rid of bias
  2. Equalize pairs