-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloss.py
94 lines (74 loc) · 3.3 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import torch
import torch.nn as nn
import pandas as pd
import numpy as np
class NT_Xent(nn.Module):
"""NT_Xent loss for simclr."""
def __init__(self, batch_size, temperature=1):
super(NT_Xent, self).__init__()
self.batch_size = batch_size
self.temperature = temperature
self.mask = self.mask_correlated_samples(batch_size)
self.criterion = nn.CrossEntropyLoss(reduction="sum")
self.similarity_f = nn.CosineSimilarity(dim=2)
def mask_correlated_samples(self, batch_size):
"""Mask correlated samples.
:param batch_size: batch size of the dataset
:type batch_size: int
"""
N = 2 * batch_size
mask = torch.ones((N, N), dtype=bool)
mask = mask.fill_diagonal_(0)
for i in range(batch_size):
mask[i, batch_size + i] = 0
mask[batch_size + i, i] = 0
return mask
def forward(self, z_i, z_j):
"""Calculate the compare loss."""
N = 2 * self.batch_size
z = torch.cat((z_i, z_j), dim=0)
sim = self.similarity_f(z.unsqueeze(1), z.unsqueeze(0)) / self.temperature
sim_i_j = torch.diag(sim, self.batch_size)
sim_j_i = torch.diag(sim, -self.batch_size)
positive_samples = torch.cat((sim_i_j, sim_j_i), dim=0).reshape(N, 1)
negative_samples = sim[self.mask].reshape(N, -1)
labels = torch.zeros(N).to(positive_samples.device).long()
logits = torch.cat((positive_samples, negative_samples), dim=1)
loss = self.criterion(logits, labels)
loss /= N
return loss
SMALL_NUM = np.log(1e-45)
class DCL(object):
"""
Decoupled Contrastive Loss proposed in https://arxiv.org/pdf/2110.06848.pdf
weight: the weighting function of the positive sample loss
temperature: temperature to control the sharpness of the distribution
"""
def __init__(self, temperature=1, weight_fn=None):
super(DCL, self).__init__()
self.temperature = temperature
self.weight_fn = weight_fn
def __call__(self, z1, z2):
"""
Calculate one way DCL loss
:param z1: first embedding vector
:param z2: second embedding vector
:return: one-way loss
"""
cross_view_distance = torch.mm(z1, z2.t())
positive_loss = -torch.diag(cross_view_distance) / self.temperature
if self.weight_fn is not None:
positive_loss = positive_loss * self.weight_fn(z1, z2)
neg_similarity = torch.cat((torch.mm(z1, z1.t()), cross_view_distance), dim=1) / self.temperature
neg_mask = torch.eye(z1.size(0), device=z1.device).repeat(1, 2)
negative_loss = torch.logsumexp(neg_similarity + neg_mask * SMALL_NUM, dim=1, keepdim=False)
return (positive_loss + negative_loss).mean()
class DCLW(DCL):
"""
Decoupled Contrastive Loss with negative von Mises-Fisher weighting proposed in https://arxiv.org/pdf/2110.06848.pdf
sigma: the weighting function of the positive sample loss
temperature: temperature to control the sharpness of the distribution
"""
def __init__(self, sigma=0.5, temperature=1):
weight_fn = lambda z1, z2: 2 - z1.size(0) * torch.nn.functional.softmax((z1 * z2).sum(dim=1) / sigma, dim=0).squeeze()
super(DCLW, self).__init__(weight_fn=weight_fn, temperature=temperature)