Difference between two tensors using the same device #1316
-
|
I’m working on the fundamentals exercise. I'm trying to understand why tensor a and b yield different results when the device assignment is as follows(assuming device=cuda): torch.manual_seed(1234) Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
|
I was working on the same problem and observed the same thing on Colab. torch.manual_seed(1234) This will definitely Solve your problem |
Beta Was this translation helpful? Give feedback.
-
|
You need to redefine the manual seed when generating both tensors. When u use It must be: |
Beta Was this translation helpful? Give feedback.
Try This one
NOTE: CPU and GPU use different random generators.
a was created on CPU first, then moved to GPU.
b was created directly on GPU.
Same seed ≠ same result if tensors are created on different devices.
Mistake:
You thought one torch.manual_seed() controls both CPU and GPU randomness.
Why your fix works:
You set the seed again before creating the tensor on the same device, so both tensors match.
CODE:
import torch
device = "cuda"
torch.manual_seed(1234)
a = torch.rand(size=(2, 3)).to(device)
torch.manual_seed(1234)
b = torch.rand(size=(2, 3)).to(device)
a==b
OUTPUT: tensor([[True, True, True],
[True, True, True]], device='cuda:0')