Hyperparameter调优

    概述

    优化模型通常需要研究更改对许多超参数的影响。解决这个问题的最佳方法通常不是像我们上面所做的那样更改训练脚本的源代码,而是为关键参数定义标志,然后根据这些标志的组合进行训练,以确定哪个标志组合产生最佳模型。

    培训的旗帜

    下面是控制模型中辍学率的两个标志的声明:

    flag_numeric“dropout1”0.4),flag_numeric“dropout2”0.3

    然后这些标志被用于模型的定义中:

    模型 % > % layer_dense单位 128激活 “relu”input_shape c784))% > % layer_dropout 旗帜dropout1% > % layer_dense单位 128激活 “relu”% > % layer_dropout 旗帜dropout2% > %

    一旦定义了标志,就可以将交替的标志值传递给training_run ()如下:

    您不需要指定所有标志(任何被排除的标志将使用它们的默认值)。

    标志使系统地探索超参数的变化对模型性能的影响变得非常简单,例如:

    标志值会自动包含在带有“flag_”前缀的运行数据中(例如:flag_dropout1flag_dropout2).

    参见下面的文章培训的旗帜有关使用标志的附加文档。

    优化运行

    上面我们演示了如何编写一个循环来调用training_run ()使用各种不同的标志值。更好的方法是tuning_run ()函数,该函数允许您为每个标志指定多个值,并为指定标志的所有组合执行训练运行。例如:

    运行 <- tuning_run“mnist_mlp。R”旗帜 列表dropout1 c0.20.30.4),dropout2 c0.20.30.4) ))#找到最好的评估准确性
    数据帧:9 x 28 run_dir eval_loss eval_acc metric_loss metric_acc metric_val_loss metric_val_acc 9分/ 2018 - 01 - 26 - t13 - 21 - 03 - z 0.1002 0.9817 0.0346 0.9900 0.1086 0.9794 6分/ 2018 - 01 - 26 - t13 z - 23 - 26 - 0.1133 0.9799 0.0409 0.9880 0.1236 0.9778 - 5分/ 2018 - 01 - 26 - t13 z - 24 - 11 - 0.1056 0.9796 0.0613 0.9826 0.1119 0.9777 - 4分/ 2018 - 01 - 26 - t13 z - 24 - 57 - 0.1098 0.9788 0.0868 0.9770 0.1071 0.9771 - 2分/ 2018 - 01 - 26 - t13 - 26 - 28 - z 0.1185 0.9783 0.0688 0.9819 0.1150 0.9783 3分/ 2018 - 01 - 26 - t13 z - 25 - 43 - 0.1238 0.9782 0.0431 0.9883 0.12460.9779 8 runs/2018-01-26T13-21-53Z 0.1064 0.9781 0.0539 0.9843 0.1086 0.9795 7 runs/2018-01-26T13-22-40Z 0.1043 0.9778 0.0796 0.9772 0.1094 0.9777 1 runs/2018-01-26T13-27-14Z 0.1330 0.9769 0.0957 0.9744 0.1304 0.9751 # ... with 21 more columns: # flag_batch_size, flag_dropout1, flag_dropout2, samples, validation_samples, batch_size, # epochs, epochs_completed, metrics, model, loss_function, optimizer, learning_rate, script, # start, end, completed, output, source_code, context, type

    请注意,tuning_run ()函数返回一个数据帧,其中包含所有已执行的训练运行的摘要。

    实验范围内

    默认情况下,所有运行都进入当前工作目录的" runs "子目录。对于各种类型的专门实验,这种方法工作得很好,但在某些情况下,为了进行调优,您可能需要创建单独的目录作用域。

    可以通过指定runs_dir论点:

    tuning_run“mnist_mlp。R”runs_dir “dropout_tuning”旗帜 列表dropout1 c0.20.30.4),dropout2 c0.20.30.4) ))# list在指定的runs_dir中运行
    数据帧:9 x 28 run_dir eval_acc eval_loss metric_loss metric_acc metric_val_loss metric_val_acc 9 dropout_tuning/2018-01-26T13-38-02Z 0.9803 0.0980 0.0324 0.9902 0.1096 0.9789 6 dropout_tuning/2018-01-26T13-40-40Z 0.9795 0.1243 0.0396 0.9885 0.1341 0.9784 2 dropout_tuning/2018-01-26T13-43-55Z 0.9791 0.1138 0.0725 0.9813 0.1205 0.9773 7 dropout_tuning/2018-01-26T13-39-49Z 0.9786 0.1027 0.0796 0.9778 0.1053 0.9761 3 dropout_tuning/2018-01-26T13-39-49Z 0.9784 0.1206 0.0479 0.9871 0.1246 0.9775 4dropout_tuning/2018-01-26T13-42-21Z 0.9784 0.1026 0.0869 0.9766 0.1108 0.9769 5 dropout_tuning/2018-01-26T13-41-31Z 0.9783 0.1086 0.0589 0.9832 0.1216 0.9764 8 dropout_tuning/2018-01-26T13-38-57Z 0.9780 0.1007 0.0511 0.9855 0.1100 0.9771 1 dropout_tuning/2018-01-26T13-44-41Z 0.9770 0.1178 0.1017 0.9734 0.1244 0.9757 #…还有21个列:# flag_batch_size, flag_dropout1, flag_dropout2, samples, validation_samples, batch_size, epoch, # epochs_completed,指标,模型,loss_function,优化器,learning_rate,脚本,开始,结束,# completed,输出,source_code,上下文,类型

    抽样标志组合

    如果标志组合的数目非常大,还可以指定只尝试使用的组合的随机样本样本parmaeter。例如:

    运行 <- tuning_run“mnist_mlp。R”样本 0.3旗帜 列表dropout1 c0.20.30.4),dropout2 c0.20.30.4