I want to find the word embedding using BERT for a single word in a sentence. Anyone has any idea how to do it?

Viewed 521

For example in the sentence: "Earth revolves around the sun and rotate around its own axis", I want to embed it with BERT word embeddings and find the embedding for "rotate" in this particular sentence. How to do it?

1 Answers

flair has made this quite easy. First install the library using

!pip install flair

then using the below code you can get the embedding for every token in a sentence AKA context:

from flair.data import Sentence
s = Sentence('the dummy sentence to be embedded')

from flair.embeddings import BertEmbeddings
bert_embedding = BertEmbeddings()
bert_embedding.embed(s)

for token in s:
   print(token.embedding)
Related