【问题标题】:How to implement hyperparameter search in Tensorflow?如何在 Tensorflow 中实现超参数搜索?
【发布时间】:2017-11-06 00:02:58
【问题描述】:

我想在 Tensorflow 中实现超参数搜索,就像 this video 中介绍的那样。不幸的是,我找不到任何关于它的教程。

我找到了一些使用它的代码,但我无法正确理解它。实现贝叶斯优化是最好的,但我想先尝试网格或随机搜索。

我之前应该创建不同的图表吗?如何在多个图上进行训练,以及如何比较它们?

【问题讨论】:

标签: python machine-learning tensorflow bayesian hyperparameters


【解决方案1】:

使用 Tensorflow 进行网格搜索的另一个可行选项是 Tune。它是用于超参数调整的可扩展框架/工具,特别适用于深度学习/强化学习。

它还在大约 10 行 Python 代码中处理了 Tensorboard 日志记录和高效搜索算法(即HyperOpt 集成和HyperBand)。

import ray
from ray import tune

def train_tf_model(config):
    model = Model(lr=config["lr"])
    for x, y in dataset:
        accuracy = model.fit(x, y)
        tune.track.log(accuracy=accuracy)

tune.run(
    train_tf_model, 
    config={"lr": tune.grid_search([0.2, 0.4, 0.6])}
)

(免责声明:我为这个项目做出了积极的贡献!)

【讨论】:

    【解决方案2】:

    您可以使用DyTB (dynamic training bench):此工具允许您专注于超参数搜索,使用 tensorboard 比较各种训练模型的测量统计数据。

    DyTB 为您创建一个与当前超参数集关联的唯一名称,并将其用作日志目录。 创建不同的日志目录允许使用 Tensorboard 进行轻松比较。

    例如,您可以使用这一行在 Cifar10 上训练 VGG(VGG 和 Cifar10 都是一些可用的预定义模型和数据集):

    import tensorflow as tf
    from dytb.inputs.predefined import Cifar10
    from dytb.train import train
    from dytb.models.predefined.VGG import VGG
    
    # Instantiate the model
    vgg = VGG()
    
    # Instantiate the CIFAR-10 input source
    cifar10 = Cifar10.Cifar10()
    
    # 1: Train VGG on Cifar10 for 50 epochs
    # Place the train process on GPU:0
    device = '/gpu:0'
    with tf.device(device):
        info = train(
            model=vgg,
            dataset=cifar10,
            hyperparameters={
                "epochs": 50,
                "batch_size": 50,
                "regularizations": {
                    "l2": 1e-5,
                    "augmentation": {
                        "name": "FlipLR",
                        "fn": tf.image.random_flip_left_right,
                        # factor is the estimated amount of augmentation
                        # that "fn" introduces.
                        # In this case, "fn" doubles the training set size
                        # Thus, an epoch is now seen as the original training
                        # training set size * 2
                        "factor": 2,
                    }
                },
                "gd": {
                    "optimizer": tf.train.AdamOptimizer,
                    "args": {
                        "learning_rate": 1e-3,
                        "beta1": 0.9,
                        "beta2": 0.99,
                        "epsilon": 1e-8
                    }
                }
            })
    

    在此模型的训练过程中,您可以使用 tensorboard 监控损失趋势和准确率值。

    使用使用的一些代表性超参数为您创建一个新文件夹:

    tensorboard --logdir "log/VGG/CIFAR-10_Adam_l2=1e-05_fliplr/"
    

    如您所见,模型的文件夹已创建,然后用于训练模型的超参数被添加为子文件夹。

    这意味着如果您更改优化器(从 ADAM 到 MomentumOptimizer)或添加注释,或更改 l2 正则化参数 ecc,DyTB 会在 VGG 文件夹中创建一个子文件夹。

    这允许您将测量的指标与 tensorboard 进行比较,使用模型目录为logdir,以这种方式:

    tensorboard --logdir log/VGG
    

    如需更全面的指南,请查看DyTB README.mdpython-notebook example

    【讨论】:

      【解决方案3】:

      在视频中提供了一些代码here,可让您重新创建他们的结果。

      代码运行模型,绘图由 tensorboard 使用命令tensorboard --logdir <log location> 生成。在这种情况下:tensorboard --logdir /tmp/mnist_tutorial

      视频摘录如下:

      # Try a few learning rates
      for learning_rate in [1E-3, 1E-4, 1E-5]:
      
          for use_two_fc in [True, False]
              for use_two_conv in [True, False]:
      
                  # Construct a hyperparameter string for each one (example: "lr_1E,fc=2,conv=2)
                  hparam_str = make_hparam_string(learning_rate, use_two_fc, use_two_conv)
      
                  writer = tf.summaru/FileWriter("/tmp/mnist_tutorial/" + hparam_str)
      
                  # Actually run with the new settings
                  mnist(learning_rate, use_two_fully_connected_layers, _use_two_conv_layers, writer)
      

      查看github repo 了解更多关于如何设置张量板的详细说明。

      【讨论】:

        【解决方案4】:

        我试图找到通过机器学习选择最佳超参数的指南。

        我选择了一个 TensorFlow 分类问题,并通过全因子超参数网格搜索计算了准确率。然后我尝试拟合逻辑回归和另一个 DNN 分类器来“学习”哪些超参数集对我的问题有好处。

        结果有点混乱......但它可能只适用于您的特定问题。 你可以看看:https://medium.com/@tirthajyoti/when-machine-learning-tries-to-predict-the-performance-of-machine-learning-6cc6a11bb9bf

        【讨论】:

          猜你喜欢
          • 2015-07-06
          • 2020-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-05
          • 2012-02-16
          • 1970-01-01
          相关资源
          最近更新 更多