2016-08-22 20:13:28 +00:00
|
|
|
export AArray, onehot, onecold
|
|
|
|
|
|
|
|
const AArray = AbstractArray
|
2016-04-01 21:11:42 +00:00
|
|
|
|
|
|
|
onehot(label, labels) = [i == label for i in labels]
|
|
|
|
onecold(pred, labels = 1:length(pred)) = labels[findfirst(pred, maximum(pred))]
|
2016-08-23 22:58:39 +00:00
|
|
|
|
2016-08-25 16:25:33 +00:00
|
|
|
initn(dims...) = randn(dims...)/100
|
|
|
|
|
2016-08-25 21:49:21 +00:00
|
|
|
function train!(m, train, test = []; epoch = 1, batch = 10, η = 0.1)
|
2016-08-23 22:58:39 +00:00
|
|
|
i = 0
|
|
|
|
∇ = zeros(length(train[1][2]))
|
|
|
|
for _ in 1:epoch
|
2016-09-06 17:37:39 +00:00
|
|
|
@progress for (x, y) in train
|
2016-08-23 22:58:39 +00:00
|
|
|
i += 1
|
2016-08-25 21:49:21 +00:00
|
|
|
pred = m(x)
|
|
|
|
any(isnan, pred) && error("NaN")
|
|
|
|
err = mse!(∇, pred, y)
|
2016-08-24 11:11:34 +00:00
|
|
|
back!(m, ∇, x)
|
2016-08-25 22:12:16 +00:00
|
|
|
i % batch == 0 && update!(m, η)
|
|
|
|
i % 1000 == 0 && @show accuracy(m, test)
|
2016-08-23 22:58:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return m
|
|
|
|
end
|
|
|
|
|
|
|
|
function accuracy(m::Model, data)
|
|
|
|
correct = 0
|
|
|
|
for (x, y) in data
|
|
|
|
onecold(m(x)) == onecold(y) && (correct += 1)
|
|
|
|
end
|
|
|
|
return correct/length(data)
|
|
|
|
end
|