Skip to content

Instantly share code, notes, and snippets.

@mohcinemadkour
Created July 25, 2020 05:45
Show Gist options
  • Save mohcinemadkour/8fa1925dcb4e65458444f9f325f439f1 to your computer and use it in GitHub Desktop.
Save mohcinemadkour/8fa1925dcb4e65458444f9f325f439f1 to your computer and use it in GitHub Desktop.
### Below is training code, uncomment to train your own model... ###
### Note: You need GPU and CUDA to run this section ###
'''
# Define networks
lenet1 = [LeNetClassifier(droprate=0, max_epoch=1500),
LeNetClassifier(droprate=0.5, max_epoch=1500)]
# Training, set verbose=True to see loss after each epoch.
[lenet.fit(trainset, testset,verbose=False) for lenet in lenet1]
# Save torch models
for ind, lenet in enumerate(lenet1):
torch.save(lenet.model, 'mnist_lenet1_'+str(ind)+'.pth')
# Prepare to save errors
lenet.test_error = list(map(str, lenet.test_error))
# Save test errors to plot figures
open("lenet1_test_errors.txt","w").write('\n'.join([','.join(lenet.test_error) for lenet in lenet1]))
'''
# Load saved models to CPU
lenet1_models = [torch.load('mnist_lenet1_'+str(ind)+'.pth', map_location={'cuda:0': 'cpu'}) for ind in [0,1]]
# Load saved test errors to plot figures.
lenet1_test_errors = [error_array.split(',') for error_array in
open("lenet1_test_errors.txt","r").read().split('\n')]
lenet1_test_errors = np.array(lenet1_test_errors,dtype='f')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment