-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathlosses.py
282 lines (229 loc) · 12.3 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from monk.gluon.losses.imports import *
from monk.system.imports import *
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def l1(system_dict, weight=None, batch_axis=0):
'''
Select L1 Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "l1";
system_dict["hyper-parameters"]["loss"]["name"] = "l1";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def l2(system_dict, weight=1.0, batch_axis=0):
'''
Select L2 Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "l2";
system_dict["hyper-parameters"]["loss"]["name"] = "l2";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int,
axis_to_sum_over=int, label_as_categories=bool, label_smoothing=bool, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def softmax_crossentropy(system_dict, weight=None, batch_axis=0, axis_to_sum_over=-1,
label_as_categories=True, label_smoothing=False):
'''
Select softmax crossentropy Loss - Auto softmax before applying loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
axis_to_sum_over (int): Set as -1
label_as_categories (bool): Fixed as True
label_smoothing (bool): If True, label smoothning is applied.
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "softmaxcrossentropy";
system_dict["hyper-parameters"]["loss"]["name"] = "softmaxcrossentropy";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["loss"]["params"]["axis_to_sum_over"] = axis_to_sum_over;
system_dict["hyper-parameters"]["loss"]["params"]["label_as_categories"] = label_as_categories;
system_dict["hyper-parameters"]["loss"]["params"]["label_smoothing"] = label_smoothing;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int,
axis_to_sum_over=int, label_as_categories=bool, label_smoothing=bool, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def crossentropy(system_dict, weight=None, batch_axis=0, axis_to_sum_over=-1,
label_as_categories=True, label_smoothing=False):
'''
Select crossentropy Loss - Need to manually apply softmax
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
axis_to_sum_over (int): Set as -1
label_as_categories (bool): Fixed as True
label_smoothing (bool): If True, label smoothning is applied.
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "crossentropy";
system_dict["hyper-parameters"]["loss"]["name"] = "crossentropy";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["loss"]["params"]["axis_to_sum_over"] = axis_to_sum_over;
system_dict["hyper-parameters"]["loss"]["params"]["label_as_categories"] = label_as_categories;
system_dict["hyper-parameters"]["loss"]["params"]["label_smoothing"] = label_smoothing;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def sigmoid_binary_crossentropy(system_dict, weight=None, batch_axis=0):
'''
Select sigmoid binary crossentropy Loss - Auto sigmoid before applying loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "sigmoidbinarycrossentropy";
system_dict["hyper-parameters"]["loss"]["name"] = "sigmoidbinarycrossentropy";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def binary_crossentropy(system_dict, weight=None, batch_axis=0):
'''
Select binary crossentropy Loss - Need to manually apply sigmoid
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "binarycrossentropy";
system_dict["hyper-parameters"]["loss"]["name"] = "binarycrossentropy";
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, log_pre_applied=bool, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int,
axis_to_sum_over=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def kldiv(system_dict, log_pre_applied=False, weight=None, batch_axis=0, axis_to_sum_over=-1):
'''
Select lkdiv Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
axis_to_sum_over (int): Set as -1
log_pre_applied (bool): If set as False, then logarithmic function is auto applied over target variables
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "kldiv";
system_dict["hyper-parameters"]["loss"]["name"] = "kldiv";
system_dict["hyper-parameters"]["loss"]["params"]["log_pre_applied"] = log_pre_applied;
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["loss"]["params"]["axis_to_sum_over"] = axis_to_sum_over;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, log_pre_applied=bool, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def poisson_nll(system_dict, log_pre_applied=False, weight=None, batch_axis=0):
'''
Select poisson_nll Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
log_pre_applied (bool): If set as False, then logarithmic function is auto applied over target variables
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "poissonnll";
system_dict["hyper-parameters"]["loss"]["name"] = "poissonnll";
system_dict["hyper-parameters"]["loss"]["params"]["log_pre_applied"] = log_pre_applied;
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, threshold_for_mean_estimator=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def huber(system_dict, weight=None, batch_axis=0, threshold_for_mean_estimator=1):
'''
Select huber Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
threshold_for_mean_estimator (int): Threshold for trimmed mean estimator.
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "huber";
system_dict["hyper-parameters"]["loss"]["name"] = "huber";
system_dict["hyper-parameters"]["loss"]["params"]["threshold_for_mean_estimator"] = threshold_for_mean_estimator;
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, margin=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def hinge(system_dict, weight=None, batch_axis=0, margin=1):
'''
Select hinge Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
margin (float): MArgin value.
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "hinge";
system_dict["hyper-parameters"]["loss"]["name"] = "hinge";
system_dict["hyper-parameters"]["loss"]["params"]["margin"] = margin;
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;
@accepts(dict, weight=[list, type(np.array([1, 2, 3])), float, type(None)], batch_axis=int, margin=int, post_trace=False)
#@TraceFunction(trace_args=False, trace_rv=False)
def squared_hinge(system_dict, weight=None, batch_axis=0, margin=1):
'''
Select squared hinge Loss
Args:
system_dict (dict): System dictionary storing experiment state and set variables
weight (float): global scalar for weight loss
batch_axis (int): Axis representing number of elements in the batch - N
margin (float): MArgin value.
Returns:
dict: updated system dict
'''
system_dict["local"]["criterion"] = "squaredhinge";
system_dict["hyper-parameters"]["loss"]["name"] = "squaredhinge";
system_dict["hyper-parameters"]["loss"]["params"]["margin"] = margin;
system_dict["hyper-parameters"]["loss"]["params"]["weight"] = weight;
system_dict["hyper-parameters"]["loss"]["params"]["batch_axis"] = batch_axis;
system_dict["hyper-parameters"]["status"] = True;
return system_dict;