Skip to content
Discussion options

You must be logged in to vote

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')

Replies: 2 comments 14 replies

Comment options

You must be logged in to vote
4 replies
@ShayLevy
Comment options

@Haji-Suleman
Comment options

@Haji-Suleman
Comment options

@ShayLevy
Comment options

Comment options

You must be logged in to vote
10 replies
@ShayLevy
Comment options

@ohecker-logicalredstone
Comment options

@ShayLevy
Comment options

@Haji-Suleman
Comment options

Answer selected by mrdbourke
@ShayLevy
Comment options

@Haji-Suleman
Comment options

@mrdbourke
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants