2016-10-29 22:36:39 +00:00
|
|
|
export AArray
|
2016-08-22 20:13:28 +00:00
|
|
|
|
|
|
|
const AArray = AbstractArray
|
2016-04-01 21:11:42 +00:00
|
|
|
|
2017-02-02 04:39:41 +00:00
|
|
|
initn(dims...) = randn(dims...)/100
|
2016-08-25 16:25:33 +00:00
|
|
|
|
2017-04-19 13:23:48 +00:00
|
|
|
tobatch(xs::Batch) = rawbatch(xs)
|
2017-04-19 16:18:40 +00:00
|
|
|
tobatch(xs) = tobatch(batchone(xs))
|
2017-04-19 13:23:48 +00:00
|
|
|
|
|
|
|
function train!(m, train, test = []; epoch = 1, η = 0.1)
|
2016-08-23 22:58:39 +00:00
|
|
|
i = 0
|
|
|
|
for _ in 1:epoch
|
2016-09-06 17:37:39 +00:00
|
|
|
@progress for (x, y) in train
|
2017-04-19 13:23:48 +00:00
|
|
|
x, y = tobatch.((x, y))
|
2016-08-23 22:58:39 +00:00
|
|
|
i += 1
|
2017-04-19 13:23:48 +00:00
|
|
|
ŷ = m(x)
|
|
|
|
any(isnan, ŷ) && error("NaN")
|
|
|
|
Δ = back!(mse, 1, ŷ, y)
|
2016-12-15 21:37:39 +00:00
|
|
|
back!(m, Δ, x)
|
2017-04-19 13:23:48 +00:00
|
|
|
update!(m, η)
|
2016-08-25 22:12:16 +00:00
|
|
|
i % 1000 == 0 && @show accuracy(m, test)
|
2016-08-23 22:58:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return m
|
|
|
|
end
|
|
|
|
|
2016-09-29 19:50:43 +00:00
|
|
|
function accuracy(m, data)
|
2016-08-23 22:58:39 +00:00
|
|
|
correct = 0
|
|
|
|
for (x, y) in data
|
2017-04-19 13:23:48 +00:00
|
|
|
x, y = tobatch.((x, y))
|
|
|
|
correct += sum(onecold(m(x)) .== onecold(y))
|
2016-08-23 22:58:39 +00:00
|
|
|
end
|
|
|
|
return correct/length(data)
|
|
|
|
end
|