fsschneider / DeepOBS

DeepOBS: A Deep Learning Optimizer Benchmark Suite
MIT License
103 stars 34 forks source link

Deterministic rotation/Hessian #30

Closed f-dangel closed 4 years ago

f-dangel commented 4 years ago

Background: I fixed the seed of torch/scipy/numpy/random and created a quadratic_deep test problem. Then I performed a forward pass. Then I did exactly the same a second time and expected the result of the forward pass to be the same. This was not true.

Reason: Creating two quadratic_deep problems results in different Hessians, as the random number generator used to generate them lives on the module level, and I could not 'reset' it.

This PR moves the random number generator to the functional level.

Demo:

import random

import numpy

import torch
from deepobs.pytorch.testproblems import quadratic_deep

# set seed
seed = 0
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)

# first problem
tp1 = quadratic_deep(5)
tp1.set_up()
h1 = tp1._hessian

# set seed
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)

# second problem
tp2 = quadratic_deep(5)
tp2.set_up()
h2 = tp2._hessian

# should be the same Hessian
print(torch.allclose(h1, h2))
fsschneider commented 4 years ago

Thanks! We did not anticipate that someone would use two quadratic problems without restarting. But this PR fixes this. Great!

f-dangel commented 4 years ago

Adding a mental note that the behavior is now different from the tensorflow implementation