Word Embeddings
Tags: machinelearning, beginners
Published: 2020-02-16 16:18:11

Word embeddings enable **knowledge representation where a vector represents a word**. This improves the ability for neural networks to learn from a textual dataset.

Before word embeddings were de facto standard for natural language processing, a common approach to deal with words was to use a one-hot vectorisation. Each word represents a column in the vector space, and each sentence is a vector of ones and zeros. Ones denote the presence of the word in the sentence.

![](https://dev-to-uploads.s3.amazonaws.com/i/guxg4pezjrdzixcr7qw5.png)

One-hot vectorisation [taken from [Text Encoding: A Review](https://towardsdatascience.com/text-encoding-a-review-7c929514cccf)]

As a result, this leads to a huge and sparse representation, because there are much more zeros than ones. When there are many words in the vocabulary, this creates a large word vector. This might become a problem for machine learning algorithms.

One-hot vectorisation also fails to capture the meaning of words. For example, “_drink_” and “_beverage_”, even though these are two different words, they have a similar definition.

With word embeddings, semantically similar words have similar vectors representation. As a result, “_I would like to order a drink_” or “_a beverage_”, an ordering system can interpret that request the same way.

## In the past

Back in 2003, [Yoshua Bengio et al.](http://www.jmlr.org/papers/volume3/bengio03a/bengio03a.pdf) introduced a language model concept. The focus of the paper is to learn representations for words, which allow the model to predict the next word.

This paper is crucial and led to the development to discover word embeddings. Yoshua received the Turing Award alongside with Geoffrey Hinton, and Yann LeCun.

![](https://dev-to-uploads.s3.amazonaws.com/i/zt4ydadx4rn37pptgfyb.png)

Input sequence of feature vectors for words, to a conditional probability distribution over words, to predict next word [image taken from [paper](http://www.jmlr.org/papers/volume3/bengio03a/bengio03a.pdf)]

In 2008, [Ronan and Jason](https://thetalkingmachines.com/sites/default/files/2018-12/unified_nlp.pdf) worked on a neural network that could learn to identify similar words. Their discovery has opened up many possibilities for natural language processing. The table below shows a list of words and the respective ten most similar words.

![](https://dev-to-uploads.s3.amazonaws.com/i/y8fsh51dwlwrg6xplpd8.png)
![](https://dev-to-uploads.s3.amazonaws.com/i/zij2ezhlsajqya3u14o3.png)

Left figure: Neural network architecture for given input sentence, outputs class probabilities. Right table: 5 chosen words and 10 most similar words. [sources taken from [paper](https://thetalkingmachines.com/sites/default/files/2018-12/unified_nlp.pdf)]

In 2013, [Tomas Mikolov et al.](https://arxiv.org/pdf/1301.3781.pdf) introduced learning high-quality word vectors from datasets with billions of words. They named it _Word2Vec,_ and it contains millions of words in the vocabulary.

Word2Vec has become popular since then. Nowadays, the word embeddings layer is in all popular deep learning framework.

## Examples

On Google’s pretrained Word2Vec model, they trained on roughly 100 billion words from Google News dataset. The word “_cat_” shares the closest meanings to “_cats_”, “_dog_”, “_mouse_”, “_pet_”.

![](https://dev-to-uploads.s3.amazonaws.com/i/0sh9zl3peu4ags4d80xd.png)

The word “_cat_” is geometrically closer to to “_cats_”, “_dog_”, “_mouse_”, “_pet_”. [taken from [Embedding Projector](http://projector.tensorflow.org/)]

Word embedding also manages to recognise relationships between words. A classic example is the gender-role relationships between words. For example, “_man_” is to “_woman_” is like “_king_” is to “_queen_”.

![](https://dev-to-uploads.s3.amazonaws.com/i/nb3roi5ftam1mmi9jow8.png)

Interesting relationships between words learnt from GloVe unsupervised learning algorithm [[image source](https://nlp.stanford.edu/projects/glove/)]

## Do it yourself

TensorFlow has provided a [tutorial](https://www.tensorflow.org/tutorials/text/word_embeddings) on word embeddings and codes in this [Colab notebook](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/text/word_embeddings.ipynb). You can get your hands dirty with the codes and use it to train your word embeddings on your dataset. This can definitely help you get started.

For who enjoys animation, there is a cool embeddings visualisation on [Embedding Projector](http://projector.tensorflow.org/). Every dot represents a word, and you can visualise semantically similar words in a 3D space.