Skip to content

Instantly share code, notes, and snippets.

@JulienPascal
Last active January 13, 2020 15:20
Show Gist options
  • Save JulienPascal/873f516013f261d613b88c7069647e6a to your computer and use it in GitHub Desktop.
Save JulienPascal/873f516013f261d613b88c7069647e6a to your computer and use it in GitHub Desktop.
CNN with Julia
# Training loop
# See: https://github.com/FluxML/model-zoo/blob/master/vision/mnist/conv.jl
best_acc = 0.0
last_improvement = 0
accuracy_target = 0.97 #Set an accuracy target. When reached, we stop training.
max_epochs = 100 #Maximum
for epoch_idx in 1:100
global best_acc, last_improvement
# Train for a single epoch
Flux.train!(loss, params(model), train_set, opt)
# Calculate accuracy:
acc = accuracy(train_set_full...)
@info(@sprintf("[%d]: Train accuracy: %.4f", epoch_idx, acc))
# Calculate accuracy:
acc = accuracy(test_set...)
@info(@sprintf("[%d]: Test accuracy: %.4f", epoch_idx, acc))
# If our accuracy is good enough, quit out.
if acc >= accuracy_target
@info(" -> Early-exiting: We reached our target accuracy of $(accuracy_target*100)%")
break
end
if epoch_idx - last_improvement >= 10
@warn(" -> We're calling this converged.")
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment