Skip to content

Instantly share code, notes, and snippets.

@gndowns
Created July 8, 2019 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gndowns/d4267f6e54c1c72ffc69e6a4b5baacbf to your computer and use it in GitHub Desktop.
Save gndowns/d4267f6e54c1c72ffc69e6a4b5baacbf to your computer and use it in GitHub Desktop.
FastAI Reproducibility Experiment
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