22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

We can leverage this fact to slightly modify the get_embeddings() function so it

works with both word and document embeddings:

Helper Function to Retrieve Embeddings

1 def get_embeddings(embeddings, sentence):

2 sent = Sentence(sentence)

3 embeddings.embed(sent)

4 if len(sent.embedding):

5 return sent.embedding.float()

6 else:

7 return torch.stack(

8 [token.embedding for token in sent.tokens]

9 ).float()

get_embeddings(bert_doc, watch1)

Output

tensor([-6.4245e-02, 3.5365e-01, -2.4962e-01, -5.3912e-01,

-1.9917e-01, -2.7712e-01, 1.6942e-01, 1.0867e-01,

...

7.4661e-02, -3.4777e-01, 1.5740e-01, 3.4407e-01,

-5.0272e-01, 1.7432e-01, 7.9398e-01, 7.3562e-01],

device='cuda:0',

grad_fn=<CatBackward>)

For more details on document embeddings, please check

"Tutorial 5: Document Embeddings." [198]

We can revisit the Sequential model from the "Word Embeddings" section and

modify it to use contextual word embeddings instead. But, first, we need to change

the datasets a bit as well.

Contextual Word Embeddings | 961

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

Saved successfully!

Ooh no, something went wrong!