我如何将hparams与估计量一起使用?

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

要记录hparams without using Keras,我正在按照tf代码here中的建议进行以下操作:

with tf.summary.create_file_writer(model_dir).as_default():
    hp_learning_rate = hp.HParam("learning_rate", hp.RealInterval(0.00001, 0.1))
    hp_distance_margin = hp.HParam("distance_margin", hp.RealInterval(0.1, 1.0))
    hparams_list = [
        hp_learning_rate,
        hp_distance_margin
    ]
    metrics_to_monitor = [
        hp.Metric("metrics_standalone/auc", group="validation"),
        hp.Metric("loss", group="train", display_name="training loss"),
    ]
    hp.hparams_config(hparams=hparams_list, metrics=metrics_to_monitor)
    hparams = {
        hp_learning_rate: params.learning_rate,
        hp_distance_margin: params.distance_margin,
    }
    hp.hparams(hparams)

请注意,params是一个字典对象,在这里我将传递给估计器。

然后我像往常一样训练估算器,

config = tf.estimator.RunConfig(model_dir=params.model_dir)
estimator = tf.estimator.Estimator(model_fn, params=params, config=config)
train_spec = tf.estimator.TrainSpec(...)
eval_spec = tf.estimator.EvalSpec(...)

tf.estimator.train_and_evaluate(estimator, train_spec=train_spec, eval_spec=eval_spec)

训练后,当我启动tensorboard时,我确实记录了hparams,但是没有看到针对它们的任何度量记录

enter image description here

我进一步确认,它们在训练和验证时在scalars页面中显示相同的标签名称,即../eval,但hparams页面看不到那些记录的张量。

我如何将hparams与估计量一起使用?


我正在使用

tensorboard              2.1.0
tensorflow               2.1.0
tensorflow-estimator     2.1.0
tensorflow-metadata      0.15.2

Python 3.7.5


尝试1:

[经过一番谷歌搜索后,我看到了一些较旧的tf代码,它们在其中将hparams传递给Estimator的params参数,因此只是为了确保tf2在给定时是否单独记录这些hparams,我检查了Estimator文档及其说:

params参数包含超参数。它被传递给model_fn,如果model_fn具有名为“ params”的参数,则输入功能以相同的方式。 Estimator仅通过参数一直以来,它不会对其进行检查。因此,params的结构为完全取决于开发人员。

因此将hparams用作params不会有用。


尝试2:

我怀疑由于估计量使用tensorflow.python.summary而不是v2中的默认值tf.summary,因此v1记录的张量可能无法访问,因此,我也尝试使用]]

with tensorflow.python.summary.FileWriter(model_dir).as_default()

但是以RuntimeError: tf.summary.FileWriter is not compatible with eager execution. Use tf.contrib.summary instead失败。

Update

:我在禁用急切执行的情况下运行了它。现在,甚至hparam初始记录都没有发生。 tensorboard中没有hparams选项卡,因为它失败并出现错误
E0129 13:03:07.656290 21584 hparams_plugin.py:104] HParams error: Can't find an HParams-plugin experiment data in the log directory. Note that it takes some time to scan the log directory; if you just started Tensorboard it could be that we haven't finished scanning it yet. Consider trying again in a few seconds.

是否有一种方法可以使张量板读取已记录的度量张量并将它们与hparams链接?

要在不使用Keras的情况下记录hparams,请按照此处tf代码的建议进行以下操作:使用tf.summary.create_file_writer(model_dir).as_default():hp_learning_rate = hp.HParam(“ ...

python tensorflow tensorboard hyperparameters
1个回答
0
投票

罪魁祸首似乎是

# This doesn't seem to compatible with Estimator API
hp.hparams_config(hparams=hparams_list, metrics=metrics_to_monitor)
© www.soinside.com 2019 - 2024. All rights reserved.