-
Notifications
You must be signed in to change notification settings - Fork 0
/
LossFunctions.jl
245 lines (223 loc) · 6.45 KB
/
LossFunctions.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
module LossFunction
using LinearAlgebra
using Statistics
LOSSES = [
:mse, :cee, :mae, :huber, :logcosh_loss, :poisson, :hinge, :smooth_hinge
]
for f in LOSSES
@eval export $(f)
end
safe_log(x, miniv = 1e-8) = map(log, clamp.(x, miniv, Inf))
@doc raw"""
mse(y, t; reduction="mean")
Mean Square Error. This is the expression:
```math
MSE(y, t) = \frac{\sum_{i=1}^{n} (t_{i}-y_{i})^{2}}{n}
```
"""
function mse(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = (y.-t).^2
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
mse(y::Number, t::Number) = (y-t)^2
@doc raw"""
cee(y, t; reduction="mean")
Cross Entropy Error. This is the expression:
```math
CEE(y, t) = \frac{\sum_{i=1}^{n} t\ln y}{n}
```
"""
function cee(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = -t.*safe_log(y)
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
cee(y::Number, t::Number) = -t*safe_log(y)
@doc raw"""
mae(y, t)
Mean Absolute Error. This is the expression:
```math
MAE(y, t) = \frac{\sum_{i=1}^{n} |t_{i}-y_{i}|}{n}
```
"""
function mae(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = abs.(t-y)
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
mae(y::Number, t::Number) = abs(t-y)
@doc raw"""
huber(y, t; δ=1, reduction="mean")
Huber-Loss. If `δ` is large, it will be a function like [`mse`](@ref), and if it is small, it will be a function like [`mae`](@ref). This is the expression:
```math
a = |t_{i}-y_{i}| \\
Huber(y, t) = \frac{1}{n} \sum_{i=1}^{n} \left\{
\begin{array}{ll}
\frac{1}{2}a^{2} & (a \leq \delta) \\
\delta(a-\frac{1}{2}\delta) & (a \gt \delta)
\end{array}
\right.
```
"""
function huber(y::AbstractVector, t::AbstractVector; reduction::String="mean", δ=1)
a = abs.(t-y)
loss = @. ifelse(a<=δ, a^2/2, (a-δ/2)δ)
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
huber(y::Number, t::Number; δ=1) = ifelse(abs(t-y)<=δ, abs(t-y)^2/2, (abs(t-y)-δ/2)δ)
@doc raw"""
logcosh_loss(y, t; reduction="mean")
Log Cosh. Basically, it's [`mae`](@ref), but if the loss is small, it will be close to [`mse`](@ref). This is the expression:
```math
Logcosh(y, t) = \frac{\sum_{i=1}^{n} \log(\cosh(t_{i}-y_{i}))}{n}
```
"""
function logcosh_loss(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = @. log(cosh(t-y))
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
logcosh_loss(y::Number, t::Number) = log(cosh(t-y))
@doc raw"""
Poisson(y, t; reduction="mean")
Poisson Loss, Distribution of predicted value and loss of Poisson distribution. This is the expression:
```math
Poisson(y, t) = \frac{\sum_{i=1}^{n} y_{i}-t_{i} \ln y}{n}
```
"""
function poisson(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = @. y - t*log(y)
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
poisson(y::Number, t::Number) = y - t*log(y)
@doc raw"""
hinge(y, t; reduction="mean")
Hinge Loss, for SVM. This is the expression:
```math
Hinge(y, t) = \frac{\sum_{i=1}^{n} \max(1-y_{i}t_{i}, 0)}{n}
```
"""
function hinge(y::AbstractVector, t::AbstractVector; reduction::String="mean")
loss = @. max(1-y*t, 0)
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
hinge(y::Number, t::Number) = max(1-y*t, 0)
@doc raw"""
smooth_hinge(y, t; reduction="mean")
Smoothing Hinge Loss. This is the expression:
```math
smoothHinge(y, t) = \frac{1}{n} \sum_{i=1}^{n} \left\{
\begin{array}{ll}
0 & (t_{i}y_{i} \geq 1) \\
\frac{1}{2}(1-t_{i}y_{i})^{2} & (0 \lt t_{i}y_{i} \lt 1) \\
\frac{1}{2} - t_{i}y_{i} & (t_{i}y_{i} \leq 0)
\end{array}
\right.
```
"""
function smooth_hinge(y::AbstractVector, t::AbstractVector; reduction::String="mean")
z = t.*y
loss = similar(z)
for i in 1 : length(y)
if z[i] >= 0
loss[i] = 0
elseif 0 < z[i] < 1
loss[i] = (1-z[i])^2/2
else
loss[i] = 1/2-z[i]
end
end
if reduction=="none"
return loss
elseif reduction=="sum"
return sum(loss)
elseif reduction=="mean"
return mean(loss)
else
throw(ArgumentError("`reduction` must be either `none`, `sum` or `mean`"))
end
end
function smooth_hinge(y::Number, t::Number)
if t*y >= 0
return 0
elseif 0 < t*y < 1
return (1-t*y)^2/2
else
return 1/2-t*y
end
end
#TODO:Consider whether to implement Calback liverer information volume
for lossfunc in LOSSES
#For Vector and Number
@eval begin
function $(lossfunc)(y::AbstractVector{T}, t::Number; reduction::String="mean") where {T}
$(lossfunc)(y, fill(t, length(y)), reduction=reduction)
end
end
#For Matrix and Number
@eval begin
function $(lossfunc)(y::AbstractMatrix{T}, t::Number; reduction::String="mean") where {T}
if length(y) == 1
return $(lossfunc)(y..., t)
elseif size(y, 1)==1 || size(y, 2)==1
return $(lossfunc)(vec(y), t, reduction=reduction)
else
throw(DimensionMismatch("LossFunctions don't support for Matrix!"))
end
end
end
end
end