SAN*_*les 5 r rstudio keras keras-layer
我正在尝试使用 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 初始值设定项文档采用种子参数。
内核初始化参数语法应该是这样的。kernel_initializer=initializer_random_uniform(minval = -0.05, maxval = 0.05, seed = 104)
尝试这些步骤。
1)导入keras/tensorflow之前为R环境设置种子
2)将tensorflow会话配置设置为使用单线程
3)设置tensorflow随机种子
4) 使用此种子创建 TensorFlow 会话并将其分配给 keras 后端。
5)最后在模型层中,如果您使用随机初始化器,例如 random_uniform(这是默认的)或 random_normal,那么您必须将种子参数更改为某个整数下面是一个示例
# 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: /sf/ask/2941606531/
#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))
Run Code Online (Sandbox Code Playgroud)
参考文献: 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
| 归档时间: |
|
| 查看次数: |
4640 次 |
| 最近记录: |