Skip to content

Instantly share code, notes, and snippets.

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