Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch_manual_seed does not seem to work with $sample() #937

Closed
LHBO opened this issue Dec 6, 2022 · 2 comments · Fixed by #938
Closed

torch_manual_seed does not seem to work with $sample() #937

LHBO opened this issue Dec 6, 2022 · 2 comments · Fixed by #938

Comments

@LHBO
Copy link

LHBO commented Dec 6, 2022

Hi,

This is my first time writing on GitHub, so I am unsure if I have included everything you need.

I have a question regarding reproducibility when generating random numbers from distributions.
From pages 36 and 37 in the manual (https://cran.r-project.org/web/packages/torch/torch.pdf) it seems that I should use the $sample() function, but then I am not able to set the seed and reproduce the sampled values even when setting the seed.
However, setting the seed works when I use $rsample(). Is there a particular reason for this?

P.S. I think you have done a splendid job in making torch much more available for us R users!

library(torch)
dn = distr_normal(0, 1)

# These calls do NOT generate the same value
torch_manual_seed(10)
dn$sample(1)
torch_manual_seed(10)
dn$sample(1)

# These calls do NOT generate the same value (checking if I needed to set seed both in R and torch)
torch_manual_seed(10)
set.seed(10)
dn$sample(1)
torch_manual_seed(10)
set.seed(10)
dn$sample(1)

# These calls do generate the same value
torch_manual_seed(10)
dn$rsample(1)
torch_manual_seed(10)
dn$rsample(1)
@LHBO
Copy link
Author

LHBO commented Dec 6, 2022

I should maybe add that I use torch version 0.9.0 (newest), R version 4.2.0 (2022-04-22), and I run on a mac with operating system x86_64-apple-darwin17.0.

Manually setting the seed works for e.g. distr_categorical.

library(torch)
dc = distr_categorical(torch_tensor(c(0.1, 0.5, 0.4)))

# Produce the same values
torch_manual_seed(1)
dc$sample(10)
torch_manual_seed(1)
dc$sample(10)
´´´

@dfalbel
Copy link
Member

dfalbel commented Dec 8, 2022

Hi @LHBO , thanks very much for reporting!

This is indeed a bug. It will be fixed once #938 is merged.
It will probably take some time to go into the CRAN version though.
Once the PR is merged you'll be able to install the dev version with

remotes::install_github("mlverse/torch")

LHBO added a commit to LHBO/shapr that referenced this issue Aug 25, 2023
… to them (mlverse/torch#937).

Also added check which removes the timing if it is not desired by the user. I did this mainly because of the test functions in the previous commits.
They would fail due to the times beeing different each time they ran this function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants