@@ -4,7 +4,7 @@ DocTestFilters = r"[0-9\.]+f0"
4
4
```
5
5
6
6
"""
7
- mae(ŷ, y; agg= mean)
7
+ mae(ŷ, y; agg = mean)
8
8
9
9
Return the loss corresponding to mean absolute error:
10
10
@@ -18,14 +18,14 @@ julia> Flux.mae(y_model, 1:3)
18
18
0.10000000000000009
19
19
```
20
20
"""
21
- mae (ŷ, y; agg= mean) = agg (abs .(ŷ .- y))
21
+ mae (ŷ, y; agg = mean) = agg (abs .(ŷ .- y))
22
22
23
23
"""
24
- mse(ŷ, y; agg= mean)
24
+ mse(ŷ, y; agg = mean)
25
25
26
26
Return the loss corresponding to mean square error:
27
27
28
- agg((ŷ .- y).^ 2)
28
+ agg((ŷ .- y) .^ 2)
29
29
30
30
See also: [`mae`](@ref), [`msle`](@ref), [`crossentropy`](@ref).
31
31
@@ -39,14 +39,14 @@ julia> Flux.mse(y_model, y_true)
39
39
0.010000000000000018
40
40
```
41
41
"""
42
- mse (ŷ, y; agg= mean) = agg ((ŷ .- y). ^ 2 )
42
+ mse (ŷ, y; agg = mean) = agg ((ŷ .- y) .^ 2 )
43
43
44
44
"""
45
- msle(ŷ, y; agg= mean, ϵ= eps(ŷ))
45
+ msle(ŷ, y; agg = mean, ϵ = eps(ŷ))
46
46
47
47
The loss corresponding to mean squared logarithmic errors, calculated as
48
48
49
- agg((log.(ŷ .+ ϵ) .- log.(y .+ ϵ)).^ 2)
49
+ agg((log.(ŷ .+ ϵ) .- log.(y .+ ϵ)) .^ 2)
50
50
51
51
The `ϵ` term provides numerical stability.
52
52
Penalizes an under-estimation more than an over-estimatation.
@@ -60,10 +60,11 @@ julia> Flux.msle(Float32[0.9, 1.8, 2.7], 1:3)
60
60
0.011100831f0
61
61
```
62
62
"""
63
- msle (ŷ, y; agg= mean, ϵ= epseltype (ŷ)) = agg ((log .((ŷ .+ ϵ) ./ (y .+ ϵ))). ^ 2 )
63
+ msle (ŷ, y; agg = mean, ϵ = epseltype (ŷ)) =
64
+ agg ((log .((ŷ .+ ϵ) ./ (y .+ ϵ))) .^ 2 )
64
65
65
66
"""
66
- huber_loss(ŷ, y; δ= 1, agg= mean)
67
+ huber_loss(ŷ, y; δ = 1, agg = mean)
67
68
68
69
Return the mean of the [Huber loss](https://en.wikipedia.org/wiki/Huber_loss)
69
70
given the prediction `ŷ` and true values `y`.
@@ -72,12 +73,12 @@ given the prediction `ŷ` and true values `y`.
72
73
Huber loss = |
73
74
| δ * (|ŷ - y| - 0.5 * δ), otherwise
74
75
"""
75
- function huber_loss (ŷ, y; agg= mean, δ= ofeltype (ŷ, 1 ))
76
+ function huber_loss (ŷ, y; agg = mean, δ = ofeltype (ŷ, 1 ))
76
77
abs_error = abs .(ŷ .- y)
77
78
# TODO : remove dropgrad when Zygote can handle this function with CuArrays
78
79
temp = Zygote. dropgrad (abs_error .< δ)
79
80
x = ofeltype (ŷ, 0.5 )
80
- agg (((abs_error.^ 2 ) .* temp) .* x .+ δ* (abs_error .- x* δ) .* (1 .- temp))
81
+ agg (((abs_error .^ 2 ) .* temp) .* x .+ δ * (abs_error .- x * δ) .* (1 .- temp))
81
82
end
82
83
83
84
"""
@@ -131,7 +132,7 @@ julia> Flux.crossentropy(y_dis, y) > Flux.crossentropy(y_dis, y_smoothed)
131
132
true
132
133
```
133
134
"""
134
- function label_smoothing (y:: Union{AbstractArray,Number} , α:: Number ; dims:: Int = 1 )
135
+ function label_smoothing (y:: Union{AbstractArray,Number} , α:: Number ; dims:: Int = 1 )
135
136
if ! (0 < α < 1 )
136
137
throw (ArgumentError (" α must be between 0 and 1" ))
137
138
end
@@ -146,7 +147,7 @@ function label_smoothing(y::Union{AbstractArray,Number}, α::Number; dims::Int=1
146
147
end
147
148
148
149
"""
149
- crossentropy(ŷ, y; dims= 1, ϵ= eps(ŷ), agg= mean)
150
+ crossentropy(ŷ, y; dims = 1, ϵ = eps(ŷ), agg = mean)
150
151
151
152
Return the cross entropy between the given probability distributions;
152
153
calculated as
@@ -201,12 +202,12 @@ julia> Flux.crossentropy(y_model, y_smooth)
201
202
1.5776052f0
202
203
```
203
204
"""
204
- function crossentropy (ŷ, y; dims= 1 , agg= mean, ϵ= epseltype (ŷ))
205
- agg (.- sum (xlogy .(y, ŷ .+ ϵ); dims= dims))
205
+ function crossentropy (ŷ, y; dims = 1 , agg = mean, ϵ = epseltype (ŷ))
206
+ agg (.- sum (xlogy .(y, ŷ .+ ϵ); dims = dims))
206
207
end
207
208
208
209
"""
209
- logitcrossentropy(ŷ, y; dims= 1, agg= mean)
210
+ logitcrossentropy(ŷ, y; dims = 1, agg = mean)
210
211
211
212
Return the cross entropy calculated by
212
213
@@ -239,16 +240,16 @@ julia> Flux.crossentropy(softmax(y_model), y_label)
239
240
1.5791197f0
240
241
```
241
242
"""
242
- function logitcrossentropy (ŷ, y; dims= 1 , agg= mean)
243
- agg (.- sum (y .* logsoftmax (ŷ; dims= dims); dims= dims))
243
+ function logitcrossentropy (ŷ, y; dims = 1 , agg = mean)
244
+ agg (.- sum (y .* logsoftmax (ŷ; dims = dims); dims = dims))
244
245
end
245
246
246
247
"""
247
- binarycrossentropy(ŷ, y; agg= mean, ϵ= eps(ŷ))
248
+ binarycrossentropy(ŷ, y; agg = mean, ϵ = eps(ŷ))
248
249
249
250
Return the binary cross-entropy loss, computed as
250
251
251
- agg(@.(-y* log(ŷ + ϵ) - (1-y)* log(1- ŷ + ϵ)))
252
+ agg(@.(-y * log(ŷ + ϵ) - (1 - y) * log(1 - ŷ + ϵ)))
252
253
253
254
Where typically, the prediction `ŷ` is given by the output of a [`sigmoid`](@ref) activation.
254
255
The `ϵ` term is included to avoid infinity. Using [`logitbinarycrossentropy`](@ref) is recomended
@@ -287,14 +288,14 @@ julia> Flux.crossentropy(y_prob, y_hot)
287
288
0.43989f0
288
289
```
289
290
"""
290
- function binarycrossentropy (ŷ, y; agg= mean, ϵ= epseltype (ŷ))
291
- agg (@. (- xlogy (y, ŷ+ ϵ) - xlogy (1 - y, 1 - ŷ + ϵ)))
291
+ function binarycrossentropy (ŷ, y; agg = mean, ϵ = epseltype (ŷ))
292
+ agg (@. (- xlogy (y, ŷ + ϵ) - xlogy (1 - y, 1 - ŷ + ϵ)))
292
293
end
293
294
# Re-definition to fix interaction with CuArrays.
294
- # CUDA.@cufunc binarycrossentropy(ŷ, y; ϵ= eps(ŷ)) = -y* log(ŷ + ϵ) - (1 - y)* log(1 - ŷ + ϵ)
295
+ # CUDA.@cufunc binarycrossentropy(ŷ, y; ϵ = eps(ŷ)) = -y * log(ŷ + ϵ) - (1 - y) * log(1 - ŷ + ϵ)
295
296
296
297
"""
297
- logitbinarycrossentropy(ŷ, y; agg= mean)
298
+ logitbinarycrossentropy(ŷ, y; agg = mean)
298
299
299
300
Mathematically equivalent to
300
301
[`binarycrossentropy(σ(ŷ), y)`](@ref) but is more numerically stable.
@@ -318,15 +319,15 @@ julia> Flux.binarycrossentropy(sigmoid.(y_model), y_bin)
318
319
0.16083185f0
319
320
```
320
321
"""
321
- function logitbinarycrossentropy (ŷ, y; agg= mean)
322
- agg (@. ((1 - y) * ŷ - logσ (ŷ)))
322
+ function logitbinarycrossentropy (ŷ, y; agg = mean)
323
+ agg (@. ((1 - y) * ŷ - logσ (ŷ)))
323
324
end
324
325
# Re-definition to fix interaction with CuArrays.
325
- # CUDA.@cufunc logitbinarycrossentropy(ŷ, y) = (1 - y)* ŷ - logσ(ŷ)
326
+ # CUDA.@cufunc logitbinarycrossentropy(ŷ, y) = (1 - y) * ŷ - logσ(ŷ)
326
327
327
328
328
329
"""
329
- kldivergence(ŷ, y; agg= mean, ϵ= eps(ŷ))
330
+ kldivergence(ŷ, y; agg = mean, ϵ = eps(ŷ))
330
331
331
332
Return the
332
333
[Kullback-Leibler divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence)
@@ -350,19 +351,19 @@ julia> p2 = fill(0.5, 2, 2)
350
351
julia> Flux.kldivergence(p2, p1) ≈ log(2)
351
352
true
352
353
353
- julia> Flux.kldivergence(p2, p1; agg= sum) ≈ 2log(2)
354
+ julia> Flux.kldivergence(p2, p1; agg = sum) ≈ 2log(2)
354
355
true
355
356
356
- julia> Flux.kldivergence(p2, p2; ϵ= 0) # about -2e-16 with the regulator
357
+ julia> Flux.kldivergence(p2, p2; ϵ = 0) # about -2e-16 with the regulator
357
358
0.0
358
359
359
- julia> Flux.kldivergence(p1, p2; ϵ= 0) # about 17.3 with the regulator
360
+ julia> Flux.kldivergence(p1, p2; ϵ = 0) # about 17.3 with the regulator
360
361
Inf
361
362
```
362
363
"""
363
- function kldivergence (ŷ, y; dims= 1 , agg= mean, ϵ= epseltype (ŷ))
364
- entropy = agg (sum (xlogx .(y), dims= dims))
365
- cross_entropy = crossentropy (ŷ, y; dims= dims, agg= agg, ϵ= ϵ)
364
+ function kldivergence (ŷ, y; dims = 1 , agg = mean, ϵ = epseltype (ŷ))
365
+ entropy = agg (sum (xlogx .(y), dims = dims))
366
+ cross_entropy = crossentropy (ŷ, y; dims = dims, agg = agg, ϵ = ϵ)
366
367
return entropy + cross_entropy
367
368
end
368
369
@@ -374,18 +375,19 @@ end
374
375
375
376
[More information.](https://peltarion.com/knowledge-center/documentation/modeling-view/build-an-ai-model/loss-functions/poisson).
376
377
"""
377
- poisson_loss (ŷ, y; agg= mean) = agg (ŷ .- xlogy .(y, ŷ))
378
+ poisson_loss (ŷ, y; agg = mean) = agg (ŷ .- xlogy .(y, ŷ))
378
379
379
380
"""
380
- hinge_loss(ŷ, y; agg= mean)
381
+ hinge_loss(ŷ, y; agg = mean)
381
382
382
383
Return the [hinge_loss loss](https://en.wikipedia.org/wiki/Hinge_loss) given the
383
384
prediction `ŷ` and true labels `y` (containing 1 or -1); calculated as
384
385
`sum(max.(0, 1 .- ŷ .* y)) / size(y, 2)`.
385
386
386
387
See also: [`squared_hinge_loss`](@ref)
387
388
"""
388
- hinge_loss (ŷ, y; agg= mean) = agg (max .(0 , 1 .- ŷ .* y))
389
+ hinge_loss (ŷ, y; agg = mean) =
390
+ agg (max .(0 , 1 .- ŷ .* y))
389
391
390
392
"""
391
393
squared_hinge_loss(ŷ, y)
@@ -395,10 +397,11 @@ Return the squared hinge_loss loss given the prediction `ŷ` and true labels `y
395
397
396
398
See also: [`hinge_loss`](@ref)
397
399
"""
398
- squared_hinge_loss (ŷ, y; agg= mean) = agg ((max .(0 , 1 .- ŷ .* y)). ^ 2 )
400
+ squared_hinge_loss (ŷ, y; agg = mean) =
401
+ agg ((max .(0 , 1 .- ŷ .* y)) .^ 2 )
399
402
400
403
"""
401
- dice_coeff_loss(ŷ, y; smooth= 1)
404
+ dice_coeff_loss(ŷ, y; smooth = 1)
402
405
403
406
Return a loss based on the dice coefficient.
404
407
Used in the [V-Net](https://arxiv.org/abs/1606.04797) image segmentation
@@ -407,21 +410,22 @@ Similar to the F1_score. Calculated as:
407
410
408
411
1 - 2*sum(|ŷ .* y| + smooth) / (sum(ŷ.^2) + sum(y.^2) + smooth)
409
412
"""
410
- dice_coeff_loss (ŷ, y; smooth= ofeltype (ŷ, 1.0 )) = 1 - (2 * sum (y .* ŷ) + smooth) / (sum (y.^ 2 ) + sum (ŷ.^ 2 ) + smooth) # TODO agg
413
+ dice_coeff_loss (ŷ, y; smooth = ofeltype (ŷ, 1.0 )) =
414
+ 1 - (2 * sum (y .* ŷ) + smooth) / (sum (y .^ 2 ) + sum (ŷ .^ 2 ) + smooth) # TODO agg
411
415
412
416
"""
413
- tversky_loss(ŷ, y; β= 0.7)
417
+ tversky_loss(ŷ, y; β = 0.7)
414
418
415
419
Return the [Tversky loss](https://arxiv.org/abs/1706.05721).
416
420
Used with imbalanced data to give more weight to false negatives.
417
421
Larger β weigh recall more than precision (by placing more emphasis on false negatives)
418
422
Calculated as:
419
423
1 - sum(|y .* ŷ| + 1) / (sum(y .* ŷ + β*(1 .- y) .* ŷ + (1 - β)*y .* (1 .- ŷ)) + 1)
420
424
"""
421
- function tversky_loss (ŷ, y; β= ofeltype (ŷ, 0.7 ))
425
+ function tversky_loss (ŷ, y; β = ofeltype (ŷ, 0.7 ))
422
426
# TODO add agg
423
427
num = sum (y .* ŷ) + 1
424
- den = sum (y .* ŷ + β* (1 .- y) .* ŷ + (1 - β)* y .* (1 .- ŷ)) + 1
428
+ den = sum (y .* ŷ + β * (1 .- y) .* ŷ + (1 - β) * y .* (1 .- ŷ)) + 1
425
429
1 - num / den
426
430
end
427
431
0 commit comments