-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_training.jl
335 lines (290 loc) · 13.6 KB
/
meta_training.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
module MetaTraining
using Dates: now
using Logging: SimpleLogger, Info
using Printf: @sprintf
using Statistics: mean, var
using Random: seed!, shuffle!
using BSON
import Flux
using Flux.Tracker: gradient
using JLD2
import JSON
using TensorBoardLogger
using ..DataIterator
using ..MetadataPredictor
using ..ModelUtils
using ..TrainingUtils
export meta_trainingloop!, MetaTrainingParameters, MTPs
Base.@kwdef struct MetaTrainingParameters
epochs::Integer = 10
lr::Float64 = 0.0002
batch_size::Integer = 32
logevery::Integer = 300
saveevery::Integer = 1500
use_bson::Bool = false
testratio::AbstractFloat = 0.1
buffer_size::Integer = 3
dataiter_threads::Integer = 0
logdir::AbstractString = joinpath("exps", newexpdir("meta"))
params_logfile::AbstractString = "params.json"
logfile::AbstractString = "training.log"
earlystoppingwaitepochs::Integer = 10
earlystoppingthreshold::AbstractFloat = Inf32
criterion::Function = Flux.mse
overfit_on_batch::Bool = false
seed::Integer = 0
end
"Shorthand for MetaTrainingParameters for interactive use."
const MTPs = MetaTrainingParameters
function meta_trainingloop!(model::Union{LearningModel, AbstractString},
dbpath::AbstractString,
params::MetaTrainingParameters=MetaTrainingParameters())
seed!(params.seed)
set_zero_subnormals(true)
paramdict = Dict{Symbol, Any}(field => getproperty(params, field) |>
x -> x isa Function ? Symbol(x) : x
for field in propertynames(params))
paramdict[:dbpath] = dbpath
db = loaddb(dbpath)
trainindices, testindices = traintestsplit(db, params.testratio)
if params.overfit_on_batch
if params.overfit_on_batch isa Bool
trainindices = view(trainindices, firstindex(trainindices):16)
else
trainindices = view(trainindices,
firstindex(trainindices):params.overfit_on_batch)
end
testindices = trainindices
end
use_bson = Val(params.use_bson)
if model isa AbstractString
paramdict[:modelpath] = model
(model, optim, trainlosses, meanlosses, varlosses, past_steps) = load_cp(
model, use_bson)
else
optim = Flux.ADAM(params.lr)
trainlosses = Float32[]
"Means of test losses."
meanlosses = eltype(trainlosses)[]
"Variances of test losses."
varlosses = eltype(trainlosses)[]
past_steps = UInt64(0)
end
model::LearningModel
paramdict[:modelparams] = Dict{Symbol, Any}(k => v isa Function ? Symbol(v) : v
for (k, v) in model.hyperparams)
epochs = params.epochs
batch_size = params.batch_size
logdir = params.logdir
logevery = params.logevery
saveevery = params.saveevery
maxmeanloss = typemin(eltype(trainlosses))
maxvarloss = typemin(eltype(trainlosses))
maxvarlossdigits = 0 # Predefined in case `varloss` is NaN.
local maxmeanlossdigits
local testlosses
steps = UInt64(0)
trainiter = gan_dataiterator(db, params.buffer_size, trainindices, batch_size,
params.dataiter_threads)
testiter = gan_dataiterator(db, params.buffer_size, testindices, batch_size,
params.dataiter_threads)
loss = makeloss(model, params.criterion)
parameters = Flux.params(model)
earlystoppingthreshold = params.earlystoppingthreshold
earlystoppingthreshold < 1 && (earlystoppingthreshold += 1)
earlystoppingwaitepochs = params.earlystoppingwaitepochs
mkpath(logdir)
# Save parameters
open(joinpath(logdir, params.params_logfile), "w") do io
JSON.print(io, paramdict, 4)
end
# Free `paramdict`
paramdict = nothing
log_io = open(joinpath(logdir, params.logfile), "a")
@fastmath try
logger = SimpleLogger(log_io)
tblogger = TBLogger(joinpath(logdir, "tensorboard"), min_level=Info)
past_steps > 0 && logprint(logger, "Loaded meta predictor with $past_steps steps.")
# To get local time instead of UTC for printing and filenames:
starttimestr = replace(string(now()), ':' => '-')
starttime = time()
logprint(logger, "Starting meta predictor training at $starttimestr for $epochs "
* "epochs. Seed: $(params.seed).")
# Initial test
testlosses = testmodel(model, testiter, testindices, batch_size, loss)
meanloss = mean(testlosses)
varloss = var(testlosses, mean=meanloss)
if past_steps == 0
@tblog(tblogger, meantestloss=meanloss, vartestloss=varloss,
log_step_increment=0)
push!(meanlosses, meanloss)
push!(varlosses, varloss)
else
@tblog(tblogger, log_step_increment=convert(Int, past_steps))
end
timediff = time() - starttime
logprint(logger, "Initial mean test loss: $(@sprintf("%.4f", meanloss)) "
* "(variance: $(@sprintf("%.3f", varloss))); "
* "total time: $(@sprintf("%.2f", timediff / 60)) min.")
for epoch in 1:epochs
for (i, j) in zip(1:cld(length(trainindices), batch_size),
Iterators.countfrom(1, batch_size))
if steps < past_steps
take!(trainiter)
steps += 1
continue
end
training_step!(model, parameters, optim, trainiter, loss,
trainlosses, tblogger)
steps += 1
if logevery != 0 && steps % logevery == 0
testlosses = testmodel(model, testiter, testindices, batch_size, loss)
meanloss = mean(testlosses)
varloss = var(testlosses, mean=meanloss)
@tblog(tblogger, meantestloss=meanloss, vartestloss=varloss,
log_step_increment=0)
if meanloss > maxmeanloss
maxmeanloss = meanloss
maxmeanlossdigits = ndigits(trunc(Int, maxmeanloss)) + 5
end
if varloss > maxvarloss
maxvarloss = varloss
maxvarlossdigits = ndigits(trunc(Int, maxvarloss)) + 4
end
push!(meanlosses, meanloss)
push!(varlosses, varloss)
if length(meanlosses) > 1
lossdiff = meanloss - meanlosses[end - 1]
lossratio = meanloss / meanlosses[end - 1]
else
lossdiff = 0
end
timediff = time() - starttime
logprint(logger, "Epoch $(lpad(epoch, ndigits(epochs))) / "
* "$epochs; sequence "
* "$(lpad(j, ndigits(length(trainindices)))) / "
* "$(length(trainindices)); mean test loss: "
* "$(lpad(@sprintf("%.4f", meanloss), maxmeanlossdigits)) "
* "(variance: "
* "$(lpad(@sprintf("%.3f", varloss), maxvarlossdigits))); "
* "mean time per step: "
* "$(@sprintf("%.3f", timediff / (steps - past_steps))) s; "
* "total time: $(@sprintf("%.2f", timediff / 60)) min.")
# Early stopping
if (epoch > earlystoppingwaitepochs && lossdiff > 0
&& lossratio >= earlystoppingthreshold)
save_cp(model, optim, trainlosses, meanlosses, varlosses,
steps, logdir, starttimestr, use_bson)
logprint(logger, "Early stopping activated after $steps training "
* "steps ($epoch epochs, $j sequences in current epoch). "
* "Loss increase: $meanloss - $(meanlosses[end - 1]) = "
* "$(round(lossdiff, digits=3)) "
* "($(round((lossratio - 1) * 100, digits=2)) %). "
* "Total time: $(round(timediff / 60, digits=2)) min.")
cleanupall(trainiter, testiter, log_io)
return (model, trainlosses, testlosses, meanlosses, varlosses,
db, trainindices, testindices)
end
end
if saveevery != 0 && steps % saveevery == 0
if logevery != 0 && steps % logevery != 0
testlosses = testmodel(model, testiter, testindices,
batch_size, loss)
meanloss = mean(testlosses)
varloss = var(testlosses, mean=meanloss)
@tblog(tblogger, meantestloss=meanloss, vartestloss=varloss,
log_step_increment=0)
push!(meanlosses, meanloss)
push!(varlosses, varloss)
end
save_cp(model, optim, trainlosses, meanlosses, varlosses,
steps, logdir, starttimestr, use_bson)
logprint(logger, "Saved checkpoint after $steps training steps.")
end
end
end
save_cp(model, optim, trainlosses, meanlosses, varlosses,
steps, logdir, starttimestr, use_bson)
logprint(logger, "Training finished after $steps training steps and "
* "$(round((time() - starttime) / 60, digits=2)) minutes.")
finally
cleanupall(trainiter, testiter, log_io)
end
return (model, trainlosses, testlosses, meanlosses, varlosses,
db, trainindices, testindices)
end
function training_step!(model, parameters, optim, trainiter, loss, trainlosses, tblogger)
real_batch, meta_batch = map(togpu, take!(trainiter))
l = Flux.data(step!(model, parameters, optim, loss, real_batch, meta_batch))
push!(trainlosses, l)
@tblog tblogger trainloss=l log_step_increment=0
return l
end
function testmodel(model, testiter, testindices, batch_size, loss)
Flux.testmode!(model)
testlosses = Float32[]
for i in 1:cld(length(testindices), batch_size)
real_batch, meta_batch = map(togpu, take!(testiter))
l = Flux.data(calculate_loss(model, loss, real_batch, meta_batch))
push!(testlosses, l)
end
Flux.testmode!(model, false)
return testlosses
end
# Numbered Vararg so we can make sure we didn't miss one without having to list them all.
cleanupall(args::Vararg{Any, 3}) = foreach(cleanup, args)
function save_cp(model, optim, trainlosses, meanlosses,
varlosses, steps, logdir, starttimestr, use_bson::Val{true})
# TODO Due to having to use a different BSON PR branch, this fails.
# When the branch is merged, update the package and remove the try-catch.
try
bson(joinpath(logdir, "meta-cp_$steps-steps_loss-$(trainlosses[end])_"
* "$starttimestr.bson"),
meta_model=tocpu(model), meta_optim=tocpu(optim),
meta_trainlosses=trainlosses, meta_meanlosses=meanlosses,
meta_varlosses=varlosses, steps=steps)
catch e
bson(joinpath(logdir, "meta-cp-no-optim_$steps-steps_loss-$(trainlosses[end])_"
* "$starttimestr.bson"),
meta_model=tocpu(model), meta_optim=nothing,
meta_trainlosses=trainlosses, meta_meanlosses=meanlosses,
meta_varlosses=varlosses, steps=steps)
end
end
function save_cp(model, optim, trainlosses, meanlosses,
varlosses, steps, logdir, starttimestr, use_bson::Val{false})
# TODO Due to having to use a different BSON PR branch, this fails.
# When the branch is merged, update the package and remove the try-catch.
# We use this signature so we don't use `mmap`, trading speed for stability.
# See https://github.com/JuliaIO/JLD2.jl/issues/55
jldopen(joinpath(logdir, "meta-cp_$steps-steps_loss-$(trainlosses[end])_"
* "$starttimestr.jld2"), true, true, true, IOStream,
compress=true) do io
# addrequire(io, :Flux)
write(io, "meta_model", tocpu(model))
write(io, "meta_optim", tocpu(optim))
write(io, "meta_trainlosses", trainlosses)
write(io, "meta_meanlosses", meanlosses)
write(io, "meta_varlosses", varlosses)
write(io, "steps", steps)
end
end
function load_cp(cppath::AbstractString, use_bson::Val{true})
cp = BSON.load(cppath)
load_cp(cp, Symbol)
end
function load_cp(cppath::AbstractString, use_bson::Val{false})
jldopen(cppath) do cp
load_cp(cp, String)
end
end
function load_cp(cp, cpkeytype::Type)
model = togpu(cp[cpkeytype("meta_model")]::Flux.Chain)
optim::Flux.ADAM = togpu(cp[cpkeytype("meta_optim")])
trainlosses::Vector{Float32} = cp[cpkeytype("meta_trainlosses")]
meanlosses::Vector{eltype(trainlosses)} = cp[cpkeytype("meta_meanlosses")]
varlosses::Vector{eltype(trainlosses)} = cp[cpkeytype("meta_varlosses")]
past_steps::UInt64 = cp[cpkeytype("steps")]
return (model, optim, trainlosses, meanlosses, varlosses, past_steps)
end
end # module