如何让谷歌云AI平台在训练中检测到`tf.summary.scalar`的调用?

问题描述 投票:1回答:1

(注:我也问过这个问题 此处)

问题

我一直在尝试让谷歌云的AI平台显示在AI平台上训练的Keras模型的准确性。我将超参数调优配置为 hptuning_config.yaml 并且能用。但是我无法让AI平台接收到 tf.summary.scalar 训练期间的调用。

文件

我一直在关注以下文档页面。

1. 超参数调整概述

2. 使用超参数调整

根据 [1]:

AI平台培训如何让你的指标 您可能会注意到,本文档中没有说明如何将您的超参数度量传递给 AI Platform Training 培训服务。这是因为该服务会监控您的训练应用程序生成的 TensorFlow 摘要事件并检索度量。"

而根据 [2]生成这样一个Tensorflow摘要事件的方法之一是通过创建一个回调类,比如这样。

class MyMetricCallback(tf.keras.callbacks.Callback):

    def on_epoch_end(self, epoch, logs=None):
        tf.summary.scalar('metric1', logs['RootMeanSquaredError'], epoch)

我的代码

所以在我的代码中,我加入了。

# hptuning_config.yaml

trainingInput:
  hyperparameters:
    goal: MAXIMIZE
    maxTrials: 4
    maxParallelTrials: 2
    hyperparameterMetricTag: val_accuracy
    params:
    - parameterName: learning_rate
      type: DOUBLE
      minValue: 0.001
      maxValue: 0.01
      scaleType: UNIT_LOG_SCALE
# model.py

class MetricCallback(tf.keras.callbacks.Callback):

    def on_epoch_end(self, epoch, logs):
        tf.summary.scalar('val_accuracy', logs['val_accuracy'], epoch)

我甚至试过

# model.py

class MetricCallback(tf.keras.callbacks.Callback):
    def __init__(self, logdir):
        self.writer = tf.summary.create_file_writer(logdir)

    def on_epoch_end(self, epoch, logs):
        with writer.as_default():
            tf.summary.scalar('val_accuracy', logs['val_accuracy'], epoch)

其中成功地将'val_accuracy'指标保存到了Google存储中(我在TensorBoard上也能看到这一点)。但这并没有被人工智能平台接收到,尽管在 [1].

部分解决方案。

使用... 雲端ML超調 包,我创建了以下类。

# model.py

class MetricCallback(tf.keras.callbacks.Callback):
    def __init__(self):
        self.hpt = hypertune.HyperTune()

    def on_epoch_end(self, epoch, logs):
        self.hpt.report_hyperparameter_tuning_metric(
            hyperparameter_metric_tag='val_accuracy',
            metric_value=logs['val_accuracy'],
            global_step=epoch
        )

可以用了!但我不明白是怎么做到的,因为它似乎只是写到AI平台上的一个文件上 劳动者/tmp/hypertune/*. 在谷歌云的文档中,没有任何解释如何让AI平台接收到这个......

我是不是遗漏了什么,才能让 tf.summary.scalar 事件进行显示?

tensorflow keras google-cloud-platform google-cloud-ml gcp-ai-platform-training
1个回答
-1
投票

我们在TF 2.1中用TF Keras和AI平台进行了测试,成功了。

class CustomCallback(tf.keras.callbacks.TensorBoard):
    """Callback to write out a custom metric used by CAIP for HP Tuning."""

    def on_epoch_end(self, epoch, logs=None):  # pylint: disable=no-self-use
        """Write tf.summary.scalar on epoch end."""
        tf.summary.scalar('epoch_accuracy', logs['accuracy'], epoch)

# Setup TensorBoard callback.
custom_cb = CustomCallback(os.path.join(args.job_dir, 'metric_tb'),
                               histogram_freq=1)

# Train model
keras_model.fit(
        training_dataset,
        steps_per_epoch=int(num_train_examples / args.batch_size),
        epochs=args.num_epochs,
        validation_data=validation_dataset,
        validation_steps=1,
        verbose=1,
        callbacks=[custom_cb])
trainingInput:
  hyperparameters:
    goal: MAXIMIZE
    maxTrials: 4
    maxParallelTrials: 2
    hyperparameterMetricTag: epoch_accuracy
    params:
    - parameterName: batch-size
      type: INTEGER
      minValue: 8
      maxValue: 256
      scaleType: UNIT_LINEAR_SCALE
    - parameterName: learning-rate
      type: DOUBLE
      minValue: 0.01
      maxValue: 0.1
      scaleType: UNIT_LOG_SCALE

似乎和你的代码完全一样 只是我不知道你是如何传递回调的 我记得在不直接指定回调的时候看到过一些问题。

编码 此处

© www.soinside.com 2019 - 2024. All rights reserved.