Created
July 8, 2019 17:41
-
-
Save gndowns/d4267f6e54c1c72ffc69e6a4b5baacbf to your computer and use it in GitHub Desktop.
FastAI Reproducibility Experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import numpy as np | |
import torch | |
from fastai.vision import * | |
def set_seeds(): | |
random.seed(42) | |
np.random.seed(12345) | |
torch.manual_seed(1234) | |
torch.backends.cudnn.deterministic = True | |
torch.backends.cudnn.benchmark = False | |
path = untar_data(URLs.MNIST_SAMPLE) | |
data = ImageDataBunch.from_folder(path) | |
model = models.resnet18 | |
# Trial 1: only set seeds once | |
print('Trial 1\n') | |
set_seeds() | |
learner = cnn_learner(data, model) | |
learner.fit_one_cycle(3, 1e-3) | |
print('\n') | |
learner = cnn_learner(data, model) | |
learner.fit_one_cycle(3, 1e-3) | |
print('\n\n') | |
# ====================================================== | |
# Trial 2: set seeds before each call to fit | |
print('Trial 2\n') | |
set_seeds() | |
learner = cnn_learner(data, model) | |
learner.fit_one_cycle(3, 1e-3) | |
print('\n') | |
set_seeds() | |
learner = cnn_learner(data, model) | |
learner.fit_one_cycle(3, 1e-3) | |
print('\n') | |
# ================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment