我正在尝试使用RStudio的Keras接口使神经网络训练可重现。在R脚本(set.seed(42)
)中设置种子似乎不起作用。是否可以将种子作为参数传递给layer_dense()
?我可以选择RandomUniform
作为初始化程序,但是我很难通过种子论证。以下行引发错误:
model %>% layer_dense(units = 12, activation = 'relu', input_shape = c(8), kernel_initializer = "RandomUniform(seed=1)")
但是可以在不尝试传递种子参数的情况下添加图层:
model %>% layer_dense(units = 12, activation = 'relu', input_shape = c(8), kernel_initializer = "RandomUniform")
RandomUniform
假设根据Keras initializer documents接受种子参数。
答案 0 :(得分:0)
kernel initializer参数语法应该是这样的。 kernel_initializer=initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 104)
尝试以下步骤。
1)在导入keras / tensorflow之前为R环境设置种子
2)设置tensorflow会话配置以使用单线程
3)设置张量流随机种子
4)使用此种子创建tensorflow会话并将其分配给keras后端。
5)最后在你的模型层中,如果你使用随机初始化器,如random_uniform(这是默认值)或random_normal,那么你必须将seed参数更改为某个整数 以下是一个例子
# Set R random seed
set.seed(104)
library(keras)
library(tensorflow)
# TensorFlow session configuration that uses only a single thread. Multiple threads are a
# potential source of non-reproducible results, see: https://stackoverflow.com/questions/42022950/which-seeds-have-to-be-set-where-to-realize-100-reproducibility-of-training-res
#session_conf <- tf$ConfigProto(intra_op_parallelism_threads = 1L,
# inter_op_parallelism_threads = 1L)
# Set TF random seed (see: https://www.tensorflow.org/api_docs/python/tf/set_random_seed)
tf$set_random_seed(104)
# Create the session using the custom configuration
sess <- tf$Session(graph = tf$get_default_graph(), config = session_conf)
# Instruct Keras to use this session
K <- backend()
K$set_session(sess)
#Then in your model architecture, set seed to all random initializers.
model %>%
layer_dense(units = n_neurons, activation = 'relu', input_shape = c(100),kernel_initializer=initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 104)) %>%
layer_dense(units = n_neurons, activation = 'relu',kernel_initializer=initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 104)) %>%
layer_dense(units =c(100) ,kernel_initializer=initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 104))
参考文献: https://rstudio.github.io/keras/articles/faq.html#how-can-i-obtain-reproducible-results-using-keras-during-development https://rstudio.github.io/keras/reference/initializer_random_normal.html#arguments
答案 1 :(得分:0)
library(keras)
use_session_with_seed(42)
use_session_with_seed()函数为R,Python,Numpy和Tensorflow建立公共随机种子。有关更多详细信息,请参见https://keras.rstudio.com/articles/faq.html