【问题标题】:Making neural network training reproducible using RStudio's Keras interface使用 RStudio 的 Keras 界面使神经网络训练具有可重复性
【发布时间】:2017-11-19 08:13:32
【问题描述】:

我正在尝试使用 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 采用种子参数。

【问题讨论】:

  • 我在 python 中使用 keras,当我使用 set.seed(42)import tensorflowtensorflow.set_seed(42) 时,它似乎可以工作。您可以在 R 中显式导入 tensorflow 并尝试吗?此外,它仅适用于 CPU,不适用于 GPU。
  • 我想我应该尝试使用 R Tensorflow 库而不是 R Keras 库,因为 Keras 集成在 Tensorflow 1.2 中

标签: r rstudio keras keras-layer


【解决方案1】:
library(keras)
use_session_with_seed(42)

use_session_with_seed() 函数为 R、Python、Numpy 和 Tensorflow 建立公共随机种子。更多详情请见https://keras.rstudio.com/articles/faq.html

【讨论】:

    【解决方案2】:

    内核初始化参数语法应该是这样的。 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: 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多