-
Notifications
You must be signed in to change notification settings - Fork 17
/
core.jl
255 lines (198 loc) · 7.79 KB
/
core.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
## EXPOSE OPTIMISERS TO MLJ (for eg, tuning)
# Here we: (i) Make the optimiser structs "transarent" so that their
# field values are exposed by calls to MLJ.params; and (ii) Overload
# `==` for optimisers, so that we can detect when their parameters
# remain unchanged on calls to MLJModelInterface.update methods.
# We define optimisers of to be `==` if: (i) They have identical type
# AND (ii) their defined field values are `==`. (Note that our `fit`
# methods will only use deep copies of optimisers specified as
# hyperparameters because some fields of `optimisers` carry "state"
# information which is mutated during chain updates.)
for opt in (:Descent, :Momentum, :Nesterov, :RMSProp, :ADAM, :AdaMax,
:ADAGrad, :ADADelta, :AMSGrad, :NADAM, :Optimiser,
:InvDecay, :ExpDecay, :WeightDecay)
@eval begin
# TODO: Uncomment next line when
# https://github.com/alan-turing-institute/MLJModelInterface.jl/issues/28
# is resolved:
# MLJModelInterface.istransparent(m::Flux.$opt) = true
function ==(m1::Flux.$opt, m2::Flux.$opt)
same_values = true
for fld in fieldnames(Flux.$opt)
same_values = same_values &&
getfield(m1, fld) == getfield(m2, fld)
end
return same_values
end
end
end
## GENERAL METHOD TO OPTIMIZE A CHAIN
"""
fit!(chain,
optimiser,
loss,
epochs,
lambda,
alpha,
verbosity,
data)
Optimize a Flux model `chain` using the regularization parameters
`lambda` (strength) and `alpha` (l2/l1 mix), where `loss(yhat, y) ` is
the supervised loss for instances (or vectors of instances) of the
target predictions `yhat` and target observations `y`.
Here `chain` is a `Flux.Chain` object, or other "Flux model" such that
`Flux.params(chain)` returns the parameters to be optimised.
The training `data` is a vector of tuples of the form `(X, y)` where:
- `X` and `y` have type `Array{<:AbstractFloat}`
- the shape of `X` is `(n1, n2, ..., nk, batch_size)` where `(n1, n2,
..., nk)` is the shape of the inputs of `chain`
- the shape of `y` is `(m1, m2, ..., mk, batch_size)` where `(m1, m2,
..., mk)` is the shape of the `chain` outputs.
The contribution to the objective function of a single input/output
instance `(X, y)` is
loss(chain(X), y) + lambda*(model.alpha*l1) + (1 - model.alpha)*l2
where `l1 = sum(norm, params(chain)` and `l2 = sum(norm, params(chain))`.
"""
function fit!(chain, optimiser, loss, epochs,
lambda, alpha, verbosity, data)
Flux.testmode!(chain, false)
# intitialize and start progress meter:
meter = Progress(epochs+1, dt=0, desc="Optimising neural net:",
barglyphs=BarGlyphs("[=> ]"), barlen=25, color=:yellow)
verbosity != 1 || next!(meter)
loss_func(x, y) = loss(chain(x), y)
history = []
prev_loss = Inf
for i in 1:epochs
# We're taking data in a Flux-fashion.
Flux.train!(loss_func, Flux.params(chain), data, optimiser)
current_loss =
mean(loss_func(data[i][1], data[i][2]) for i=1:length(data))
verbosity < 2 ||
@info "Loss is $(round(current_loss.data; sigdigits=4))"
push!(history, current_loss)
# Early stopping is to be externally controlled.
# So @ablaom has commented next 5 lines :
# if current_loss == prev_loss
# @info "Model has reached maximum possible accuracy."*
# "More training won't increase accuracy"
# break
# end
prev_loss = current_loss
verbosity != 1 || next!(meter)
end
Flux.testmode!(chain, true) # to use in inference mode
return chain, history
end
# TODO: add callback functionality to above.
## BUILDING CHAINS A FROM HYPERPARAMETERS + INPUT/OUTPUT SHAPE
# We introduce chain builders as a way of exposing neural network
# hyperparameters (describing, architecture, dropout rates, etc) to
# the MLJ user. These parameters generally exclude the input/output
# dimensions/shapes, as the MLJ fit methods will determine these from
# the training data. A `Builder` object stores the parameters and an
# associated `fit` method builds a corresponding chain given the
# input/output dimensions/shape.
# Below n or (n1, n2) etc refers to network inputs, while m or (m1,
# m2) etc refers to outputs.
abstract type Builder <: MLJModelInterface.Model end
# baby example 1:
mutable struct Linear <: Builder
σ
end
Linear(; σ=Flux.relu) = Linear(σ)
build(builder::Linear, n::Integer, m::Integer) =
Flux.Chain(Flux.Dense(n, m, builder.σ))
# baby example 2:
mutable struct Short <: Builder
n_hidden::Int # if zero use geometric mean of input/output
dropout::Float64
σ
end
Short(; n_hidden=0, dropout=0.5, σ=Flux.sigmoid) = Short(n_hidden, dropout, σ)
function build(builder::Short, n, m)
n_hidden =
builder.n_hidden == 0 ? round(Int, sqrt(n*m)) : builder.n_hidden
return Flux.Chain(Flux.Dense(n, n_hidden, builder.σ),
Flux.Dropout(builder.dropout),
Flux.Dense(n_hidden, m))
end
## HELPERS
"""
nrows(X)
Find the number of rows of `X`, where `X` is an `AbstractVector or
Tables.jl table.
"""
function nrows(X)
Tables.istable(X) || throw(ArgumentError)
Tables.columnaccess(X) || return length(collect(X))
# if has columnaccess
cols = Tables.columntable(X)
!isempty(cols) || return 0
return length(cols[1])
end
nrows(y::AbstractVector) = length(y)
reformat(X) = reformat(X, scitype(X))
# ---------------------------------
# Reformatting tables
reformat(X, ::Type{<:Table}) = MLJModelInterface.matrix(X)'
# ---------------------------------
# Reformatting images
reformat(X, ::Type{<:GrayImage}) =
reshape(Float32.(X), size(X)..., 1)
function reformat(X, ::Type{<:AbstractVector{<:GrayImage}})
ret = zeros(Float32, size(first(X))..., 1, length(X))
for idx=1:size(ret, 4)
ret[:, :, :, idx] .= reformat(X[idx])
end
return ret
end
function reformat(X, ::Type{<:ColorImage})
ret = zeros(Float32, size(X)... , 3)
for w = 1:size(X)[1]
for h = 1:size(X)[2]
ret[w, h, :] .= Float32.([X[w, h].r, X[w, h].g, X[w, h].b])
end
end
return ret
end
function reformat(X, ::Type{<:AbstractVector{<:ColorImage}})
ret = zeros(Float32, size(first(X))..., 3, length(X))
for idx=1:size(ret, 4)
ret[:, :, :, idx] .= reformat(X[idx])
end
return ret
end
# ------------------------------------------------------------
# Reformatting vectors of "scalar" types
reformat(y, ::Type{<:AbstractVector{<:Continuous}}) = y
function reformat(y, ::Type{<:AbstractVector{<:Finite}})
levels = y |> first |> MLJModelInterface.classes
return hcat([Flux.onehot(ele, levels) for ele in y]...,)
end
function reformat(y, ::Type{<:AbstractVector{<:Count}})
levels = y |> first |> MLJModelInterface.classes
return hcat([Flux.onehot(ele, levels) for ele in y]...,)
end
function reformat(y, ::Type{<:AbstractVector{<:Multiclass}})
levels = y |> first |> MLJModelInterface.classes
return hcat([Flux.onehot(ele, levels) for ele in y]...,)
end
_get(Xmatrix::AbstractMatrix, b) = Xmatrix[:, b]
_get(y::AbstractVector, b) = y[b]
# each element in X is a single image of size (w, h, c)
_get(X::AbstractArray{<:Any, 4}, b) = X[:, :, :, b]
"""
collate(model, X, y)
Return the Flux-friendly data object required by `MLJFlux.fit!`, given
input `X` and target `y` in the form required by
`MLJModelInterface.input_scitype(X)` and
`MLJModelInterface.target_scitype(y)`. (The batch size used is given
by `model.batch_size`.)
"""
function collate(model, X, y)
row_batches = Base.Iterators.partition(1:nrows(y), model.batch_size)
Xmatrix = reformat(X)
ymatrix = reformat(y)
return [(_get(Xmatrix, b), _get(ymatrix, b)) for b in row_batches]
end