Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vectorization and Broadcasting with Pytorch
import torch
from torch.nn.functional import cosine_similarity
def embeddings_to_cosine_similarity_matrix(E):
"""
Converts a a tensor of n embeddings to an (n, n) tensor of similarities.
"""
similarities = [[cosine_similarity(a, b, dim=0) for a in E] for b in E]
similarities = list(map(torch.cat, similarities))
return torch.stack(similarities)
import torch
from torch.nn.functional import cosine_similarity
def embeddings_to_cosine_similarity_matrix(E):
"""
Converts a a tensor of n embeddings to an (n, n) tensor of similarities.
"""
dot = E @ E.t()
norm = torch.norm(E, 2, 1)
x = torch.div(dot, norm)
x = torch.div(x, torch.unsqueeze(norm, 0))
return x
E = torch.randn(20000, 100)
embeddings_to_cosine_similarity_matrix(E)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.