Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 843
Asef Avatar asked Jul 24 '19 11:07

Asef


People also ask

What does callback do in Keras?

A callback is an object that can perform actions at various stages of training (e.g. at the start or end of an epoch, before or after a single batch, etc). You can use callbacks to: Write TensorBoard logs after every batch of training to monitor your metrics. Periodically save your model to disk.

Which Keras function do you use for training a model?

We call fit() , which will train the model by slicing the data into "batches" of size batch_size , and repeatedly iterating over the entire dataset for a given number of epochs .

What is verbose in Keras?

verbose is the choice that how you want to see the output of your Nural Network while it's training. If you set verbose = 0, It will show nothing.


Video Answer


2 Answers

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.

like image 66
Anubhav Singh Avatar answered Oct 13 '22 01:10

Anubhav Singh


Why a callback ? You could just do:

from time import time

start = time()
model.fit(.....)
print(time()-start)
like image 30
Simon Delecourt Avatar answered Oct 13 '22 02:10

Simon Delecourt