Introduction to Word Embeddings
Last updated
Last updated
Word embeddings are a form of representing words in order for a machine to understand them and extract information from them.
Instead of using one-hot vectors to represent words, we use a featurized representation that consists of scores based on the word having certain features. This is because the dot product of any 2 one-hot vectors is zero, thereby leaving no means to compare words using their vectors.
For example, the following are the word embeddings for man, woman, king, queen, apple and orange:
The above matrix is known as the embedding matrix.
The columns denote word vectors and the number of columns is the size of the vocabulary. The number of rows decides the vector size. These vectors can be learned from a large vocabulary using transfer learning. More on learning word embeddings in the next sub-section.
Note that the word vector for a word can be obtained by multiplying the embedding matrix with the one-hot vector of the word.
It is interesting to know that King - Man + Woman Queen!
Also, cosine similarity can be used to estimate the similarity between two vectors. The result of cosine similarity is between -1 and 1 (similar).
cosine_similarity(u, v) =
The t-SNE algorithm allows us to create a 2D visualization of word vectors, thereby allowing us to determine words that may be grouped together.
An interesting use case for word embeddings is Named Entity Recognition. If we have the word embedding for a certain word, we can comprehend its meaning by comparing it to words in our dataset and understand its use in a given context, even if the word is not in our dataset.