Open njtierney opened 3 weeks ago
Somewhat confusingly it seems you can use set.seed()
or tensorflow::set_random_seed()
, and they are usually equivalent to each other, e.g.,
devtools::load_all(".")
#> ℹ Loading greta
#> ℹ Initialising python and checking dependencies, this may take a moment.
#>
#> ✔ Initialising python and checking dependencies ... done!
a <- normal(0, 1)
y <- normal(a, 1)
m <- model(y)
set.seed(12345)
c_one <- calculate(y, nsim = 1)
set.seed(12345)
c_two <- calculate(y, nsim = 1)
as.numeric(c_one)
#> [1] 1.236408
as.numeric(c_two)
#> [1] 1.236408
all.equal(as.numeric(c_one), as.numeric(c_two))
#> [1] TRUE
c_one <- calculate(y, nsim = 1, seed = 12345)
c_two <- calculate(y, nsim = 1, seed = 12345)
as.numeric(c_one)
#> [1] -1.84305
as.numeric(c_two)
#> [1] -1.84305
all.equal(as.numeric(c_one), as.numeric(c_two))
#> [1] TRUE
set.seed(12345)
one <- mcmc(m, n_samples = 1, chains = 1)
#> warmup 0/1000 | eta: ?s warmup == 50/1000 | eta: 13s warmup ==== 100/1000 | eta: 7s warmup ====== 150/1000 | eta: 5s warmup ======== 200/1000 | eta: 4s warmup ========== 250/1000 | eta: 3s warmup =========== 300/1000 | eta: 2s warmup ============= 350/1000 | eta: 2s warmup =============== 400/1000 | eta: 2s warmup ================= 450/1000 | eta: 2s warmup =================== 500/1000 | eta: 1s warmup ===================== 550/1000 | eta: 1s warmup ======================= 600/1000 | eta: 1s warmup ========================= 650/1000 | eta: 1s warmup =========================== 700/1000 | eta: 1s warmup ============================ 750/1000 | eta: 1s warmup ============================== 800/1000 | eta: 0s warmup ================================ 850/1000 | eta: 0s warmup ================================== 900/1000 | eta: 0s warmup ==================================== 950/1000 | eta: 0s warmup ====================================== 1000/1000 | eta: 0s
#> sampling 0/1 | eta: ?s
set.seed(12345)
two <- mcmc(m, n_samples = 1, chains = 1)
#> warmup 0/1000 | eta: ?s warmup == 50/1000 | eta: 10s warmup ==== 100/1000 | eta: 5s warmup ====== 150/1000 | eta: 4s warmup ======== 200/1000 | eta: 3s warmup ========== 250/1000 | eta: 2s warmup =========== 300/1000 | eta: 2s warmup ============= 350/1000 | eta: 2s warmup =============== 400/1000 | eta: 2s warmup ================= 450/1000 | eta: 1s warmup =================== 500/1000 | eta: 1s warmup ===================== 550/1000 | eta: 1s warmup ======================= 600/1000 | eta: 1s warmup ========================= 650/1000 | eta: 1s warmup =========================== 700/1000 | eta: 1s warmup ============================ 750/1000 | eta: 1s warmup ============================== 800/1000 | eta: 0s warmup ================================ 850/1000 | eta: 0s warmup ================================== 900/1000 | eta: 0s warmup ==================================== 950/1000 | eta: 0s warmup ====================================== 1000/1000 | eta: 0s
#> sampling 0/1 | eta: ?s
as.numeric(one)
#> [1] 1.609063
as.numeric(two)
#> [1] 1.609063
all.equal(as.numeric(one), as.numeric(two))
#> [1] TRUE
tensorflow::set_random_seed(12345)
one_tf <- mcmc(m, n_samples = 1, chains = 1)
#> warmup 0/1000 | eta: ?s warmup == 50/1000 | eta: 10s warmup ==== 100/1000 | eta: 6s warmup ====== 150/1000 | eta: 4s warmup ======== 200/1000 | eta: 3s warmup ========== 250/1000 | eta: 3s warmup =========== 300/1000 | eta: 2s warmup ============= 350/1000 | eta: 2s warmup =============== 400/1000 | eta: 2s warmup ================= 450/1000 | eta: 1s warmup =================== 500/1000 | eta: 1s warmup ===================== 550/1000 | eta: 1s warmup ======================= 600/1000 | eta: 1s warmup ========================= 650/1000 | eta: 1s warmup =========================== 700/1000 | eta: 1s warmup ============================ 750/1000 | eta: 1s warmup ============================== 800/1000 | eta: 0s warmup ================================ 850/1000 | eta: 0s warmup ================================== 900/1000 | eta: 0s warmup ==================================== 950/1000 | eta: 0s warmup ====================================== 1000/1000 | eta: 0s
#> sampling 0/1 | eta: ?s
tensorflow::set_random_seed(12345)
two_tf <- mcmc(m, n_samples = 1, chains = 1)
#> warmup 0/1000 | eta: ?s warmup == 50/1000 | eta: 10s warmup ==== 100/1000 | eta: 5s warmup ====== 150/1000 | eta: 4s warmup ======== 200/1000 | eta: 3s warmup ========== 250/1000 | eta: 2s warmup =========== 300/1000 | eta: 2s warmup ============= 350/1000 | eta: 2s warmup =============== 400/1000 | eta: 2s warmup ================= 450/1000 | eta: 1s warmup =================== 500/1000 | eta: 1s warmup ===================== 550/1000 | eta: 1s warmup ======================= 600/1000 | eta: 1s warmup ========================= 650/1000 | eta: 1s warmup =========================== 700/1000 | eta: 1s warmup ============================ 750/1000 | eta: 1s warmup ============================== 800/1000 | eta: 0s warmup ================================ 850/1000 | eta: 0s warmup ================================== 900/1000 | eta: 0s warmup ==================================== 950/1000 | eta: 0s warmup ====================================== 1000/1000 | eta: 0s
#> sampling 0/1 | eta: ?s
as.numeric(one_tf)
#> [1] 1.609063
as.numeric(two_tf)
#> [1] 1.609063
all.equal(as.numeric(one_tf), as.numeric(two_tf))
#> [1] TRUE
Created on 2024-11-07 with reprex v2.1.1
However when writing a test for this in test_seed.R
, running this locally I get the same for both TF set seed, and set.seed, but running it as a test I do not...something to come back to perhaps.
test_that("mcmc samples are the same when the R seed is the same, also with tf set seed", {
skip_if_not(check_tf_version())
a <- normal(0, 1)
y <- normal(a, 1)
m <- model(y)
set.seed(12345)
one <- mcmc(m, warmup = 10, n_samples = 1, chains = 1)
set.seed(12345)
two <- mcmc(m, warmup = 10, n_samples = 1, chains = 1)
expect_equal(
as.numeric(one),
as.numeric(two)
)
tensorflow::set_random_seed(12345)
one_tf <- mcmc(m, warmup = 10, n_samples = 1, chains = 1)
tensorflow::set_random_seed(12345)
two_tf <- mcmc(m, warmup = 10, n_samples = 1, chains = 1)
expect_equal(
as.numeric(one_tf),
as.numeric(two_tf)
)
# this fails
expect_equal(as.numeric(one, one_tf))
expect_equal(as.numeric(two, two_tf))
# this doesn't
mcmc_matches_tf_one <- identical(as.numeric(one),as.numeric(one_tf))
mcmc_matches_tf_two <- identical(as.numeric(two),as.numeric(two_tf))
expect_false(mcmc_matches_tf_one)
expect_false(mcmc_matches_tf_two)
})
See https://github.com/greta-dev/greta/issues/559
And also update "note" docs in
mcmc()