Overview of Word Embedding using Embeddings from Language Models (ELMo)
What is word embeddings?
It is the representation of words into vectors. These vectors capture important information about the words such that the words sharing the same neighborhood in the vector space represent similar meaning. There are various methods for creating word embeddings, for example, Word2Vec, Continuous Bag of Words(CBOW), Skip Gram, Glove, Elmo, etc.
Embeddings from Language Models(ELMo) :
- ELMo is an NLP framework developed by AllenNLP. ELMo word vectors are calculated using a two-layer bidirectional language model (biLM). Each layer comprises forward and backward pass.
- Unlike Glove and Word2Vec, ELMo represents embeddings for a word using the complete sentence containing that word. Therefore, ELMo embeddings are able to capture the context of the word used in the sentence and can generate different embeddings for the same word used in a different context in different sentences.
Embeddings from Language Models(ELMo)
For Example: –
- I love to watch Television.
- I am wearing a wristwatch.
Here in the 1st sentence watch is used as a verb while in the 2nd sentence watch is a noun. These words having different context in different sentences are called polysemous words.ELMo can successfully handle this nature of words that GLOVE or FastText fail to capture.
Implementation of word embeddings using ELMo:
The below code is tested on google colab. Run these command before running the code in your terminal to install the necessary libraries.
pip install "tensorflow>=2.0.0"
pip install --upgrade tensorflow-hub
tensorflow_hub as hub
tensorflow.compat.v1 as tf
"I love to watch TV"
"I am wearing a wrist watch"
'Word embeddings for word WATCH in first sentence'
'Word embeddings for word WATCH in second sentence'
Word embeddings for word WATCH in first sentence
[ 0.14079645 -0.15788531 -0.00950466 ... 0.4300597 -0.52887094
Word embeddings for word WATCH in second sentence
[-0.08213335 0.01050366 -0.01454147 ... 0.48705393 -0.54457957
Explanation: The output shows different word embeddings for the same word WATCH used in a different context in different sentences.