Skip to content

Commit a995862

Browse files
authored
(Bug) Update sentence_encoder.py
If we compare two equal embeddings, emb1 == emb2, the cosine similarity should be 1. However, due to floating point precision, we might end up with a value slightly greater than 1, such as 1.00004. This results in an undefined NaN in torch.acos(cos_sim), causing get_angular_sim to return NaN instead of 1. By using cos_sim = torch.clamp(cos_sim, -1.0, 1.0), we ensure that the cos_sim value remains within the valid range expected by torch.acos(cos_sim).
1 parent 5fbb076 commit a995862

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

textattack/constraints/semantics/sentence_encoders/sentence_encoder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def get_angular_sim(emb1, emb2):
215215
"""Returns the _angular_ similarity between a batch of vector and a batch
216216
of vectors."""
217217
cos_sim = torch.nn.CosineSimilarity(dim=1)(emb1, emb2)
218+
cos_sim = torch.clamp(cos_sim, -1.0, 1.0)
218219
return 1 - (torch.acos(cos_sim) / math.pi)
219220

220221

0 commit comments

Comments
 (0)