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.
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.
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 .
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.
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.
Why a callback ? You could just do:
from time import time
start = time()
model.fit(.....)
print(time()-start)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With