Reputation: 69
I got the results by running the code provided in this link Neural Network – Predicting Values of Multiple Variables. I was able to compute losses accuracy etc. However, every time I run this code, I get a new result. Is it possible to get the same (consistent) result?
Upvotes: 4
Views: 4870
Reputation: 81
You have to set the seed for different things like numpy, cuda etc.. Then your program gives same result. Use below function
def set_random_seed(random_seed):
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)
Then call with a seed set_random_seed(1234)
This will give you same result no matter on which machine you run. You can change the seed if you want. Different seed
results in different results.
Upvotes: 1
Reputation: 18367
The code is full of random.randint()
everywhere! Furthermore, the weights are most of the time randomly set aswell, and the batch_size also has an influence (although pretty minor) in the result.
adam
as optimizer, means you'll be performing stochastic gradient descent. With a random beginning point of the iterations in order to converge.Solution:
np.random.seed()
If I find a way to have consistente sampling methods for the batch_size
/epoch
issue I will edit my answer.
Upvotes: 4
Reputation: 8112
There are lots of random arrays in there. Use np.random.seed()
to get the same ones each time. For example:
np.random.seed(42)
for _ in range(3):
print(np.random.random(3))
Every time you run this code, you'll get the same result. On my machine:
[0.37454012 0.95071431 0.73199394]
[0.59865848 0.15601864 0.15599452]
[0.05808361 0.86617615 0.60111501]
Note that lots of other bits of the machine learning pipeline use randomization too. For example:
train_test_split()
.Most ML functions allow you to pass a seed as an argument. Have a look in the documentation. Depending on what you are doing, and which libraries you're using, you may or may not be able to make the entire pipeline reproducible.
You might also like this article or this one about getting reproducible results with Keras.
Upvotes: 2