-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclr.py
42 lines (31 loc) · 1.18 KB
/
clr.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
"""
@author: xi
"""
import torch
from torch import nn
from torch.nn import functional as F
class ProjectionHead(nn.Module):
def __init__(self, emb_size, head_size):
super(ProjectionHead, self).__init__()
self.hidden = nn.Linear(emb_size, emb_size)
self.out = nn.Linear(emb_size, head_size)
def forward(self, h: torch.Tensor) -> torch.Tensor:
h = self.hidden(h)
h = F.relu_(h)
h = self.out(h)
return h
def nt_xent_loss(z1: torch.Tensor, z2: torch.Tensor, t=0.3, eps=1e-10) -> torch.Tensor:
batch_size = z1.shape[0]
assert batch_size == z2.shape[0]
assert batch_size > 1
# compute the similarity matrix
# values in the diagonal elements represent the similarity between the (POS, POS) pairs
# while the other values are the similarity between the (POS, NEG) pairs
z1 = F.normalize(z1, dim=1)
z2 = F.normalize(z2, dim=1)
sim_mat = z1 @ z2.T
scaled_prob_mat = F.softmax(sim_mat / t, dim=1)
# construct a cross-entropy loss to maximize the probability of the (POS, POS) pairs
log_prob = torch.log(scaled_prob_mat + eps)
return -torch.diagonal(log_prob).mean()