1
$\begingroup$

I'm using PyTorch (1.7.1), PyTorch Geometric (1.6.3), NVIDIA Cuda (11.2).

I need to make a neural network reproducible for a competition.

However, when I try:

device = torch.device('cuda:0')
rand = 123
torch.manual_seed(rand)
torch.cuda.manual_seed(rand)
torch.cuda.manual_seed_all(rand)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(rand)
random.seed(rand)

the results are different each time I run the code. How can I fix it?

(For a reference, it always comes out the same in device = torch.device('cpu').)

$\endgroup$
0

1 Answer 1

2
$\begingroup$

There is this nice function from Kaggle:

def seed_everything(seed=1234):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

seed_everything()

There are several version of this function for torch, tensorflow and combinations. This specific one is from here.

Run this before you run your model. But not only the fitting function. It needs to be before the validation split function as well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.