-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_loss.py
148 lines (118 loc) · 4.29 KB
/
get_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<<<<<<< HEAD
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.functional as F
class ContrastiveLoss(nn.Module):
def __init__(self, temperature):
super(ContrastiveLoss, self).__init__()
self.temperature = temperature
def forward(self, z_i, z_j):
batch_size = z_i.shape[0]
# Concatenate the two sets of embeddings
z = torch.cat((z_i, z_j), dim=0)
# Calculate the cosine similarity matrix
sim_matrix = F.cosine_similarity(z.unsqueeze(1), z.unsqueeze(0), dim=2)
# Scale the similarity matrix by the temperature
sim_matrix = sim_matrix / self.temperature
# Create labels for the positive pairs
labels = torch.arange(batch_size).to(z.device)
labels = torch.cat((labels, labels), dim=0)
labels[:batch_size] = labels[:batch_size]+batch_size-1
# Create a mask to remove self-similarities
mask = torch.eye(batch_size * 2, dtype=torch.bool).to(z.device)
sim_matrix = sim_matrix[~mask].view(batch_size * 2, -1)
# Calculate the cross-entropy loss
loss = F.cross_entropy(sim_matrix, labels)
return loss
criterion = ContrastiveLoss(temperature=0.5)
def get_loss(loss_name,**args):
if loss_name == 'cross_entropy':
criterion = nn.CrossEntropyLoss()
elif loss_name == 'mse':
criterion = nn.MSELoss()
elif loss_name == 'l1':
criterion = nn.L1Loss()
elif loss_name == 'ContrastLoss':
if 'temperature' in args:
temperature = args['temperature']
else:
temperature = 0.5
criterion = ContrastiveLoss(temperature)
else:
raise NotImplementedError
return criterion
if __name__ == '__main__':
batch_size = 4
embedding_dim = 128
temperature = 0.5
z_i = torch.randn(batch_size, embedding_dim, requires_grad=True)
z_j = z_i
criterion = get_loss("ContrastLoss")
loss = criterion(z_i, z_j)
print('Loss:', loss.item())
# Backward pass
# loss.backward()
# print('Gradient for z_i:', z_i.grad)
=======
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.functional as F
class ContrastiveLoss(nn.Module):
def __init__(self, temperature):
super(ContrastiveLoss, self).__init__()
self.temperature = temperature
def forward(self, z_i, z_j):
batch_size = z_i.shape[0]
# Concatenate the two sets of embeddings
z = torch.cat((z_i, z_j), dim=0)
# Calculate the cosine similarity matrix
sim_matrix = F.cosine_similarity(z.unsqueeze(1), z.unsqueeze(0), dim=2)
# Scale the similarity matrix by the temperature
sim_matrix = sim_matrix / self.temperature
# Create labels for the positive pairs
labels = torch.arange(batch_size).to(z.device)
labels = torch.cat((labels, labels), dim=0)
labels[:batch_size] = labels[:batch_size]+batch_size-1
# Create a mask to remove self-similarities
mask = torch.eye(batch_size * 2, dtype=torch.bool).to(z.device)
sim_matrix = sim_matrix[~mask].view(batch_size * 2, -1)
# Calculate the cross-entropy loss
loss = F.cross_entropy(sim_matrix, labels)
return loss
criterion = ContrastiveLoss(temperature=0.5)
def get_loss(loss_name,args):
if loss_name == 'cross_entropy':
criterion = nn.CrossEntropyLoss()
elif loss_name == 'mse':
criterion = nn.MSELoss()
elif loss_name == 'l1':
criterion = nn.L1Loss()
elif loss_name == 'ContrastLoss':
if 'temperature' in args:
temperature = args['temperature']
else:
temperature = 0.5
criterion = ContrastiveLoss(temperature)
else:
raise NotImplementedError
return criterion
if __name__ == '__main__':
batch_size = 4
embedding_dim = 128
temperature = 0.5
z_i = torch.randn(batch_size, embedding_dim, requires_grad=True)
z_j = z_i
criterion = ContrastiveLoss(temperature)
loss = criterion(z_i, z_j)
print('Loss:', loss.item())
# Backward pass
# loss.backward()
# print('Gradient for z_i:', z_i.grad)
>>>>>>> 667564aaac8d2a50880224169a6e0fb2176d2014
# print('Gradient for z_j:', z_j.grad)