我想比较不同模型之间的计算时间。 在拟合期间,每个纪元的计算时间将打印到控制台。
Epoch 5/5
160000/160000 [==============================] - **10s** ......
我正在寻找一种方法来存储这些时间,其方式与每个纪元中保存的模型指标类似,并通过历史对象提供。
答案 0 :(得分:18)
尝试以下回调:
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
应该存储纪元计算时间。
答案 1 :(得分:6)
请参阅MarcinMożejko的答案
import time
class TimeHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.times = []
def on_epoch_begin(self, epoch, logs={}):
self.epoch_time_start = time.time()
def on_epoch_end(self, epoch, logs={}):
self.times.append(time.time() - self.epoch_time_start)
然后
time_callback = TimeHistory()
model.fit(..., callbacks=[..., time_callback],...)
执行日志
Train on 17000 samples, validate on 8000 samples
Epoch 1/3
17000/17000 [==============================] - 5s 266us/step - loss: 36.7562 - mean_absolute_error: 4.5074 - val_loss: 34.2384 - val_mean_absolute_error: 4.3929
Epoch 2/3
17000/17000 [==============================] - 4s 253us/step - loss: 33.5529 - mean_absolute_error: 4.2956 - val_loss: 32.0291 - val_mean_absolute_error: 4.2484
Epoch 3/3
17000/17000 [==============================] - 5s 265us/step - loss: 31.0547 - mean_absolute_error: 4.1340 - val_loss: 30.6292 - val_mean_absolute_error: 4.1480
然后
print(time_callback.times)
输出
[4.531331300735474, 4.308278322219849, 4.505300283432007]
答案 2 :(得分:0)
根据Benjamin Striner,您可以按照以下几行编写一个keras回调
from time import time
class TimingCallback(Callback):
def __init__():
self.logs=[]
def on_epoch_begin(epoch, logs={}):
self.starttime=time()
def on_epoch_end(epoch, logs={}):
self.logs.append(time()-self.starttime)
...
cb = TimingCallback()
model.fit(..., callbacks=[cb])
print(cb.logs)