22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

GloVe

GloVe embeddings are not contextual, as you already know, but they can

also be easily retrieved using WordEmbeddings from flair:

from flair.embeddings import WordEmbeddings

glove_embedding = WordEmbeddings('glove')

Now, let’s retrieve the word embeddings for our sentences, but first, and this

is very important, we need to create new Sentence objects for them:

new_flair_sentences = [Sentence(s) for s in sentences]

glove_embedding.embed(new_flair_sentences)

Output

[Sentence: "The Hatter was the first to break the silence . `

What day of the month is it ? ' he said , turning to Alice :

he had taken his watch out of his pocket , and was looking at

it uneasily , shaking it every now and then , and holding it

to his ear ." [ Tokens: 58],

Sentence: "Alice thought this a very curious thing , and she

went nearer to watch them , and just as she came up to them

she heard one of them say , ` Look out now , Five ! Do n't go

splashing paint over me like that !" [ Tokens: 48]]

Never reuse a Sentence object to retrieve different word

embeddings! The embedding attribute may be partially

overwritten (depending on the number of dimensions), and

you may end up with mixed embeddings (e.g., 3,072

dimensions from ELMo, but the first 100 values are

overwritten by GloVe embeddings).

Since GloVe is not contextual, the word "watch" will have the same

embedding regardless of which sentence you retrieve it from:

956 | Chapter 11: Down the Yellow Brick Rabbit Hole

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!