Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

怎样在一张表里比较不同参数组合?

先说出你的答案

定义边界、关键机制、一个反例;P8题再补容量、失败与验证。

查看完整题解

参考回答

直接回答

让每组参数对应一个独立试验目录,在 HParams 表里显示学习率、批大小与结果。先筛到训练和评估条件一致的试验,再按要优化的指标排序。

讲清原理

要判断一组参数稳不稳,可以按配置把不同种子的试验归在一起,在外部统计均值和波动。面板中最好保留单次结果,方便追到反常的某次训练。

HParams 表是怎样拼出来的?
HParams 表是怎样拼出来的?

例子与易错点

下面写两组学习率和示意分数,再读回同一个验证标签,核对表格要用的原始数值。

from pathlib import Path
from tempfile import TemporaryDirectory

directory = TemporaryDirectory()
root = Path(directory.name)
import tensorflow as tf
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

from tensorboard.plugins.hparams import api as hp

rate = hp.HParam("learning_rate", hp.Discrete([0.001, 0.01]))
header = tf.summary.create_file_writer(str(root))
with header.as_default():
    hp.hparams_config([rate], [hp.Metric("val/accuracy")])
header.close()

for label, value, score in [("low", 0.001, 0.75), ("high", 0.01, 0.875)]:
    writer = tf.summary.create_file_writer(str(root / label))
    with writer.as_default():
        hp.hparams({rate: value})
        tf.summary.scalar("val/accuracy", score, step=1)
    writer.close()

rows = {}
for label in ("low", "high"):
    log = EventAccumulator(str(root / label)).Reload()
    item, = log.Tensors("val/accuracy")
    rows[label] = tf.make_ndarray(item.tensor_proto).item()
assert sorted(rows, key=rows.get, reverse=True) == ["high", "low"]
print("high ranks above low: 0.875 vs 0.75")

相关题:第 35 题、第 36 题、第 37 题、第 38 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。