This material is adapted from Alammar, J (2018). The Illustrated Transformer [Blog post]. Retrieved from https://jalammar.github.io/illustrated-word2vec/ and used under the Creative Commons Attribution—NonCommercial—ShareAlike 4.0 International License.
If we want to apply machine learning to text, especially when using a neural network, we need a way to represent the text as numbers. The way we normally represent text in computers—as a sequence of ASCII characters—is not suitable for machine learning algorithms. We don’t want to focus on the characters themselves, but rather on the meaning and relationships between the words. In the sentence “Bob needed a book, so he went to the library.”, we want to focus on Bob, not B, o, b.
We could try to invent a dictionary that maps each word to a unique number. However, there are several issues with this approach. For one, the dictionary would need to be very large to cover all the words in a typical vocabulary. Another important issue is that the meaning and relationships between words would be lost in the translation to numbers.
Embeddings are a way to represent words as a sequence of numbers (a vector) in a way that captures information about the word’s meaning and relationships with other words. This allows machine learning algorithms to better understand and process text. Applications such as Siri, Google Assistant, Alexa, Google Translate, and smartphone keyboards with next-word prediction use this approach.