Skip to content

Commit 96b92be

Browse files
authored
Merge pull request #804 from Aniloid2/patch-1
(Bug) Update sentence_encoder.py: clamping cos_sim between -1 and 1 to avoid floating point precision errors in torch.acos(cos_sim)
2 parents 4c5b8dc + a995862 commit 96b92be

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)