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
Word embeddings for word WATCH in first sentence [ 0.14079645 -0.15788531 -0.00950466 ... 0.4300597 -0.52887094 0.06327899] Word embeddings for word WATCH in second sentence [-0.08213335 0.01050366 -0.01454147 ... 0.48705393 -0.54457957 0.5262399 ]
Explanation: The output shows different word embeddings for the same word WATCH used in a different context in different sentences.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course