/
LossFunctions.jl
229 lines (207 loc) · 6.86 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
module LossFunctionsModule
using Random: MersenneTwister
using StatsBase: StatsBase
using DynamicExpressions: AbstractExpressionNode, Node, constructorof
using LossFunctions: LossFunctions
using LossFunctions: SupervisedLoss
using ..InterfaceDynamicExpressionsModule: eval_tree_array
using ..CoreModule: Options, Dataset, DATA_TYPE, LOSS_TYPE
using ..ComplexityModule: compute_complexity
using ..DimensionalAnalysisModule: violates_dimensional_constraints
function _loss(
x::AbstractArray{T}, y::AbstractArray{T}, loss::LT
) where {T<:DATA_TYPE,LT<:Union{Function,SupervisedLoss}}
if LT <: SupervisedLoss
return LossFunctions.mean(loss, x, y)
else
l(i) = loss(x[i], y[i])
return LossFunctions.mean(l, eachindex(x))
end
end
function _weighted_loss(
x::AbstractArray{T}, y::AbstractArray{T}, w::AbstractArray{T}, loss::LT
) where {T<:DATA_TYPE,LT<:Union{Function,SupervisedLoss}}
if LT <: SupervisedLoss
return LossFunctions.sum(loss, x, y, w; normalize=true)
else
l(i) = loss(x[i], y[i], w[i])
return sum(l, eachindex(x)) / sum(w)
end
end
"""If any of the indices are `nothing`, just return."""
@inline function maybe_getindex(v, i...)
if any(==(nothing), i)
return v
else
return getindex(v, i...)
end
end
# Evaluate the loss of a particular expression on the input dataset.
function _eval_loss(
tree::AbstractExpressionNode{T},
dataset::Dataset{T,L},
options::Options,
regularization::Bool,
idx,
)::L where {T<:DATA_TYPE,L<:LOSS_TYPE}
(prediction, completion) = eval_tree_array(
tree, maybe_getindex(dataset.X, :, idx), options
)
if !completion
return L(Inf)
end
loss_val = if dataset.weighted
_weighted_loss(
prediction,
maybe_getindex(dataset.y, idx),
maybe_getindex(dataset.weights, idx),
options.elementwise_loss,
)
else
_loss(prediction, maybe_getindex(dataset.y, idx), options.elementwise_loss)
end
if regularization
loss_val += dimensional_regularization(tree, dataset, options)
end
return loss_val
end
# This evaluates function F:
function evaluator(
f::F, tree::AbstractExpressionNode{T}, dataset::Dataset{T,L}, options::Options, idx
)::L where {T<:DATA_TYPE,L<:LOSS_TYPE,F}
if hasmethod(f, typeof((tree, dataset, options, idx)))
# If user defines method that accepts batching indices:
return f(tree, dataset, options, idx)
elseif options.batching
error(
"User-defined loss function must accept batching indices if `options.batching == true`. " *
"For example, `f(tree, dataset, options, idx)`, where `idx` " *
"is `nothing` if full dataset is to be used, " *
"and a vector of indices otherwise.",
)
else
return f(tree, dataset, options)
end
end
# Evaluate the loss of a particular expression on the input dataset.
function eval_loss(
tree::AbstractExpressionNode{T},
dataset::Dataset{T,L},
options::Options;
regularization::Bool=true,
idx=nothing,
)::L where {T<:DATA_TYPE,L<:LOSS_TYPE}
loss_val = if options.loss_function === nothing
_eval_loss(tree, dataset, options, regularization, idx)
else
f = options.loss_function::Function
evaluator(f, tree, dataset, options, idx)
end
return loss_val
end
function eval_loss_batched(
tree::AbstractExpressionNode{T},
dataset::Dataset{T,L},
options::Options;
regularization::Bool=true,
idx=nothing,
)::L where {T<:DATA_TYPE,L<:LOSS_TYPE}
_idx = idx === nothing ? batch_sample(dataset, options) : idx
return eval_loss(tree, dataset, options; regularization=regularization, idx=_idx)
end
function batch_sample(dataset, options)
return StatsBase.sample(1:(dataset.n), options.batch_size; replace=true)::Vector{Int}
end
# Just so we can pass either PopMember or Node here:
get_tree(t::AbstractExpressionNode) = t
get_tree(m) = m.tree
# Beware: this is a circular dependency situation...
# PopMember is using losses, but then we also want
# losses to use the PopMember's cached complexity for trees.
# TODO!
# Compute a score which includes a complexity penalty in the loss
function loss_to_score(
loss::L,
use_baseline::Bool,
baseline::L,
member,
options::Options,
complexity::Union{Int,Nothing}=nothing,
)::L where {L<:LOSS_TYPE}
# TODO: Come up with a more general normalization scheme.
normalization = if baseline >= L(0.01) && use_baseline
baseline
else
L(0.01)
end
loss_val = loss / normalization
size = complexity === nothing ? compute_complexity(member, options) : complexity
parsimony_term = size * options.parsimony
loss_val += L(parsimony_term)
return loss_val
end
# Score an equation
function score_func(
dataset::Dataset{T,L}, member, options::Options; complexity::Union{Int,Nothing}=nothing
)::Tuple{L,L} where {T<:DATA_TYPE,L<:LOSS_TYPE}
result_loss = eval_loss(get_tree(member), dataset, options)
score = loss_to_score(
result_loss,
dataset.use_baseline,
dataset.baseline_loss,
member,
options,
complexity,
)
return score, result_loss
end
# Score an equation with a small batch
function score_func_batched(
dataset::Dataset{T,L},
member,
options::Options;
complexity::Union{Int,Nothing}=nothing,
idx=nothing,
)::Tuple{L,L} where {T<:DATA_TYPE,L<:LOSS_TYPE}
result_loss = eval_loss_batched(get_tree(member), dataset, options; idx=idx)
score = loss_to_score(
result_loss,
dataset.use_baseline,
dataset.baseline_loss,
member,
options,
complexity,
)
return score, result_loss
end
"""
update_baseline_loss!(dataset::Dataset{T,L}, options::Options) where {T<:DATA_TYPE,L<:LOSS_TYPE}
Update the baseline loss of the dataset using the loss function specified in `options`.
"""
function update_baseline_loss!(
dataset::Dataset{T,L}, options::Options
) where {T<:DATA_TYPE,L<:LOSS_TYPE}
example_tree = constructorof(options.node_type)(T; val=dataset.avg_y)
# TODO: It could be that the loss function is not defined for this example type?
baseline_loss = eval_loss(example_tree, dataset, options)
if isfinite(baseline_loss)
dataset.baseline_loss = baseline_loss
dataset.use_baseline = true
else
dataset.baseline_loss = one(L)
dataset.use_baseline = false
end
return nothing
end
function dimensional_regularization(
tree::AbstractExpressionNode{T}, dataset::Dataset{T,L}, options::Options
) where {T<:DATA_TYPE,L<:LOSS_TYPE}
if !violates_dimensional_constraints(tree, dataset, options)
return zero(L)
elseif options.dimensional_constraint_penalty === nothing
return L(1000)
else
return L(options.dimensional_constraint_penalty::Float32)
end
end
end