将 TensorBoard 与 Keras Tuner 结合使用 [英] Use TensorBoard with Keras Tuner

查看:62
本文介绍了将 TensorBoard 与 Keras Tuner 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Keras Tuner,用于使用 TF2 构建的模型.后者的典型设置需要在调谐器的 search() 方法中设置 Tensorboard 回调,该方法包装了模型的 fit() 方法.

I ran into an apparent circular dependency trying to use log data for TensorBoard during a hyper-parameter search done with Keras Tuner, for a model built with TF2. The typical setup for the latter needs to set up the Tensorboard callback in the tuner's search() method, which wraps the model's fit() method.

from kerastuner.tuners import RandomSearch
tuner = RandomSearch(build_model, #this method builds the model
             hyperparameters=hp, objective='val_accuracy')
tuner.search(x=train_x, y=train_y,
             validation_data=(val_x, val_y),
             callbacks=[tensorboard_cb]

在实践中,tensorboard_cb 回调方法需要设置记录数据的目录并且这个目录对于每个试验必须是唯一的.一种常用的方法是根据当前时间戳命名目录,代码如下.

In practice, the tensorboard_cb callback method needs to set up the directory where data will be logged and this directory has to be unique to each trial. A common way is to do this by naming the directory based on the current timestamp, with code like below.

log_dir = time.strftime('trial_%Y_%m_%d-%H_%M_%S')
tensorboard_cb = TensorBoard(log_dir)

这在训练具有已知超参数的模型时有效.但是,在进行超参数搜索时,我必须在调用 tuner.search() 之前定义和指定 TensorBoard 回调.这就是问题所在:tuner.search() 将多次调用 build_model() 并且这些试验中的每一个都应该有自己的 TensorBoard 目录.理想情况下,log_dir 的定义将在 build_model() 内完成,但 Keras Tuner 搜索 API 强制在该函数之外定义 TensorBoard.

This works when training a model with known hyper-parameters. However, when doing hyper-parameters search, I have to define and specify the TensorBoard callback before invoking tuner.search(). This is the problem: tuner.search() will invoke build_model() multiple times and each of these trials should have its own TensorBoard directory. Ideally defining log_dir will be done inside build_model() but the Keras Tuner search API forces the TensorBoard to be defined outside of that function.

TL;DR:TensorBoard 通过回调获取数据,并且每次试验需要一个日志目录,但 Keras Tuner 需要为整个搜索定义一次回调,在执行之前,而不是每次试验.在这种情况下,如何定义每次试验的唯一目录?

TL;DR: TensorBoard gets data through a callback and requires one log directory per trial, but Keras Tuner requires defining the callback once for the entire search, before performing it, not per trial. How can unique directories per trial be defined in this case?

推荐答案

keras tuner 为每次运行创建一个子目录(语句可能取决于版本).

The keras tuner creates a subdir for each run (statement is probably version dependent).

我想找到正确的版本组合很重要.

这是它在 jupyterlab 中对我的工作方式.

Here is how it works for me, in jupyterlab.

先决条件:

  1. 点数要求

    keras-tuner==1.0.1
    tensorboard==2.1.1
    tensorflow==2.1.0
    Keras==2.2.4
    jupyterlab==1.1.4

(2.) jupyterlab 安装、构建和运行 [标准编译参数:生产:最小化]

(2.) jupyterlab installed, built and running [standard compile arguments: production:minimize]

这是实际的代码.首先我定义日志文件夹和回调

Here is the actual code. First i define the log folder and the callback

# run parameter
log_dir = "logs/" + datetime.datetime.now().strftime("%m%d-%H%M")

# training meta
stop_callback = EarlyStopping(
    monitor='loss', patience=1, verbose=0, mode='auto')

hist_callback = tf.keras.callbacks.TensorBoard(
    log_dir=log_dir,
    histogram_freq=1,
    embeddings_freq=1,
    write_graph=True,
    update_freq='batch')

print("log_dir", log_dir)

然后我定义我的超模,我不想透露.然后我设置了超参数搜索

Then i define my hypermodel, which i do not want to disclose. Afterwards i set up the hyper parameter search

from kerastuner.tuners import Hyperband

hypermodel = get_my_hpyermodel()

tuner = Hyperband(
    hypermodel
    max_epochs=40,
    objective='loss',
    executions_per_trial=5,
    directory=log_dir,
    project_name='test'
)

然后我执行

tuner.search(
    train_data,
    labels,
    epochs=10,
    validation_data=(val_data, val_labels),
    callbacks=[hist_callback],
    use_multiprocessing=True)

tuner.search_space_summary()

当带有此代码的笔记本搜索足够的超参数时,我控制了另一个笔记本中的损失.由于可以通过魔术函数

While the notebook with this code searches for adequate hyper parameters i control the loss in another notebook. Since tf V2 tensorboard can be called via a magic function

单元格 1

import tensorboard

单元 2

%load_ext tensorboard

单元格 3

%tensorboard --logdir 'logs/'

Sitenote:由于我在 docker 容器中运行 jupyterlab,因此我必须为 tensorboard 指定适当的地址和端口,并在 dockerfile 中转发.

Sitenote: Since i run jupyterlab in a docker container i have to specifiy the appropriate address and port for tensorboard and also forward this in the dockerfile.

结果对我来说并不是真正可以预测的......我还不明白,当我可以期待张量板中的直方图和分布时.有些运行加载时间似乎真的过长...所以要耐心

The result is not really predictable for me... I did not understand yet, when i can expect histograms and distributions in tensorboard. Some runs the loading time seems really excessive... so have patience

在标量下,我找到如下转弯列表

Under scalars i find a list of the turns as follows

"logdir"/"model_has"/execution[iter]/[train/validation]

"logdir"/"model_has"/execution[iter]/[train/validation]

例如0101-1010/bb7981e03d05b05106d8a35923353ec46570e4b6/execution0/train0101-1010/bb7981e03d05b05106d8a35923353ec46570e4b6/execution0/验证

E.g. 0101-1010/bb7981e03d05b05106d8a35923353ec46570e4b6/execution0/train 0101-1010/bb7981e03d05b05106d8a35923353ec46570e4b6/execution0/validation

这篇关于将 TensorBoard 与 Keras Tuner 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆