Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making neural network training reproducible using RStudio's Keras interface

I'm trying to make neural network training reproducible using RStudio's Keras interface. Setting a seed in the R script (set.seed(42)) doesn't seem to work. Is it possible to pass seeding as an argument to layer_dense()? I can choose RandomUniform as an initializer but I'm having difficulty passing a seeding argument along with it. The following line throws an error:

model %>% layer_dense(units = 12, activation = 'relu', input_shape = c(8), kernel_initializer = "RandomUniform(seed=1)")

But a layer can be added without the attempt to pass a seed argument:

model %>% layer_dense(units = 12, activation = 'relu', input_shape = c(8), kernel_initializer = "RandomUniform")

RandomUniform is suppose to take a seed argument according to the Keras initializer documents.

like image 723
SANBI samples Avatar asked Sep 11 '25 02:09

SANBI samples


2 Answers

library(keras)
use_session_with_seed(42)

The use_session_with_seed() function establishes a common random seed for R, Python, Numpy, and Tensorflow. For further details, see https://keras.rstudio.com/articles/faq.html

As of TensorFlow 2.0:

library(tensorflow)
tensorflow::set_random_seed(42)

See also this discussion.

like image 40
dfrankow Avatar answered Sep 16 '25 09:09

dfrankow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!