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
|
|
|
|
|
|
|
function train!(m::Model, train, test = []; epoch = 1, batch = 10, η = 0.1)
|
2016-08-25 16:25:33 +00:00
|
|
|
initn(dims...) = randn(dims...)/100
|
|
|
|
|
2016-08-23 22:58:39 +00:00
|
|
|
i = 0
|
|
|
|
∇ = zeros(length(train[1][2]))
|
|
|
|
for _ in 1:epoch
|
|
|
|
for (x, y) in shuffle!(train)
|
|
|
|
i += 1
|
|
|
|
err = mse!(∇, m(x), y)
|
2016-08-24 11:11:34 +00:00
|
|
|
back!(m, ∇, x)
|
2016-08-23 22:58:39 +00:00
|
|
|
i % batch == 0 && update!(m, η/batch)
|
|
|
|
end
|
|
|
|
@show accuracy(m, test)
|
|
|
|
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
|