首页 > 解决方案 > 使用 R Keras 包时,如何获取在 R 中每次调整运行中使用的标志值?

问题描述

我正在尝试使用 keras 包在 R 中使用标志和 tune_run 调整我的完全连接的深度学习模型的超参数。我在哪里可以找到每次运行中使用的实际标志值?

我尝试查找生成的结果数据框和运行/文件夹中使用的超参数值。虽然所有关于运行的准确度值、损失函数和其他元细节都在那里,但不包括生成这些结果的超参数(我遵循了这里给出的这个例子:https ://tensorflow.rstudio.com/tools/tfruns /articles/tuning.html)。我正在调用我的 tune_run,如下所示

runs <- tuning_run("test.R", flags = list(dropout1=c(0.5,0.4,0.3),dropout2=c(0.3,0.2),dense_units=c(128,256)),sample=0.3)

我的模型消耗像这样的标志

model <- keras_model_sequential()
model %>% 
  layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% 
  layer_dropout(rate = FLAGS$dropout_1) %>% 
  layer_dense(units = FLAGS$dense_units, activation = 'relu') %>%
  layer_dropout(rate = FLAGS$dropout_2) %>%
  layer_dense(units = 10, activation = 'softmax')

当我运行它时,稍后查找为(运行数据帧)生成一定验证准确性的标志的值这是我观察到的

Data frame: 2 x 25 
                    run_dir eval_loss eval_acc metric_loss metric_acc
1 runs/2019-03-29T00-14-10Z    0.1315   0.9794      0.0075     0.9977
2 runs/2019-03-29T00-10-37Z    0.1326   0.9816      0.0096     0.9973
  metric_val_loss metric_val_acc
1          0.1475         0.9794
2          0.1443         0.9794
# ... with 18 more columns:
#   samples, validation_samples, batch_size, epochs, epochs_completed,
#   metrics, model, loss_function, optimizer, learning_rate, script, start,
#   end, completed, output, source_code, context, type

我想知道在哪里可以找到每次迭代中使用的标志值。还是我做错了什么?任何帮助,将不胜感激。谢谢!

标签: rmachine-learningkerasdeep-learninghyperparameters

解决方案


我发现了问题所在。这些标志也需要在目标脚本中定义,以便 keras 报告它。这就是为什么它没有在结果帧中显示标志。

一旦我将这些行添加到 test.R 它工作正常

FLAGS <- flags(
  flag_numeric('dropout_1', 0.04, 'First dropout'),
  flag_numeric('dropout_2', 0.3, 'Second dropout'),
  flag_integer('dense_units', 128, 'Units in dense layer')
)

同样的问题和解决方案在这里讨论:https ://github.com/rstudio/tfruns/issues/24


推荐阅读