forked from rfelixmg/pytorch.repmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repmet_loss.py
executable file
·244 lines (177 loc) · 9 KB
/
repmet_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import torch
import torch.nn.functional as F
import torch.nn as nn
from loss import Loss
from utils import ensure_tensor, ensure_numpy
class RepMetLoss(Loss):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def update_clusters(self, set_x, max_iter=20):
"""
Given an array of representations for the entire training set,
recompute clusters and store example cluster assignments in a
quickly sampleable form.
"""
if self.centroids is None:
# make leaf variable after editing it then wrap in param
self.centroids = nn.Parameter(torch.zeros((self.num_classes * self.k, set_x.shape[1]), requires_grad=True).cuda())
super().update_clusters(set_x, max_iter=max_iter)
def loss(self, x, y):
"""Compute repmet loss.
Given a tensor of features `x`, the assigned class for each example,
compute the repmet loss according to equations (5) in
https://arxiv.org/pdf/1806.04728.pdf.
Args:
x: A batch of features.
y: Class labels for each example.
Returns:
total_loss: The total magnet loss for the batch.
losses: The loss for each example in the batch.
acc: The predictive accuracy of the batch
"""
# Compute distance of each example to each cluster centroid (euclid without the root)
distances = self.calculate_distance(self.centroids, x)
# Compute the two masks selecting the sample_costs related to each class(r)=class(cluster/rep)
intra_cluster_mask = self.comparison_mask(y, torch.from_numpy(self.cluster_classes).cuda())
intra_cluster_mask_op = ~intra_cluster_mask
# Calculate the minimum distance to rep of same class
# therefore we make all values for other clusters bigger so they are ignored..
intra_cluster_costs_ignore = (intra_cluster_mask_op.float() * (distances.max() * 1.5))
intra_cluster_costs_desired = intra_cluster_mask.float() * distances
intra_cluster_costs_together = intra_cluster_costs_ignore + intra_cluster_costs_desired
min_match, _ = intra_cluster_costs_together.min(1)
d = ensure_numpy(distances)
t = ensure_numpy(intra_cluster_costs_ignore)
tt = ensure_numpy(intra_cluster_costs_desired)
ttt = ensure_numpy(intra_cluster_costs_together)
tttt = ensure_numpy(min_match)
# Compute variance of intra-cluster distances
# N = x.shape[0]
# variance = min_match.sum() / float((N - 1))
variance = 0.5 # hard code 0.5 [as suggested in paper]
var_normalizer = -1 / (2 * variance**2)
if not self.avg_variance:
self.avg_variance = variance
else:
self.avg_variance = (self.avg_variance + variance) / 2
# Compute numerator
numerator = torch.exp(var_normalizer * min_match)
# Compute denominator
diff_class_mask = intra_cluster_mask_op.float()
denom_sample_costs = torch.exp(var_normalizer * distances)
denominator = (diff_class_mask.cuda() * denom_sample_costs).sum(1)
# Compute example losses and total loss
epsilon = 1e-8
# Compute example losses and total loss
losses = F.relu(-torch.log(numerator / (denominator + epsilon) + epsilon) + self.alpha)
total_loss = losses.mean()
_, preds = distances.min(1)
preds = ensure_tensor(self.cluster_classes[preds]).cuda() # convert from cluster ids to class ids
acc = torch.eq(y, preds).float().mean()
return total_loss, losses, acc
# Don't only take min of class in numerator and use all in denominator
class RepMetLoss2(Loss):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def update_clusters(self, set_x, max_iter=20):
"""
Given an array of representations for the entire training set,
recompute clusters and store example cluster assignments in a
quickly sampleable form.
"""
if self.centroids is None:
# make leaf variable after editing it then wrap in param
self.centroids = nn.Parameter(torch.zeros((self.num_classes * self.k, set_x.shape[1]), requires_grad=True).cuda())
super().update_clusters(set_x, max_iter=max_iter)
def loss(self, x, y):
"""Compute repmet loss.
Given a tensor of features `x`, the assigned class for each example,
compute the repmet loss version 2 see readme of details.
Args:
x: A batch of features.
y: Class labels for each example.
Returns:
total_loss: The total magnet loss for the batch.
losses: The loss for each example in the batch.
acc: The predictive accuracy of the batch
"""
# Compute distance of each example to each cluster centroid (euclid without the root)
distances = self.calculate_distance(self.centroids, x)
# Compute the mask selecting the distances related to each class(r)=class(cluster/rep)
intra_cluster_mask = self.comparison_mask(y, torch.from_numpy(self.cluster_classes).cuda())
# Compute variance of intra-cluster distances
# N = x.shape[0]
# variance = min_match.sum() / float((N - 1))
variance = 0.5 # hard code 0.5 [as suggested in paper]
var_normalizer = -1 / (2 * variance**2)
if not self.avg_variance:
self.avg_variance = variance
else:
self.avg_variance = (self.avg_variance + variance) / 2
# Compute numerator
numerator_pre_mask = torch.exp(var_normalizer * distances)
numerator = (intra_cluster_mask.float() * numerator_pre_mask).sum(1)
# Compute denominator
denominator = numerator_pre_mask.sum(1)
# Compute example losses and total loss
epsilon = 1e-8
# Compute example losses and total loss
losses = F.relu(-torch.log(numerator / (denominator + epsilon) + epsilon) + self.alpha)
total_loss = losses.mean()
_, preds = distances.min(1)
preds = ensure_tensor(self.cluster_classes[preds]).cuda() # convert from cluster ids to class ids
acc = torch.eq(y, preds).float().mean()
return total_loss, losses, acc
# Don't only take min of class in numerator, still exclude from denominator though
class RepMetLoss3(Loss):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def update_clusters(self, set_x, max_iter=20):
"""
Given an array of representations for the entire training set,
recompute clusters and store example cluster assignments in a
quickly sampleable form.
"""
if self.centroids is None:
# make leaf variable after editing it then wrap in param
self.centroids = nn.Parameter(torch.zeros((self.num_classes * self.k, set_x.shape[1]), requires_grad=True).cuda())
super().update_clusters(set_x, max_iter=max_iter)
def loss(self, x, y):
"""Compute repmet loss.
Given a tensor of features `x`, the assigned class for each example,
compute the repmet loss version 2 see readme of details.
Args:
x: A batch of features.
y: Class labels for each example.
Returns:
total_loss: The total magnet loss for the batch.
losses: The loss for each example in the batch.
acc: The predictive accuracy of the batch
"""
# Compute distance of each example to each cluster centroid (euclid without the root)
distances = self.calculate_distance(self.centroids, x)
# Compute the two masks selecting the sample_costs related to each class(r)=class(cluster/rep)
intra_cluster_mask = self.comparison_mask(y, torch.from_numpy(self.cluster_classes).cuda())
# Compute variance of intra-cluster distances
# N = x.shape[0]
# variance = min_match.sum() / float((N - 1))
variance = 0.5 # hard code 0.5 [as suggested in paper] but seems to now work as well as the calculated variance in my exp
var_normalizer = -1 / (2 * variance**2)
if not self.avg_variance:
self.avg_variance = variance
else:
self.avg_variance = (self.avg_variance + variance) / 2
# Compute numerator
numerator_pre_mask = torch.exp(var_normalizer * distances)
numerator = (intra_cluster_mask.float() * numerator_pre_mask).sum(1)
# Compute denominator
denominator = numerator_pre_mask.sum(1)
# Compute example losses and total loss
epsilon = 1e-8
# Compute example losses and total loss
losses = F.relu(-torch.log(numerator / (denominator - numerator + epsilon) + epsilon) + self.alpha)
total_loss = losses.mean()
_, preds = distances.min(1)
preds = ensure_tensor(self.cluster_classes[preds]).cuda() # convert from cluster ids to class ids
acc = torch.eq(y, preds).float().mean()
return total_loss, losses, acc