greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
528 stars 63 forks source link

specific seed is only respected after tf has been initialised? #427

Open goldingn opened 3 years ago

goldingn commented 3 years ago

greta should respect the RNG seed specified in R (greta explicitly passes the latest RNG state into TF, and increments the RNG state in R), and it does, but apparently only after TF has been initialised.

In the below reprex, the values af a, b, and c should all be the same, but the value for a (seed set before fir greta array created and initialisation of python) is different than the other two.

library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
#>     %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#>     eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#>     tapply
set.seed(1)
x <- normal(0, 1)
#> ℹ Initialising python and checking dependencies
#> ✓ Initialising python and checking dependencies ... done
#> 
a <- calculate(x, nsim = 1)

set.seed(1)
y <- normal(0, 1)
b <- calculate(y, nsim = 1)

set.seed(1)
z <- normal(0, 1)
c <- calculate(z, nsim = 1)

a
#> $x
#> , , 1
#> 
#>          [,1]
#> [1,] 1.304048
b
#> $y
#> , , 1
#> 
#>           [,1]
#> [1,] 0.4455042
c
#> $z
#> , , 1
#> 
#>           [,1]
#> [1,] 0.4455042

Created on 2021-06-09 by the reprex package (v2.0.0)

sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] greta_0.3.1.9012

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        highr_0.8         compiler_3.6.2    pillar_1.4.3     
 [5] prettyunits_1.1.1 base64enc_0.1-3   tools_3.6.2       progress_1.2.2   
 [9] digest_0.6.27     evaluate_0.14     jsonlite_1.7.2    lifecycle_1.0.0  
[13] tibble_3.0.3      lattice_0.20-38   pkgconfig_2.0.3   png_0.1-7        
[17] rlang_0.4.11      reprex_2.0.0      Matrix_1.2-18     cli_2.5.0        
[21] rstudioapi_0.13   yaml_2.2.1        parallel_3.6.2    xfun_0.22        
[25] coda_0.19-4       knitr_1.33        withr_2.4.2       styler_1.1.1     
[29] vctrs_0.3.8       fs_1.5.0          globals_0.14.0    hms_1.1.0        
[33] grid_3.6.2        reticulate_1.20   glue_1.4.2        listenv_0.8.0    
[37] R6_2.5.0          processx_3.5.2    parallelly_1.25.0 rmarkdown_2.8    
[41] callr_3.7.0       clipr_0.7.1       purrr_0.3.4       whisker_0.4      
[45] magrittr_2.0.1    ps_1.6.0          htmltools_0.4.0   backports_1.1.5  
[49] codetools_0.2-16  tfruns_1.5.0      ellipsis_0.3.2    future_1.21.0    
[53] tensorflow_2.4.0  crayon_1.4.1    
goldingn commented 3 years ago

a workaround is to initialise before setting the RNG seed:

. <- variable()
set.seed(2020-06-09)