Classic word embeddings in Flair
The simplest type of embedding in Flair that still contains semantic information about the word is called classic word embeddings. These embeddings are pre-trained and non-contextual and require no preprocessing. The non-contextual nature means one word always maps to one precomputed embedding regardless of the context. To use them, we simply instantiate a WordEmbeddings
class by passing in the ID of the embedding of our choice. Then, we simply wrap our text into a Sentence
object and call the embed(sentence)
method on our WordEmbeddings
class.
Let's obtain embeddings for a few random words using the FastText "crawl" embeddings:
from flair.data import Sentence from flair.embeddings import WordEmbeddings embedding = WordEmbeddings('crawl') sentence = Sentence("one two three one") embedding.embed(sentence) for token in sentence: print(token.embedding)
The preceding code will print out...