Flux.jl/src/training.jl

33 lines
727 B
Julia
Raw Normal View History

2017-05-01 11:41:54 +00:00
tobatch(xs::Batch) = rawbatch(xs)
tobatch(xs) = tobatch(batchone(xs))
function accuracy(m, data)
2017-05-01 12:40:11 +00:00
n = 0
2017-05-01 11:41:54 +00:00
correct = 0
for (x, y) in data
x, y = tobatch.((x, y))
2017-05-01 12:40:11 +00:00
n += size(x, 1)
2017-05-01 11:41:54 +00:00
correct += sum(onecold(m(x)) .== onecold(y))
end
2017-05-01 12:40:11 +00:00
return correct/n
2017-05-01 11:41:54 +00:00
end
function train!(m, train, test = [];
epoch = 1, η = 0.1, loss = mse)
i = 0
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")
@progress for (x, y) in train
x, y = tobatch.((x, y))
i += 1
= m(x)
any(isnan, ) && error("NaN")
Δ = back!(loss, 1, , y)
back!(m, Δ, x)
update!(m, η)
i % 1000 == 0 && @show accuracy(m, test)
end
end
return m
end