我可以编写一个 keras 回调来记录并返回总训练时间吗?

Ase*_*sef 5 keras tensorflow

我想记录 keras 模型训练运行的持续时间。我知道我可以把每一步的时间乘以总步数,但我怀疑时间可能会因批次而异。

Anu*_*ngh 7

试试keras.callbacks.Callback()。而且,是的,您是对的,时间会根据批量大小而有所不同。

from timeit import default_timer as timer

class TimingCallback(keras.callbacks.Callback):
    def __init__(self, logs={}):
        self.logs=[]
    def on_epoch_begin(self, epoch, logs={}):
        self.starttime = timer()
    def on_epoch_end(self, epoch, logs={}):
        self.logs.append(timer()-self.starttime)

cb = TimingCallback()

model = Sequential()
# Your code
model.fit(X, y, epochs=epochs, batch_size=batch_size, callbacks=[cb])
print(cb.logs)
print(sum(cb.logs))
Run Code Online (Sandbox Code Playgroud)

这里阅读它。


Sim*_*urt 6

为什么要回调?你可以这样做:

from time import time

start = time()
model.fit(.....)
print(time()-start)
Run Code Online (Sandbox Code Playgroud)