Reproducability #468
-
Hi guys, I have a doubt regarding using a random seed for the reproducibility of tensors and their operations. In video no. 36 where @mrdbourke demonstrates an example code, he sets the manual seed for both the tensors individually. However, setting the seed only once produces a different result. I have tried something like this in the Exercise provided at the end of the module. ![]() ![]() What is the difference between these 2 methods? Any help will be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Coderbitxh, I think the problem is that the explanation is a bit off with what a seed is. Seeds are used to have a know starting point, so each consecutive call to a random method uses that seed. It does not only apply to the first call, it is for all consecutive calls. When you set the seed, the first time you call random it will always return the same value* . Same happens for the rest of calls, the second return the same value each time (not the same as the first call), and so on. Maybe an example is better. With the example code, we can modify it to call to times the rand function and get C and D. The next time you run it, you will get the same values for C and D. This is illustrated in code by resetting the seed to 42 and calling rand again and getting c2 and d2. Both have the same value than the previous call. import torch
import random
# # Set the random seed
RANDOM_SEED=42
torch.manual_seed(seed=RANDOM_SEED)
random_tensor_C = torch.rand(3, 4)
random_tensor_D = torch.rand(3, 4)
print(f"Tensor C:\n{random_tensor_C}\n")
print(f"Tensor D:\n{random_tensor_D}\n")
## Imagine this is just a second execution of the code
torch.manual_seed(seed=RANDOM_SEED)
random_tensor_C2 = torch.rand(3, 4)
random_tensor_D2 = torch.rand(3, 4)
print(f"Same value of C and C2 and D and D2?")
random_tensor_C == random_tensor_C2, random_tensor_D == random_tensor_D2 This is useful if you are coding something and it does not work. It would be really difficult to search for the error if the data is changing all the time. In this way you can run the code 20 times with the sames values. If you want new data, you change the seed to another number and get new values for the rand calls. If you like games maybe you have used the seed in minecraft, that allows you to create the same work consistently.
|
Beta Was this translation helpful? Give feedback.
Hi @Coderbitxh,
I think the problem is that the explanation is a bit off with what a seed is.
Seeds are used to have a know starting point, so each consecutive call to a random method uses that seed. It does not only apply to the first call, it is for all consecutive calls.
When you set the seed, the first time you call random it will always return the same value* . Same happens for the rest of calls, the second return the same value each time (not the same as the first call), and so on.
Maybe an example is better. With the example code, we can modify it to call to times the rand function and get C and D. The next time you run it, you will get the same values for C and D. This is illustra…