Flux.jl/src/training.jl

40 lines
794 B
Julia
Raw Normal View History

2017-05-01 12:43:07 +00:00
"""
@cb for ... end t expr
Run the for loop, executing `expr` every `t` seconds.
"""
macro cb(ex, t, f)
@assert isexpr(ex, :for)
cond, body = ex.args
@esc t f cond body
:(let
t0 = time_ns()
dt = $t*1e9
@progress $(Expr(:for, cond, quote
t = time_ns()
if t - t0 > dt
t0 = t
2017-05-01 13:00:39 +00:00
f = () -> $f
2017-05-01 12:43:07 +00:00
f()
end
$body
end))
end)
end
function train!(m, train; cb = [],
2017-05-01 11:41:54 +00:00
epoch = 1, η = 0.1, loss = mse)
2017-05-01 11:46:02 +00:00
@progress for e in 1:epoch
2017-05-01 11:41:54 +00:00
info("Epoch $e")
2017-05-01 12:43:07 +00:00
@cb for (x, y) in train
2017-05-01 11:41:54 +00:00
x, y = tobatch.((x, y))
= m(x)
any(isnan, ) && error("NaN")
Δ = back!(loss, 1, , y)
back!(m, Δ, x)
update!(m, η)
2017-05-01 12:43:07 +00:00
end 5 foreach(f -> f(), cb)
2017-05-01 11:41:54 +00:00
end
return m
end