Word Embedding

One-hot encoding

Problems

  1. High-dimension: Dimension of encoded data is equivalent to the number of different tokens within the dictionary
  2. Sparse:
  3. Lack of semantic meaning: we want the embedding of the token to represent something that we can understand. For example, King-Queen = Man-Woman. Ideally, we hope the minus of the embedding of “king” and “queen” is equivalent to the minus of the embedding of “man” and “woman” (the different between “king” and “queen” is the same as the different between “man” and “woman”)

Example:

However, human labeling is time-consuming and lack of generalization, human labeling is very subjective

Traditional Word Embedding

Word Embedding Model

We want a model for mapping word to embedding that can compact representations for each word with no label (i.e., every token has semantic meaning and can link to some other tokens)

We need to define the loss function and the model architecture

  • Fully connected neural network
  • Able to deal with unlabel data

Loss Function:

  • With the nice embedding, words can indicate their neighbor words in the sentences, and vice versa (if the embedding word is meaningful, then with its embedding and the model, we can probably infer other words in the sentences)

  • Example: The man loves his son

Skip-Gram Model

Assume $u$ and $v$ are the embeddings from the neural network model

  • The probability of getting the word outputs (surrounding words) based on the prior:
  • For a sentence with $T$ words, the probability of having the exact sentence with window size $m$:
  • Loss function:

Idea: it uses the center word to predict the surrounding words within the window

Continuous Bag of Words Model

Assume $u$ and $v$ are the embeddings from the neural network model

  • The probability of getting the word output (center word) based on the prior:

  • For a sentence with $T$ words, the probability of having the exact sentence with window size $m$:

  • Loss function:

Idea: it uses the surrounding words to predict the center word within the window

Limitations

  1. Can’t consider the sentence context for each word
    After the embedding is generated, the embedding for each word will be fixed. Although it uses sentence information to learn the embedding for each word, the embedding for each word will be fixed when we are using the embedding, so we cannot consider the context when using the embedding of each word. Although we consider the sentence context during the training, those embedding will be fixed during the application
  2. Negative embedding
    Suppose we want to generate $w_o$ (the center word), then we need to calculate the distribution of $w_o$ over all the tokens within the dictionary ($i \in V$), which means we need to consider the entire dictionary in order to get the distribution of a token. Therefore, the computational cost is very expensive and inefficient
  3. Context-dependent word embedding
    A word (exact token) can have multiple meanings in different contexts, so the embedding should be different in different contexts

BERT

Idea: With the attention machanism, we can generate the context-dependent embedding or hidden state of the query token, so we can apply the attention machansim for the word embedding task.

Focusing on the Encoder within the Transformer: Given a sentence, generate an embedding, which is very good to be the embedding task

BERT: Bidirectional Encoder Representations from Transformers, which only depends on the transformer encoder

Bidirectional: With bidirectional LSTM, we can consider the paths from the future. In the attention model, for each of the tokens within the sentence, we will consider the token’s interaction against all the other tokens within the sentence, which means not just the token before it will be considered, it also considers the tokens after it

Training

  1. Task 1: Classification Task
    Within a sentence, it will mask some tokens and then predict the masked token

    GOAL: Use the multi-layer attention model to get the embedding for all the input tokens and to predict the token MASK, and stack a prediction layer to predict the missing token MASK (Can use softmax to select the most likely token). Then, compare the prediction with the ground truth sentence in the training layer and compute the loss to make a good propagation and train the model

  2. Task 2: Binary Prediction Task (with 2 pairs of sentences as input)
    If we have a paragraph, not just a separate sentence, we may want to predict whether 2 sentences are next to each other

    GOAL: To predict whether 2 sentences are next to each other. To get read of handling the variant length of the sentences, we have a token CLS put at the beginning of all sentences and SEP to separate the sentences

    • CLS is a reversed token for the embedding to stack another layer to make the prediction
    • Question: Why the CLS token can consider the information of both sentence 1 and sentence 2?
      Due to the attention model, in attention model, each token interacts with all the tokens within the input, which means the CLS token will interact with sentence 1, the SEP token and sentence 2, so the information from sentence 1 and sentence 2 will be aggregated during the attention mechanism, such that it has enough information to predict whether 2 sentences are next to each other
  • Loss Function: Linear combination of the above two tasks

Applications

  1. As the feature extractor

  2. As the pre-trained model before fine-tunning
    Directly stack another layer to the BERT model, with the last layer related to the downstream task (parameters within the last layer are initialized randomly but the parameters within the BERT model are pre-trained )

  • Pre-training BERT requirement
    • Parameters: 340 millions
    • Corpus: BooksCorpus: 800M words, English Wikipedia: 2,500M words

Questions

With skip-gram or continuous bag of words methods, the word embedding of each word is the same in different sentences (true)
Attention model is context-dependent. For different sentences, the outputted hidden state of the query can be different (true)
Skip-gram or continuous bag of words methods are supervised learning methods (false: they are unsupervised learning methods)
Trained with different corpus, the embedding of the same word can be slightly different (true)