Last time, we saw how autoencoders are used to learn a latent embedding space: an alternative, low-dimensional representation of a set of data with some appealing properties: for example, we saw that interpolating in the latent space is a way of generating new examples. In particular, interpolation in the latent space generates more compelling examples than, say, interpolating in the raw pixel space.
The idea of learning an alternative representation/features/embeddings of data is a prevalent one in machine learning. You already saw how we used features computed by AlexNet as a component of a model. Good representations will make downstream tasks (like generating new data, clustering, computing distances) perform much better.
GloVe embeddings provides a similar kind of pre-trained embeddings, but for words. The way that GloVe embeddings are generated is related to what we did in Project 2, but somewhat different. The specifics of the algorithm, loss function, and training is beyond the scope of this course. You should think of GloVe embeddings similarly to pre-trained AlexNet weights. More information about GloVe is available here: https://nlp.stanford.edu/projects/glove/
Just like for AlexNet, PyTorch makes it easy for us to use pre-trained GloVe embeddings. There are several variations of GloVe embeddings available; they differ in the corpus (data) that the embeddings are trained on, and the size (length) of each word embedding vector.
These embeddings were trained by the authors of GloVe (Pennington et al. 2014), and are also available on the website https://nlp.stanford.edu/projects/glove/
To load pre-trained GloVe embeddings, we'll use a package called torchtext
.
The package torchtext
contains other useful tools for working with text
that we will see later in the course. The documentation for torchtext
GloVe vectors are available at: https://torchtext.readthedocs.io/en/latest/vocab.html#glove
import torch
import torchtext
# The first time you run this will download a ~823MB file
glove = torchtext.vocab.GloVe(name="6B", # trained on Wikipedia 2014 corpus
dim=100) # embedding size = 50
Let's look at what the embedding of the word "car" looks like:
glove['cat']
It is a torch tensor with dimension (50,)
. It is difficult to determine what each
number in this embedding means, if anything. However, we know that there is structure
in this embedding space. That is, distances in this embedding space is meaningful.
To explore the structure of the embedding space, it is necessary to introduce a notion of distance. You are probably already familiar with the notion of the Euclidean distance. The Euclidean distance of two vectors $x = [x_1, x_2, ... x_n]$ and $y = [y_1, y_2, ... y_n]$ is just the 2-norm of their difference $x - y$. We can compute the Euclidean distance between $x$ and $y$: $\sqrt{\sum_i (x_i - y_i)^2}$
The PyTorch function torch.norm
computes the 2-norm of a vector for us, so we
can compute the Euclidean distance between two vectors like this:
x = glove['cat']
y = glove['dog']
torch.norm(y - x)
An alternative measure of distance is the Cosine Similarity. The cosine similarity measures the angle between two vectors, and has the property that it only considers the direction of the vectors, not their the magnitudes. (We'll use this property next class.)
x = torch.tensor([1., 1., 1.]).unsqueeze(0)
y = torch.tensor([2., 2., 2.]).unsqueeze(0)
torch.cosine_similarity(x, y) # should be one
The cosine similarity is a similarity measure rather than a distance measure: The larger the similarity, the "closer" the word embeddings are to each other.
x = glove['cat']
y = glove['dog']
torch.cosine_similarity(glove['cat'].unsqueeze(0),
glove['dog'].unsqueeze(0))
Now that we have a notion of distance in our embedding space, we can talk about words that are "close" to each other in the embedding space. For now, let's use Euclidean distances to look at how close various words are to the word "cat".
word = 'cat'
other = ['dog', 'bike', 'kitten', 'puppy', 'kite', 'computer', 'neuron']
for w in other:
dist = torch.norm(glove[word] - glove[w]) # euclidean distance
print(w, float(dist))
In fact, we can look through our entire vocabulary for words that are closest to a point in the embedding space -- for example, we can look for words that are closest to another word like "cat". (You did this in project 2!)
Keep in mind that GloVe vectors are trained on word co-occurrences, and so words with similar embeddings will tend to co-occur with other words. For example, "cat" and "dog" tend to occur with similar other words---even more so than "cat" and "kitten" because these two words tend to occur in different contexts!
def print_closest_words(vec, n=5):
dists = torch.norm(glove.vectors - vec, dim=1) # compute distances to all words
lst = sorted(enumerate(dists.numpy()), key=lambda x: x[1]) # sort by distance
for idx, difference in lst[1:n+1]: # take the top n
print(glove.itos[idx], difference)
print_closest_words(glove["cat"], n=10)
print_closest_words(glove['nurse'])
print_closest_words(glove['computer'])
print_closest_words(glove['white'])
We could also look at which words are closest to the midpoints of two words:
print_closest_words((glove['happy'] + glove['sad']) / 2)
One surprising aspect of GloVe vectors is that the directions in the embedding space can be meaningful. The structure of the GloVe vectors certain analogy-like relationship like this tend to hold:
$$ king - man + woman \approx queen $$
print_closest_words(glove['king'] - glove['man'] + glove['woman'])
We get reasonable answers like "queen", "throne" and the name of our current queen.
We can likewise flip the analogy around:
print_closest_words(glove['queen'] - glove['woman'] + glove['man'])
Or, try a different but related analogies along the gender axis:
print_closest_words(glove['king'] - glove['prince'] + glove['princess'])
print_closest_words(glove['uncle'] - glove['man'] + glove['woman'])
print_closest_words(glove['grandmother'] - glove['mother'] + glove['father'])
print_closest_words(glove['old'] - glove['young'] + glove['father'])
Machine learning models have an air of "fairness" about them, since models make decisions without human intervention. However, models can and do learn whatever bias is present in the training data!
GloVe vectors seems innocuous enough: they are just representations of words in some embedding space. Even so, we'll show that the structure of the GloVe vectors encodes the everyday biases present in the texts that they are trained on.
We'll start with an example analogy:
$$ doctor - man + woman \approx ?? $$
Let's use GloVe vectors to find the answer to the above analogy:
print_closest_words(glove['doctor'] - glove['man'] + glove['woman'])
The $$doctor - man + woman \approx nurse$$ analogy is very concerning. Just to verify, the same result does not appear if we flip the gender terms:
print_closest_words(glove['doctor'] - glove['woman'] + glove['man'])
We see similar types of gender bias with other professions.
print_closest_words(glove['programmer'] - glove['man'] + glove['woman'])
Beyond the first result, none of the other words are even related to programming! In contrast, if we flip the gender terms, we get very different results:
print_closest_words(glove['programmer'] - glove['woman'] + glove['man'])
Here are the results for "engineer":
print_closest_words(glove['engineer'] - glove['man'] + glove['woman'])
print_closest_words(glove['engineer'] - glove['woman'] + glove['man'])