在model.fit()期间记录Keras中每个时期的计算时间 [英] record the computation time for each epoch in Keras during model.fit()

查看:448
本文介绍了在model.fit()期间记录Keras中每个时期的计算时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较不同模型之间的计算时间. 在拟合期间,每个时期的计算时间会打印到控制台上.

I want to compare the computation time between different models. During the fit the computation time per epoch is printed to the console.

Epoch 5/5
160000/160000 [==============================] - **10s** ......

我正在寻找一种方式来存储这些时间,类似于存储在每个时期并且可以通过历史记录对象获取的模型指标.

I'm looking for a way to store these times in a similar way to the model metrics that are saved in each epoch and avaliable through the history object.

推荐答案

尝试以下回调:

class TimeHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.times = []

    def on_epoch_begin(self, batch, logs={}):
        self.epoch_time_start = time.time()

    def on_epoch_end(self, batch, logs={}):
        self.times.append(time.time() - self.epoch_time_start)

然后:

time_callback = TimeHistory()
model.fit(..., callbacks=[..., time_callback],...)
times = time_callback.times

在这种情况下,times应该存储历元计算时间.

In this case times should store the epoch computation times.

这篇关于在model.fit()期间记录Keras中每个时期的计算时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆