princeton-nlp / SimCSE

[EMNLP 2021] SimCSE: Simple Contrastive Learning of Sentence Embeddings https://arxiv.org/abs/2104.08821

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why divide by temp when calculating cosine similarity

FinalFlowers opened this issue · comments

`class Similarity(nn.Module):
"""
Dot product or cosine similarity
"""

def __init__(self, temp):
    super().__init__()
    self.temp = temp
    self.cos = nn.CosineSimilarity(dim=-1)

def forward(self, x, y):
    return self.cos(x, y) / self.temp`

Hi, please check our paper for details. Temperature is one of our hyperparameters and we use it to adjust how "sharp" the distribution is.