如何在Tensorflow中实现超参数搜索?

时间:2017-11-06 00:02:58

标签: python machine-learning tensorflow bayesian hyperparameters

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

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

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

4 个答案:

答案 0 :(得分:3)

您可以使用DyTB (dynamic training bench):此工具可让您将集中在超参数搜索上,使用张量板来比较经过训练的多样化模型的测量统计数据。

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 --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

答案 1 :(得分:2)

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

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

import ray
from ray import tune

def train_tf_model(config, tune_reporter):  # 1 new arg for reporting results
    # ... train here ....
    # ... train here ....
    # ... train here ....
    pass

tune.register_trainable("train", train_tf_model) 
ray.init()

tune.run_experiments({
    "my_experiment": {
        "run": "train",
        "stop": { "mean_accuracy": 100 },
        "config": {
            "alpha": tune.grid_search([0.2, 0.4, 0.6]),
            "beta": tune.grid_search([1, 2]),
        }}})

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

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:1)

在视频中提供了一些代码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以获取有关如何设置张量板的详细说明。