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.

Well, they’re not quite embeddings, but at least we can use cosine similarity to find

out how similar to each other two restaurants are:

ratings = torch.as_tensor([[.7, -.4, .7],

[.3, .7, -.5],

[.9, -.55, .8],

[-.3, .8, .34]]).float()

sims = torch.zeros(4, 4)

for i in range(4):

for j in range(4):

sims[i, j] = F.cosine_similarity(ratings[i],

ratings[j],

dim=0)

sims

Output

tensor([[ 1.0000, -0.4318, 0.9976, -0.2974],

[-0.4318, 1.0000, -0.4270, 0.3581],

[ 0.9976, -0.4270, 1.0000, -0.3598],

[-0.2974, 0.3581, -0.3598, 1.0000]])

As expected, restaurants #1 and #3 are remarkably similar (0.9976), and

restaurants #2 and #4 are somewhat similar (0.3581). Restaurant #1 is quite

different from restaurants #2 and #4 (-0.4318 and -0.2974, respectively), and so is

restaurant #3 (-0.4270 and -0.3598, respectively).

Although we can compute the cosine similarity between two restaurants now, the

values in the table above are not real embeddings. It was only an example that

illustrates well the concept of embedding dimensions as attributes.

Unfortunately, the dimensions of the word embeddings learned

by the Word2Vec model do not have clear-cut meanings like

that.

On the bright side, though, it is possible to do arithmetic with

word embeddings!

"Say what?"

926 | 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!