python-3.x - 如何在 Keras Lambda 回调中按时期访问步数
问题描述
我想知道如何从 Keras lambda 回调内部访问每个时期的批次数,即传递给函数参数steps_per_epoch
的model.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 一起使用,但如有必要,我可以更新。
解决方案
答案可能来得有点晚,但我已经花了一些时间来挖掘它,所以无论如何这可能会有帮助。
您需要的信息在这里
self.params.get('steps')
推荐阅读
- swift - 从字符串获取子字符串直到字符串索引的结尾
- javascript - 如何调用原始数据重塑过程形成,我们可以轻松访问所需的数据?
- javascript - 如何在不影响应用程序性能的情况下搜索包含字符串数组中指定的任何子字符串的元素?
- python - 替换与数据框中特定字符串匹配的值
- asp.net - 尝试写入网络文件夹时出现 ASP.NET 访问被拒绝错误
- file - 为什么 Hashicorp Packer 文件配置器应该使用临时目录?
- node.js - 在 MongoDB (Mongoose) 中导致更新失败的情况
- apache - 在我们访问目录时设置 Apache mod_headers 任何目录?
- computer-science - 有向无环图的最小入度和最大出度之和的值是多少?
- android - Android-检查 API 级别低于 26 的安装包权限