-
Notifications
You must be signed in to change notification settings - Fork 34
/
tst_loss.jl
434 lines (381 loc) · 15.5 KB
/
tst_loss.jl
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
function test_value_typestable(l::SupervisedLoss)
@testset "$(l): " begin
for y in (-1, 1, Int32(-1), Int32(1), -1.5, 1.5, Float32(-.5), Float32(.5))
for t in (-2, 2, Int32(-1), Int32(1), -.5, .5, Float32(-1), Float32(1))
# get expected return type
T = promote_type(typeof(y), typeof(t))
# test basic loss
val = LossFunctions.value(l, y, t)
@test typeof(val) <: T
# test scaled version of loss
@test typeof(LossFunctions.value(T(2)*l, y, t)) <: T
end
end
end
end
function test_value_float32_preserving(l::SupervisedLoss)
@testset "$(l): " begin
for y in (-1, 1, Int32(-1), Int32(1), -1.5, 1.5, Float32(-.5), Float32(.5))
for t in (-2, 2, Int32(-1), Int32(1), -.5, .5, Float32(-1), Float32(1))
val = LossFunctions.value(l, y, t)
T = promote_type(typeof(y),typeof(t))
if !(T <: AbstractFloat)
# cast Integers to a float
# (whether its Float32 or Float64 depends on the loss...)
@test (typeof(val) <: AbstractFloat)
elseif T <: Float32
# preserve Float32
@test (typeof(val) <: Float32)
else
@test (typeof(val) <: Float64)
end
end
end
end
end
function test_value_float64_forcing(l::SupervisedLoss)
@testset "$(l): " begin
for y in (-1, 1, Int32(-1), Int32(1), -1.5, 1.5, Float32(-.5), Float32(.5))
for t in (-2, 2, Int32(-1), Int32(1), -.5, .5, Float32(-1), Float32(1))
val = LossFunctions.value(l, y, t)
@test (typeof(val) <: Float64)
end
end
end
end
function test_value(l::SupervisedLoss, f::Function, y_vec, t_vec)
@testset "$(l): " begin
for y in y_vec, t in t_vec
@test abs(LossFunctions.value(l, y, t) - f(y, t)) < 1e-10
end
end
end
function test_deriv(l::MarginLoss, t_vec)
@testset "$(l): " begin
for y in [-1., 1], t in t_vec
if isdifferentiable(l, y*t)
d_dual = epsilon(LossFunctions.value(l, dual(y, 0), dual(t, 1)))
d_comp = deriv(l, y, t)
@test abs(d_dual - d_comp) < 1e-10
val = LossFunctions.value(l, y, t)
val2, d_comp2 = value_deriv(l, y, t)
val3, d_comp3 = value_deriv_fun(l)(y, t)
val4, d_comp4 = value_deriv(l, y * t)
@test_approx_eq val val2
@test_approx_eq val val3
@test_approx_eq val val4
@test_approx_eq val LossFunctions.value(l, y, t)
@test_approx_eq val LossFunctions.value(l, y*t)
@test_approx_eq val value_fun(l)(y, t)
@test_approx_eq val value_fun(l)(y*t)
@test_approx_eq d_comp d_comp2
@test_approx_eq d_comp d_comp3
@test_approx_eq d_comp y*d_comp4
@test_approx_eq d_comp y*deriv(l, y*t)
@test_approx_eq d_comp deriv_fun(l)(y, t)
@test_approx_eq d_comp y*deriv_fun(l)(y*t)
else
# y*t == 1 ? print(".") : print("(no $(y)*$(t)) ")
#print(".")
end
end
end
end
function test_deriv(l::DistanceLoss, t_vec)
@testset "$(l): " begin
for y in -20:.2:20, t in t_vec
if isdifferentiable(l, t-y)
d_dual = epsilon(LossFunctions.value(l, dual(t-y, 1)))
d_comp = deriv(l, y, t)
@test abs(d_dual - d_comp) < 1e-10
val = LossFunctions.value(l, y, t)
val2, d_comp2 = value_deriv(l, y, t)
val3, d_comp3 = value_deriv_fun(l)(y, t)
val4, d_comp4 = value_deriv(l, t-y)
@test_approx_eq val val2
@test_approx_eq val val3
@test_approx_eq val val4
@test_approx_eq val LossFunctions.value(l, y, t)
@test_approx_eq val LossFunctions.value(l, t-y)
@test_approx_eq val value_fun(l)(y, t)
@test_approx_eq val value_fun(l)(t-y)
@test_approx_eq d_comp d_comp2
@test_approx_eq d_comp d_comp3
@test_approx_eq d_comp d_comp4
@test_approx_eq d_comp deriv(l, t-y)
@test_approx_eq d_comp deriv_fun(l)(y, t)
@test_approx_eq d_comp deriv_fun(l)(t-y)
else
# y-t == 0 ? print(".") : print("$(y-t) ")
#print(".")
end
end
end
end
function test_deriv(l::SupervisedLoss, t_vec)
@testset "$(l): " begin
for y in -20:.2:20, t in t_vec
if isdifferentiable(l, y, t)
d_dual = epsilon(LossFunctions.value(l, y, dual(t, 1)))
d_comp = deriv(l, y, t)
@test abs(d_dual - d_comp) < 1e-10
val = LossFunctions.value(l, y, t)
val2, d_comp2 = value_deriv(l, y, t)
val3, d_comp3 = value_deriv_fun(l)(y, t)
@test_approx_eq val val2
@test_approx_eq val val3
@test_approx_eq val LossFunctions.value(l, y, t)
@test_approx_eq val value_fun(l)(y, t)
@test_approx_eq d_comp d_comp2
@test_approx_eq d_comp d_comp3
@test_approx_eq d_comp deriv(l, y, t)
@test_approx_eq d_comp deriv_fun(l)(y, t)
else
# y-t == 0 ? print(".") : print("$(y-t) ")
#print(".")
end
end
end
end
function test_deriv2(l::MarginLoss, t_vec)
@testset "$(l): " begin
for y in [-1., 1], t in t_vec
if istwicedifferentiable(l, y*t) && isdifferentiable(l, y*t)
d2_dual = epsilon(deriv(l, dual(y, 0), dual(t, 1)))
d2_comp = deriv2(l, y, t)
@test abs(d2_dual - d2_comp) < 1e-10
@test_approx_eq d2_comp deriv2(l, y, t)
@test_approx_eq d2_comp deriv2(l, y*t)
@test_approx_eq d2_comp deriv2_fun(l)(y, t)
@test_approx_eq d2_comp deriv2_fun(l)(y*t)
else
# y*t == 1 ? print(".") : print("(no $(y)*$(t)) ")
#print(".")
end
end
end
end
function test_deriv2(l::DistanceLoss, t_vec)
@testset "$(l): " begin
for y in -20:.2:20, t in t_vec
if istwicedifferentiable(l, t-y) && isdifferentiable(l, t-y)
d2_dual = epsilon(deriv(l, dual(t-y, 1)))
d2_comp = deriv2(l, y, t)
@test abs(d2_dual - d2_comp) < 1e-10
@test_approx_eq d2_comp deriv2(l, y, t)
@test_approx_eq d2_comp deriv2(l, t-y)
@test_approx_eq d2_comp deriv2_fun(l)(y, t)
@test_approx_eq d2_comp deriv2_fun(l)(t-y)
else
# y-t == 0 ? print(".") : print("$(y-t) ")
#print(".")
end
end
end
end
function test_scaledloss(l::Loss, t_vec, y_vec)
@testset "Scaling for $(l): " begin
for λ = (2.0, 2)
sl = ScaledLoss(l,λ)
@test sl == λ * l
for t in t_vec
for y in y_vec
@test LossFunctions.value(ScaledLoss(l,λ),t,y) == λ*LossFunctions.value(l,t,y)
@test deriv(ScaledLoss(l,λ),t,y) == λ*deriv(l,t,y)
@test deriv2(ScaledLoss(l,λ),t,y) == λ*deriv2(l,t,y)
end
end
end
end
end
function test_scaledloss(l::Loss, n_vec)
@testset "Scaling for $(l): " begin
for λ = (2.0, 2)
sl = ScaledLoss(l,λ)
@test sl == λ * l
for n in n_vec
@test LossFunctions.value(ScaledLoss(l,λ),n) == λ*LossFunctions.value(l,n)
@test deriv(ScaledLoss(l,λ),n) == λ*deriv(l,n)
@test deriv2(ScaledLoss(l,λ),n) == λ*deriv2(l,n)
end
end
end
end
# ====================================================================
@testset "Test typestable supervised loss for type stability" begin
for loss in [L1HingeLoss(), L2HingeLoss(), ModifiedHuberLoss(), PerceptronLoss(),
LPDistLoss(1), LPDistLoss(2), LPDistLoss(3)]
test_value_typestable(loss)
# TODO: add ZeroOneLoss after scaling works...
end
end
@testset "Test float-forcing supervised loss for type stability" begin
# Losses that should always return Float64
for loss in [SmoothedL1HingeLoss(0.5), SmoothedL1HingeLoss(1), L1EpsilonInsLoss(0.5),
L1EpsilonInsLoss(1), L2EpsilonInsLoss(0.5), L2EpsilonInsLoss(1),
PeriodicLoss(1), PeriodicLoss(1.5), HuberLoss(1.0)]
test_value_float64_forcing(loss)
test_value_float64_forcing(2.0 * loss)
end
test_value_float64_forcing(2.0 * LogitDistLoss())
test_value_float64_forcing(2.0 * LogitMarginLoss())
# Losses that should return an AbstractFloat, preserving type if possible
for loss in [PeriodicLoss(Float32(1)), PeriodicLoss(Float32(0.5)),
LogitDistLoss(), LogitMarginLoss(),
L1EpsilonInsLoss(Float32(1)), L1EpsilonInsLoss(Float32(0.5)),
L2EpsilonInsLoss(Float32(1)), L2EpsilonInsLoss(Float32(0.5))]
test_value_float32_preserving(loss)
test_value_float32_preserving(Float32(2) * loss)
end
end
@testset "Test margin-based loss against reference function" begin
_zerooneloss(y, t) = sign(y*t) < 0 ? 1 : 0
test_value(ZeroOneLoss(), _zerooneloss, [-1.,1], -10:0.1:10)
_hingeloss(y, t) = max(0, 1 - y.*t)
test_value(HingeLoss(), _hingeloss, [-1.,1], -10:0.1:10)
_l2hingeloss(y, t) = max(0, 1 - y.*t)^2
test_value(L2HingeLoss(), _l2hingeloss, [-1.,1], -10:0.1:10)
_perceptronloss(y, t) = max(0, -y.*t)
test_value(PerceptronLoss(), _perceptronloss, [-1.,1], -10:0.1:10)
_logitmarginloss(y, t) = log(1 + exp(-y.*t))
test_value(LogitMarginLoss(), _logitmarginloss, [-1.,1], -10:0.1:10)
function _smoothedl1hingeloss(γ)
function _value(y, t)
if y.*t >= 1 - γ
1/(2γ) * max(0, 1- y.*t)^2
else
1 - γ / 2 - y.*t
end
end
_value
end
test_value(SmoothedL1HingeLoss(.5), _smoothedl1hingeloss(.5), [-1.,1], -10:0.1:10)
test_value(SmoothedL1HingeLoss(1), _smoothedl1hingeloss(1), [-1.,1], -10:0.1:10)
test_value(SmoothedL1HingeLoss(2), _smoothedl1hingeloss(2), [-1.,1], -10:0.1:10)
function _modhuberloss(y, t)
if y.*t >= -1
max(0, 1 - y.*t)^2
else
-4.*y.*t
end
end
test_value(ModifiedHuberLoss(), _modhuberloss, [-1.,1], -10:0.1:10)
end
@testset "Test distance-based loss against reference function" begin
yr,tr = linspace(-20,20,10),linspace(-30,30,10)
_l1distloss(y, t) = abs(t - y)
test_value(L1DistLoss(), _l1distloss, yr, tr)
_l2distloss(y, t) = (t - y)^2
test_value(L2DistLoss(), _l2distloss, yr, tr)
_lp15distloss(y, t) = abs(t - y)^(1.5)
test_value(LPDistLoss(1.5), _lp15distloss, yr, tr)
function _periodicloss(c)
_value(y, t) = 1 - cos((y-t)*2π/c)
_value
end
test_value(PeriodicLoss(0.5), _periodicloss(0.5), yr, tr)
test_value(PeriodicLoss(1), _periodicloss(1), yr, tr)
test_value(PeriodicLoss(1.5), _periodicloss(1.5), yr, tr)
function _huberloss(d)
_value(y, t) = abs(y-t)<d ? (abs2(y-t)/2) : (d*(abs(y-t) - (d/2)))
_value
end
test_value(HuberLoss(0.5), _huberloss(0.5), yr, tr)
test_value(HuberLoss(1), _huberloss(1), yr, tr)
test_value(HuberLoss(1.5), _huberloss(1.5), yr, tr)
function _l1epsinsloss(ɛ)
_value(y, t) = max(0, abs(t - y) - ɛ)
_value
end
test_value(EpsilonInsLoss(0.5), _l1epsinsloss(0.5), yr, tr)
test_value(EpsilonInsLoss(1), _l1epsinsloss(1), yr, tr)
test_value(EpsilonInsLoss(1.5), _l1epsinsloss(1.5), yr, tr)
function _l2epsinsloss(ɛ)
_value(y, t) = max(0, abs(t - y) - ɛ)^2
_value
end
test_value(L2EpsilonInsLoss(0.5), _l2epsinsloss(0.5), yr, tr)
test_value(L2EpsilonInsLoss(1), _l2epsinsloss(1), yr, tr)
test_value(L2EpsilonInsLoss(1.5), _l2epsinsloss(1.5), yr, tr)
_logitdistloss(y, t) = -log((4*exp(t-y))/(1+exp(t-y))^2)
test_value(LogitDistLoss(), _logitdistloss, yr, tr)
end
@testset "Test other loss against reference function" begin
_crossentropyloss(y, t) = -y*log(t) - (1-y)*log(1-t)
test_value(CrossentropyLoss(), _crossentropyloss, 0:0.01:1, 0.01:0.01:0.99)
_poissonloss(y, t) = exp(t) - t*y
test_value(PoissonLoss(), _poissonloss, 0:10, linspace(0,10,11))
end
margin_losses = [LogitMarginLoss(), L1HingeLoss(), L2HingeLoss(), PerceptronLoss(),
SmoothedL1HingeLoss(.5), SmoothedL1HingeLoss(1), SmoothedL1HingeLoss(2),
ModifiedHuberLoss(), ZeroOneLoss()]
@testset "Test first derivatives of margin-based losses" begin
for loss in margin_losses
test_deriv(loss, -10:0.1:10)
end
end
@testset "Test second derivatives of margin-based losses" begin
for loss in margin_losses
test_deriv2(loss, -10:0.1:10)
end
end
@testset "Test margin-based scaled loss" begin
for loss in margin_losses
test_scaledloss(loss, [-1.,1], -10:0.1:10)
test_scaledloss(loss, -10:0.1:10)
end
end
distance_losses = [L2DistLoss(), LPDistLoss(2.0), L1DistLoss(), LPDistLoss(1.0),
LPDistLoss(0.5), LPDistLoss(1.5), LPDistLoss(3),
LogitDistLoss(), L1EpsilonInsLoss(0.5), EpsilonInsLoss(1.5),
L2EpsilonInsLoss(0.5), L2EpsilonInsLoss(1.5), PeriodicLoss(1),
HuberLoss(1), HuberLoss(1.5)]
@testset "Test first derivatives of distance-based losses" begin
for loss in distance_losses
test_deriv(loss, -30:0.5:30)
end
end
@testset "Test first derivatives of other losses" begin
test_deriv(PoissonLoss(), 0:30)
end
@testset "Test second derivatives of distance-based losses" begin
for loss in distance_losses
test_deriv2(loss, -30:0.5:30)
end
end
@testset "Test distance-based scaled loss" begin
for loss in distance_losses
test_scaledloss(loss, -20:.2:20, -30:0.5:30)
test_scaledloss(loss, -30:0.5:30)
end
end
@testset "Test sparse array conventions for margin-based losses" begin
@testset "sparse vector target, vector output" begin
N = 50
# sparse vector of {0,1}
sparse_target = sprand(N,0.5)
nz = sparse_target .> 0.0
sparse_target[nz] = 1.0
@test typeof(sparse_target) <: AbstractSparseArray
# dense vector of {-1,1}
target = [ t == 0.0 ? -1.0 : 1.0 for t in sparse_target ]
output = randn(N)
for loss in margin_losses
@test isapprox(LossFunctions.value(loss,sparse_target,output), LossFunctions.value(loss,target,output))
end
end
@testset "sparse vector target, matrix output" begin
N = 50
# sparse vector of {0,1}
sparse_target = sprand(N,0.5)
nz = sparse_target .> 0.0
sparse_target[nz] = 1.0
@test typeof(sparse_target) <: AbstractSparseArray
# dense vector of {-1,1}
target = [ t == 0.0 ? -1.0 : 1.0 for t in sparse_target ]
output = randn(N,N)
for loss in margin_losses
@test isapprox(LossFunctions.value(loss,sparse_target,output), LossFunctions.value(loss,target,output))
end
end
end