首页 > 解决方案 > 如何在 Keras Lambda 回调中按时期访问步数

问题描述

我想知道如何从 Keras lambda 回调内部访问每个时期的批次数,即传递给函数参数steps_per_epochmodel.fit值。

下面是我的自定义回调:

(我要填写???????batch_per_epoch = ???????

class MyBatchLogger(keras.callbacks.Callback):
    def __init__(self):
        super().__init__()
        self._current_epoch = 0

    def on_epoch_begin(self, epoch, logs=None):
        self._current_epoch = epoch

    def on_epoch_end(self, epoch, logs=None):
        print("Epoch end", logs)

    def on_batch_end(self, batch, logs={}):
        batch_per_epoch = ???????
        acc = logs["acc"].item()
        loss = logs["loss"].item()
        mae = logs["mean_absolute_error"].item()
        ca = logs["categorical_accuracy"].item()

        print(json.dumps({
            "timestamp": datetime.now().isoformat(),
            "epoch": self._current_epoch,
            "batch": batch,
            "batchPerEpoch": batch_per_epoch,
            "accuracy": acc,
            "meanAbsoluteError": mae,
            "categoricalAccuracy": ca,
            "loss": loss
        }))

我将 Keras 2.2.5 与 Tensorflow 1.14.1 一起使用,但如有必要,我可以更新。

标签: python-3.xkeras

解决方案


答案可能来得有点晚,但我已经花了一些时间来挖掘它,所以无论如何这可能会有帮助。

您需要的信息在这里

self.params.get('steps')

推荐阅读