Skip to content

Instantly share code, notes, and snippets.

@laifi
Created July 29, 2019 09:58
Show Gist options
  • Save laifi/b947aa910dcfc873fa1aeb81014889fe to your computer and use it in GitHub Desktop.
Save laifi/b947aa910dcfc873fa1aeb81014889fe to your computer and use it in GitHub Desktop.
Cosine Similarity Pytorch
def cosine_distance(x1, x2=None, eps=1e-8):
x2 = x1 if x2 is None else x2
w1 = x1.norm(p=2, dim=1, keepdim=True)
w2 = w1 if x2 is x1 else x2.norm(p=2, dim=1, keepdim=True)
return 1 - torch.mm(x1, x2.t()) / (w1 * w2.t()).clamp(min=eps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment