首页 > 解决方案 > 本地超参数调优——Tensorflow Google Cloud ML Engine

问题描述

是否可以使用 ML Engine 调整超参数以在本地训练模型?文档只提到了在云中进行超参数调整的训练(提交作业),并没有提到在本地这样做。

否则,是否有另一种常用的超参数调整,如人口普查估计器教程中那样将命令参数传递给 task.py?

https://github.com/GoogleCloudPlatform/cloudml-samples/tree/master/census

标签: tensorflowgcloudgoogle-cloud-mltensorflow-estimatorhyperparameters

解决方案


正如 Puneith 所说,超参数调优无法在 ML-Engine 中本地运行。

SciKit Optimize 提供了一个易于使用的包装器,适用于包括估计器在内的任何模型。只需将运行 N 个 epoch 的训练的代码放入它自己的函数中,该函数返回评估 1-accuracy、1-auroc 或用于最小化的损失度量。

import numpy as np
from skopt import gp_minimize

def train(hyperparam_config):
    # set from passed in hyperparameters
    learning_rate = hyperparam_config[0]
    num_layers = hyperparam_config[2]
    # run training
    res = estimator.train_and_evaluate()...
    return res['loss']  # return metric to minimize

hyperparam_config = [Real(0.0001, 0.01, name="learning_rate"),
                      Integer(3, 10, name="num_layers")]
res = gp_minimize(train, hyperparam_config)
with open('results.txt', 'w') as wf:
    wf.write(str(res))
print(res)

来源: https ://github.com/scikit-optimize/scikit-optimize/blob/master/examples/hyperparameter-optimization.ipynb


推荐阅读