NLP/NLU Specialization Notes
  • Natural Language Processing/Understanding Notes, Resources
  • Natural Language Processing With Attention Models
    • NLP With Attention Models
  • Natural Language Processing With Attention Models Notebooks
    • Assignment 1: Neural Machine Translation
    • Basic Attention Operation: Ungraded Lab
    • Calculating the Bilingual Evaluation Understudy (BLEU) score: Ungraded Lab
    • Scaled Dot-Product Attention: Ungraded Lab
    • Assignment 2: Transformer Summarizer
    • The Three Ways of Attention and Dot Product Attention: Ungraded Lab Notebook
    • Masking
    • Positional Encoding
    • Assignment 3: Question Answering
    • Assignment 3: Question Answering
    • Question Answering with BERT and HuggingFace
    • Question Answering with BERT and HuggingFace 🤗 (Fine-tuning)
    • SentencePiece and BPE
  • Natural Language Processing With Classification And Vector Spaces
    • NLP With Classification and Vector Spaces
  • Natural Language Processing With Sequence Models
    • NLP With Sequence Models
  • Natural Language Processing with Classification and Vector Spaces Notebooks
    • Assignment 1: Logistic Regression
    • Preprocessing
    • Building and Visualizing word frequencies
    • Visualizing tweets and the Logistic Regression model
    • Assignment 2: Naive Bayes
    • Assignment 3: Hello Vectors
    • Linear algebra in Python with NumPy
    • Manipulating word embeddings
    • Another explanation about PCA
    • Assignment 4 - Naive Machine Translation and LSH
    • Vector manipulation in Python
    • Hash functions and multiplanes
  • Natural Language Processing with Probabilistic Models
    • NLP With Probabilistic Models
  • Natural Language Processing with Probabilistic Models Notebooks
    • Assignment 1: Autocorrect
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 01
    • NLP Course 2 Week 1 Lesson : Building The Model - Lecture Exercise 02
    • Assignment 2: Parts-of-Speech Tagging (POS)
    • Parts-of-Speech Tagging - First Steps: Working with text files, Creating a Vocabulary and Handling Unknown Words
    • Parts-of-Speech Tagging - Working with tags and Numpy
    • Assignment 3: Language Models: Auto-Complete
    • N-grams Corpus preprocessing
    • Building the language model
    • Out of vocabulary words (OOV)
    • Assignment 4: Word Embeddings
    • Word Embeddings First Steps: Data Preparation
    • Word Embeddings: Intro to CBOW model, activation functions and working with Numpy
    • Word Embeddings: Training the CBOW model
    • Word Embeddings: Hands On
    • Word Embeddings: Ungraded Practice Notebook
  • Natural Language Processing with Sequence Models Notebooks
    • Assignment 1: Deep N-grams
    • Hidden State Activation : Ungraded Lecture Notebook
    • Assignment 1: Sentiment with Deep Neural Networks
    • Vanilla RNNs and GRUs
    • Lab 1: TensorFlow Tutorial and Some Useful Functions
    • Calculating perplexity using numpy: Ungraded Lecture Notebook
    • Assignment 2 - Named Entity Recognition (NER)
    • Vanishing Gradients and Exploding Gradients in RNNs : Ungraded Lecture Notebook
    • Evaluate a Siamese model: Ungraded Lecture Notebook
    • Assignment 3: Question duplicates
    • Modified Triplet Loss : Ungraded Lecture Notebook
    • Creating a Siamese model: Ungraded Lecture Notebook
  • Stanford
    • Stanford CS 224U,224N
  • Udacity
    • NLP Nanodegree
  • Previous
  • Next
  • Word Embeddings: Hands On
    • Extracting word embedding vectors

Word Embeddings: Hands On¶

In previous lecture notebooks you saw all the steps needed to train the CBOW model. This notebook will walk you through how to extract the word embedding vectors from a model.

Let's dive into it!

In [ ]:
Copied!
import numpy as np
from utils2 import get_dict
import numpy as np from utils2 import get_dict

Before moving on, you will be provided with some variables needed for further procedures, which should be familiar by now. Also a trained CBOW model will be simulated, the corresponding weights and biases are provided:

In [ ]:
Copied!
# Define the tokenized version of the corpus
words = ['i', 'am', 'happy', 'because', 'i', 'am', 'learning']

# Define V. Remember this is the size of the vocabulary
V = 5

# Get 'word2Ind' and 'Ind2word' dictionaries for the tokenized corpus
word2Ind, Ind2word = get_dict(words)


# Define first matrix of weights
W1 = np.array([[ 0.41687358,  0.08854191, -0.23495225,  0.28320538,  0.41800106],
               [ 0.32735501,  0.22795148, -0.23951958,  0.4117634 , -0.23924344],
               [ 0.26637602, -0.23846886, -0.37770863, -0.11399446,  0.34008124]])

# Define second matrix of weights
W2 = np.array([[-0.22182064, -0.43008631,  0.13310965],
               [ 0.08476603,  0.08123194,  0.1772054 ],
               [ 0.1871551 , -0.06107263, -0.1790735 ],
               [ 0.07055222, -0.02015138,  0.36107434],
               [ 0.33480474, -0.39423389, -0.43959196]])

# Define first vector of biases
b1 = np.array([[ 0.09688219],
               [ 0.29239497],
               [-0.27364426]])

# Define second vector of biases
b2 = np.array([[ 0.0352008 ],
               [-0.36393384],
               [-0.12775555],
               [-0.34802326],
               [-0.07017815]])
# Define the tokenized version of the corpus words = ['i', 'am', 'happy', 'because', 'i', 'am', 'learning'] # Define V. Remember this is the size of the vocabulary V = 5 # Get 'word2Ind' and 'Ind2word' dictionaries for the tokenized corpus word2Ind, Ind2word = get_dict(words) # Define first matrix of weights W1 = np.array([[ 0.41687358, 0.08854191, -0.23495225, 0.28320538, 0.41800106], [ 0.32735501, 0.22795148, -0.23951958, 0.4117634 , -0.23924344], [ 0.26637602, -0.23846886, -0.37770863, -0.11399446, 0.34008124]]) # Define second matrix of weights W2 = np.array([[-0.22182064, -0.43008631, 0.13310965], [ 0.08476603, 0.08123194, 0.1772054 ], [ 0.1871551 , -0.06107263, -0.1790735 ], [ 0.07055222, -0.02015138, 0.36107434], [ 0.33480474, -0.39423389, -0.43959196]]) # Define first vector of biases b1 = np.array([[ 0.09688219], [ 0.29239497], [-0.27364426]]) # Define second vector of biases b2 = np.array([[ 0.0352008 ], [-0.36393384], [-0.12775555], [-0.34802326], [-0.07017815]])

Extracting word embedding vectors¶

Once you have finished training the neural network, you have three options to get word embedding vectors for the words of your vocabulary, based on the weight matrices $\mathbf{W_1}$ and/or $\mathbf{W_2}$.

Option 1: extract embedding vectors from $\mathbf{W_1}$¶

The first option is to take the columns of $\mathbf{W_1}$ as the embedding vectors of the words of the vocabulary, using the same order of the words as for the input and output vectors.

Note: in this practice notebooks the values of the word embedding vectors are meaningless after a single iteration with just one training example, but here's how you would proceed after the training process is complete.

For example $\mathbf{W_1}$ is this matrix:

In [ ]:
Copied!
# Print W1
W1
# Print W1 W1

The first column, which is a 3-element vector, is the embedding vector of the first word of your vocabulary. The second column is the word embedding vector for the second word, and so on.

The first, second, etc. words are ordered as follows.

In [ ]:
Copied!
# Print corresponding word for each index within vocabulary's range
for i in range(V):
    print(Ind2word[i])
# Print corresponding word for each index within vocabulary's range for i in range(V): print(Ind2word[i])

So the word embedding vectors corresponding to each word are:

In [ ]:
Copied!
# Loop through each word of the vocabulary
for word in word2Ind:
    # Extract the column corresponding to the index of the word in the vocabulary
    word_embedding_vector = W1[:, word2Ind[word]]
    # Print word alongside word embedding vector
    print(f'{word}: {word_embedding_vector}')
# Loop through each word of the vocabulary for word in word2Ind: # Extract the column corresponding to the index of the word in the vocabulary word_embedding_vector = W1[:, word2Ind[word]] # Print word alongside word embedding vector print(f'{word}: {word_embedding_vector}')

Option 2: extract embedding vectors from $\mathbf{W_2}$¶

The second option is to take $\mathbf{W_2}$ transposed, and take its columns as the word embedding vectors just like you did for $\mathbf{W_1}$.

In [ ]:
Copied!
# Print transposed W2
W2.T
# Print transposed W2 W2.T
In [ ]:
Copied!
# Loop through each word of the vocabulary
for word in word2Ind:
    # Extract the column corresponding to the index of the word in the vocabulary
    word_embedding_vector = W2.T[:, word2Ind[word]]
    # Print word alongside word embedding vector
    print(f'{word}: {word_embedding_vector}')
# Loop through each word of the vocabulary for word in word2Ind: # Extract the column corresponding to the index of the word in the vocabulary word_embedding_vector = W2.T[:, word2Ind[word]] # Print word alongside word embedding vector print(f'{word}: {word_embedding_vector}')

Option 3: extract embedding vectors from $\mathbf{W_1}$ and $\mathbf{W_2}$¶

The third option, which is the one you will use in this week's assignment, uses the average of $\mathbf{W_1}$ and $\mathbf{W_2^\top}$.

Calculate the average of $\mathbf{W_1}$ and $\mathbf{W_2^\top}$, and store the result in W3.

In [ ]:
Copied!
# Compute W3 as the average of W1 and W2 transposed
W3 = (W1+W2.T)/2

# Print W3
W3
# Compute W3 as the average of W1 and W2 transposed W3 = (W1+W2.T)/2 # Print W3 W3

Expected output:

array([[ 0.09752647,  0.08665397, -0.02389858,  0.1768788 ,  0.3764029 ],
       [-0.05136565,  0.15459171, -0.15029611,  0.19580601, -0.31673866],
       [ 0.19974284, -0.03063173, -0.27839106,  0.12353994, -0.04975536]])

Extracting the word embedding vectors works just like the two previous options, by taking the columns of the matrix you've just created.

In [ ]:
Copied!
# Loop through each word of the vocabulary
for word in word2Ind:
    # Extract the column corresponding to the index of the word in the vocabulary
    word_embedding_vector = W3[:, word2Ind[word]]
    # Print word alongside word embedding vector
    print(f'{word}: {word_embedding_vector}')
# Loop through each word of the vocabulary for word in word2Ind: # Extract the column corresponding to the index of the word in the vocabulary word_embedding_vector = W3[:, word2Ind[word]] # Print word alongside word embedding vector print(f'{word}: {word_embedding_vector}')

Now you know 3 different options to get the word embedding vectors from a model!

How this practice relates to and differs from the upcoming graded assignment¶

  • After extracting the word embedding vectors, you will use principal component analysis (PCA) to visualize the vectors, which will enable you to perform an intrinsic evaluation of the quality of the vectors, as explained in the lecture.

Congratulations on finishing all lecture notebooks for this week!

You're now ready to take on this week's assignment!

Keep it up!


Documentation built with MkDocs.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search