Asef
Asef

Reputation: 65

Can I write a keras callback that records and returns the total training time?

I want to record the duration of the training run for a keras model. I understand that I could just take the time per step and multiply it with the total number of steps but I suspect that time could vary based on the batch.

Upvotes: 6

Views: 8445

Answers (2)

Anubhav Singh
Anubhav Singh

Reputation: 8699

Try keras.callbacks.Callback(). And, yeah you are right that time will vary based on the batch size.

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))

Read about it from here.

Upvotes: 12

Simon Delecourt
Simon Delecourt

Reputation: 1599

Why a callback ? You could just do:

from time import time

start = time()
model.fit(.....)
print(time()-start)

Upvotes: 10

Related Questions