-
Notifications
You must be signed in to change notification settings - Fork 657
/
Copy pathntxent_loss.py
28 lines (20 loc) · 1.05 KB
/
ntxent_loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
from .generic_pair_loss import GenericPairLoss
class NTXentLoss(GenericPairLoss):
def __init__(self, temperature, **kwargs):
super().__init__(**kwargs, use_similarity=True, mat_based_loss=False)
self.temperature = temperature
def _compute_loss(self, pos_pairs, neg_pairs, indices_tuple):
a1, _, a2, _ = indices_tuple
if len(a1) > 0 and len(a2) > 0:
pos_pairs = pos_pairs.unsqueeze(1) / self.temperature
neg_pairs = neg_pairs / self.temperature
n_per_p = (a2.unsqueeze(0) == a1.unsqueeze(1)).float()
neg_pairs = neg_pairs*n_per_p
neg_pairs[n_per_p==0] = float('-inf')
max_val = torch.max(pos_pairs, torch.max(neg_pairs, dim=1, keepdim=True)[0])
numerator = torch.exp(pos_pairs - max_val).squeeze(1)
denominator = torch.sum(torch.exp(neg_pairs - max_val), dim=1) + numerator
log_exp = torch.log((numerator/denominator) + 1e-20)
return torch.mean(-log_exp)
return 0