-
Notifications
You must be signed in to change notification settings - Fork 2
/
losses.py
157 lines (115 loc) · 4.23 KB
/
losses.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
import torch
'''
loss functions
'''
def loss_bce(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert not torch.any(observed_labels == -1)
assert P['train_set_variant'] == 'clean'
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = neg_log(1.0 - preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_iun(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true']
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[true_labels == 0] = neg_log(1.0 - preds[true_labels == 0]) # FIXME
reg_loss = None
return loss_mtx, reg_loss
def loss_an(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true'].to(Z['device'])
# input validation:
assert torch.min(observed_labels) >= 0
# compute loss:
loss_mtx = torch.zeros_like(observed_labels)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = neg_log(1.0 - preds[observed_labels == 0])
reg_loss = None
return loss_mtx, reg_loss
def loss_EM(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
true_labels = batch['label_vec_true'].to(Z['device'])
# input validation:
assert torch.min(observed_labels) >= 0
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
return loss_mtx, None
def loss_EM_APL(batch, P, Z):
# unpack:
preds = batch['preds']
observed_labels = batch['label_vec_obs']
# input validation:
assert torch.min(observed_labels) >= -1
loss_mtx = torch.zeros_like(preds)
loss_mtx[observed_labels == 1] = neg_log(preds[observed_labels == 1])
loss_mtx[observed_labels == 0] = -P['alpha'] * (
preds[observed_labels == 0] * neg_log(preds[observed_labels == 0]) +
(1 - preds[observed_labels == 0]) * neg_log(1 - preds[observed_labels == 0])
)
soft_label = -observed_labels[observed_labels < 0]
loss_mtx[observed_labels < 0] = P['beta'] * (
soft_label * neg_log(preds[observed_labels < 0]) +
(1 - soft_label) * neg_log(1 - preds[observed_labels < 0])
)
return loss_mtx, None
loss_functions = {
'bce': loss_bce,
'iun': loss_iun,
'an': loss_an,
'EM': loss_EM,
'EM_APL': loss_EM_APL,
}
'''
top-level wrapper
'''
def compute_batch_loss(batch, P, Z):
assert batch['preds'].dim() == 2
batch_size = int(batch['preds'].size(0))
num_classes = int(batch['preds'].size(1))
loss_denom_mtx = (num_classes * batch_size) * torch.ones_like(batch['preds'])
# input validation:
assert torch.max(batch['label_vec_obs']) <= 1
assert torch.min(batch['label_vec_obs']) >= -1
assert batch['preds'].size() == batch['label_vec_obs'].size()
assert P['loss'] in loss_functions
# validate predictions:
assert torch.max(batch['preds']) <= 1
assert torch.min(batch['preds']) >= 0
# compute loss for each image and class:
loss_mtx, reg_loss = loss_functions[P['loss']](batch, P, Z)
main_loss = (loss_mtx / loss_denom_mtx).sum()
if reg_loss is not None:
batch['loss_tensor'] = main_loss + reg_loss
batch['reg_loss_np'] = reg_loss.clone().detach().cpu().numpy()
else:
batch['loss_tensor'] = main_loss
batch['reg_loss_np'] = 0.0
batch['loss_np'] = batch['loss_tensor'].clone().detach().cpu().numpy()
return batch
'''
helper functions
'''
LOG_EPSILON = 1e-5
def neg_log(x):
return - torch.log(x + LOG_EPSILON)