-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_functions.py
177 lines (152 loc) · 5.86 KB
/
loss_functions.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
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn import MSELoss
import utils
import math
def loss_func_joint_training(output, targets, separate=False):
losses, costs = [], []
loss_func = nn.CrossEntropyLoss()
for idx, o in enumerate(output):
prediction, cost = o[0], o[1]
loss = cost * loss_func(prediction, targets)
costs.append(cost)
losses.append(loss)
if separate:
return losses
else:
# to compensate for magnitude
return sum(losses)/sum(costs)
def loss_func_exits(output, targets):
two_exit_losses = []
loss_func = nn.CrossEntropyLoss()
for idx, prediction in enumerate(output):
loss = loss_func(prediction, targets)
two_exit_losses.append(loss)
return two_exit_losses
def dist_exits_loss_func(output, targets, alpha):
MSEloss = nn.MSELoss(reduction='mean').to(device=utils.get_device())
loss_func = nn.CrossEntropyLoss()
first_exit_pred = output[0]
second_exit_pred = output[1]
teacher_pred = output[2]
teacher_pred = teacher_pred.detach()
# classification loss calculation
classification_loss_first = loss_func(first_exit_pred, targets)
classification_loss_second = loss_func(second_exit_pred, targets)
# distillation loss calculation
dist_loss_first = MSEloss(first_exit_pred, teacher_pred)
dist_loss_second = MSEloss(second_exit_pred, teacher_pred)
# final loss calculation
final_loss_first = alpha * dist_loss_first + (1 - alpha) * classification_loss_first
final_loss_second = alpha * dist_loss_second + (1 - alpha) * classification_loss_second
loss = [final_loss_first, final_loss_second]
return loss
def loss_func_joint_distillation(output, targets, alpha, separate=False):
MSEloss = nn.MSELoss(reduction='mean').to(device=utils.get_device())
classification_loss = 0
predictions = []
costs = []
losses = []
num_exits = len(output)
# classification loss calculation
loss_func = nn.CrossEntropyLoss()
for o in output:
prediction, cost = o[0], o[1]
predictions.append(prediction)
costs.append(cost)
# print(prediction.shape)
loss = loss_func(prediction, targets)
losses.append(loss)
loss = cost * loss
classification_loss += loss
avg_cost_to_normalize = sum(costs) / len(costs)
classification_loss /= avg_cost_to_normalize
target_output = predictions[0].detach()
dist_loss_second = MSEloss(predictions[1], target_output)
dist_loss_first = MSEloss(predictions[2], target_output)
distillation_loss = dist_loss_first + dist_loss_second
final_loss = (1 - alpha) * classification_loss + (alpha) * distillation_loss
if separate == True:
return losses
return final_loss
def loss_func_exit_ensemble(output, targets, alpha=1, gamma=1, lamda=1, separate=False):
MSEloss = nn.MSELoss(reduction='mean').to(device=utils.get_device())
classification_loss = 0
distillation_loss = 0
lambdas = []
losses = []
predictions = []
costs = []
num_exits = len(output)
for i in range(1, num_exits+1):
lambdas.append(pow(lamda, num_exits-i))
# classification loss calculation
loss_func = nn.CrossEntropyLoss()
for idx, o in enumerate(output):
prediction, cost = o[0], o[1]
predictions.append(prediction)
costs.append(cost)
# print(prediction.shape)
loss = cost * loss_func(prediction, targets)
losses.append(loss)
balancing_constant = 1 + alpha - pow(gamma, idx)
classification_loss += loss * balancing_constant
# cum_loss = cum_loss/num_exits
avg_cost_to_normalize = sum(costs) / len(costs)
classification_loss /= avg_cost_to_normalize
# ensemble
target_output = ((lambdas[0] * predictions[0]/3 + lambdas[1] *
predictions[1]/3 + lambdas[2] * predictions[2]/3)/sum(lambdas)).detach()
temp_loss = MSEloss(predictions[0], target_output)
losses[0] += temp_loss
distillation_loss += temp_loss * pow(gamma, 0)
temp_loss = MSEloss(predictions[1], target_output)
losses[1] += temp_loss
distillation_loss += temp_loss * pow(gamma, 1)
temp_loss = MSEloss(predictions[2], target_output)
losses[2] += temp_loss
distillation_loss += temp_loss * pow(gamma, 2)
final_loss = classification_loss + distillation_loss
if separate == True:
return losses
return final_loss
# def loss_func_exit_ensemble_not_weighted(output, targets, alpha=1, gamma=1.15, lamda=1.6, separate=False):
# MSEloss = nn.MSELoss(reduction='mean').to(device=utils.get_device())
# classification_loss = 0
# distillation_loss = 0
# losses = []
# predictions = []
# costs = []
# # classification loss calculation
# loss_func = nn.CrossEntropyLoss()
# for idx, o in enumerate(output):
# prediction, cost = o[0], o[1]
# predictions.append(prediction)
# costs.append(cost)
# # print(prediction.shape)
# loss = cost * loss_func(prediction, targets)
# losses.append(loss)
# classification_loss += loss
# # cum_loss = cum_loss/num_exits
# avg_cost_to_normalize = sum(costs) / len(costs)
# classification_loss /= avg_cost_to_normalize
#
#
# # ensemble
# target_output = (predictions[0]/3 + predictions[1]/3 + predictions[2]/3).detach()
#
# temp_loss = MSEloss(predictions[0], target_output)
# losses[0] += temp_loss
# distillation_loss += temp_loss
# temp_loss = MSEloss(predictions[1], target_output)
# losses[1] += temp_loss
# distillation_loss += temp_loss
# temp_loss = MSEloss(predictions[2], target_output)
# losses[2] += temp_loss
# distillation_loss += temp_loss
# final_loss = (1-alpha) * classification_loss + alpha * distillation_loss
#
# if separate == True:
# return losses
# return final_loss