Tensorflow 模型的超参数调优
Hyperparameter Tuning of Tensorflow Model
我之前使用过 Scikit-learn 的 GridSearchCV 来优化我的模型的超参数,但只是想知道是否存在类似的工具来优化 Tensorflow 的超参数(例如 时期数、学习率、滑动 window 尺寸等)
如果没有,我如何才能实现有效运行所有不同组合的代码段?
即使似乎没有明确记录(在版本 1.2 中),包 tf.contrib.learn
(included in TensorFlow) defines classifiers that are supposed to be compatible with scikit-learn... However, looking at the source, it seems you need to explicitly set the environment variable TENSORFLOW_SKLEARN
(e.g. to "1"
) to actually get this compatibility. If this works, you can already use GridSearchCV
(see this test case).
也就是说,还有一些选择。我不知道 TensorFlow 有什么特别之处,但 hyperopt, Scikit-Optimize or SMAC3 should all be valid options. MOE and Spearmint 看起来曾经是不错的选择,但现在似乎维护得不太好。
或者,你可以看看像SigOpt(MOE原作者的公司)这样的服务。
编辑
关于运行参数的所有可能组合,核心逻辑,自己实现的话,其实并不复杂。您可以只定义包含每个参数的可能值的列表,然后 运行 通过 itertools.product
的所有组合。类似于:
from itertools import product
param1_values = [...]
param2_values = [...]
param3_values = [...]
for param1, param2, param3 in product(param1_values, param2_values param3_values):
run_experiment(param1, param2, param3)
但是请注意,在许多情况下,网格搜索对于 运行 而言可能非常昂贵,即使只是在参数 space 中进行随机搜索也可能更有效(更多关于 in this publication).
使用 Tensorflow 进行网格搜索的另一个可行(并记录在案)选项是 Ray Tune。它是一个可扩展的超参数调整框架,专门用于深度 learning/reinforcement 学习。
你可以试试a fast tutorial here。
它还在 Python.
的大约 10 行中处理 Tensorboard 日志记录和高效搜索算法(即 HyperOpt
integration and HyperBand)
from ray import tune
def train_tf_model(config):
for i in range(num_epochs):
accuracy = train_one_epoch(model)
tune.report(acc=accuracy)
tune.run(train_tf_model,
config={
"alpha": tune.grid_search([0.2, 0.4, 0.6]),
"beta": tune.grid_search([1, 2]),
})
(免责声明:我积极参与这个项目!)
我之前使用过 Scikit-learn 的 GridSearchCV 来优化我的模型的超参数,但只是想知道是否存在类似的工具来优化 Tensorflow 的超参数(例如 时期数、学习率、滑动 window 尺寸等)
如果没有,我如何才能实现有效运行所有不同组合的代码段?
即使似乎没有明确记录(在版本 1.2 中),包 tf.contrib.learn
(included in TensorFlow) defines classifiers that are supposed to be compatible with scikit-learn... However, looking at the source, it seems you need to explicitly set the environment variable TENSORFLOW_SKLEARN
(e.g. to "1"
) to actually get this compatibility. If this works, you can already use GridSearchCV
(see this test case).
也就是说,还有一些选择。我不知道 TensorFlow 有什么特别之处,但 hyperopt, Scikit-Optimize or SMAC3 should all be valid options. MOE and Spearmint 看起来曾经是不错的选择,但现在似乎维护得不太好。
或者,你可以看看像SigOpt(MOE原作者的公司)这样的服务。
编辑
关于运行参数的所有可能组合,核心逻辑,自己实现的话,其实并不复杂。您可以只定义包含每个参数的可能值的列表,然后 运行 通过 itertools.product
的所有组合。类似于:
from itertools import product
param1_values = [...]
param2_values = [...]
param3_values = [...]
for param1, param2, param3 in product(param1_values, param2_values param3_values):
run_experiment(param1, param2, param3)
但是请注意,在许多情况下,网格搜索对于 运行 而言可能非常昂贵,即使只是在参数 space 中进行随机搜索也可能更有效(更多关于 in this publication).
使用 Tensorflow 进行网格搜索的另一个可行(并记录在案)选项是 Ray Tune。它是一个可扩展的超参数调整框架,专门用于深度 learning/reinforcement 学习。
你可以试试a fast tutorial here。
它还在 Python.
的大约 10 行中处理 Tensorboard 日志记录和高效搜索算法(即HyperOpt
integration and HyperBand)
from ray import tune
def train_tf_model(config):
for i in range(num_epochs):
accuracy = train_one_epoch(model)
tune.report(acc=accuracy)
tune.run(train_tf_model,
config={
"alpha": tune.grid_search([0.2, 0.4, 0.6]),
"beta": tune.grid_search([1, 2]),
})
(免责声明:我积极参与这个项目!)