How to disable printing reports after each epoch in Keras?
Question:
After each epoch I have printout like below:
Train on 102 samples, validate on 26 samples
Epoch 1/1
Epoch 00000: val_acc did not improve
102/102 [==============================] - 3s - loss: 0.4934 - acc: 0.8997 - val_loss: 0.4984 - val_acc: 0.9231
I am not using built-in epochs, so I would like to disable these printouts and print something myself.
How to do that?
I am using tensorflow backend if it matters.
Answers:
Set verbose=0
to the fit method of your model.
First of all, you should set verbose=0
to the fit method for having a silent environment then we need to have a callback for controlling it. I always use the following code for showing every 10 epochs with its loss.
import tensorflow as tf
class Callback(tf.keras.callbacks.Callback):
SHOW_NUMBER = 10
counter = 0
epoch = 0
def on_epoch_begin(self, epoch, logs=None):
self.epoch = epoch
def on_train_batch_end(self, batch, logs=None):
if self.counter == self.SHOW_NUMBER or self.epoch == 1:
print('Epoch: ' + str(self.epoch) + ' loss: ' + str(logs['loss']))
if self.epoch > 1:
self.counter = 0
self.counter += 1
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, use_multiprocessing=True, callbacks=[Callback()], verbose=0)
Notice that if you increase SHOW_NUMBER
, the processing will be finished sooner, so if your epochs are big, you should set it more.
In case anyone doesn’t want to implement the progress bar from scratch:
import tensorflow as tf
class SelectiveProgbarLogger(tf.keras.callbacks.ProgbarLogger):
def __init__(self, verbose, epoch_interval, *args, **kwargs):
super().__init__(*args, **kwargs)
self.default_verbose = verbose
self.epoch_interval = epoch_interval
def on_epoch_begin(self, epoch, *args, **kwargs):
self.verbose = (
0
if epoch % self.epoch_interval != 0
else self.default_verbose
)
super().on_epoch_begin(epoch, *args, **kwargs)
model.fit(
x=..., y=...,
batch_size=..., epochs=...,
# Set the verbosity level here! This is important.
callbacks=[SelectiveProgbarLogger(verbose = 1)],
# This disables the default `ProgbarLogger`
verbose=0
)
After each epoch I have printout like below:
Train on 102 samples, validate on 26 samples
Epoch 1/1
Epoch 00000: val_acc did not improve
102/102 [==============================] - 3s - loss: 0.4934 - acc: 0.8997 - val_loss: 0.4984 - val_acc: 0.9231
I am not using built-in epochs, so I would like to disable these printouts and print something myself.
How to do that?
I am using tensorflow backend if it matters.
Set verbose=0
to the fit method of your model.
First of all, you should set verbose=0
to the fit method for having a silent environment then we need to have a callback for controlling it. I always use the following code for showing every 10 epochs with its loss.
import tensorflow as tf
class Callback(tf.keras.callbacks.Callback):
SHOW_NUMBER = 10
counter = 0
epoch = 0
def on_epoch_begin(self, epoch, logs=None):
self.epoch = epoch
def on_train_batch_end(self, batch, logs=None):
if self.counter == self.SHOW_NUMBER or self.epoch == 1:
print('Epoch: ' + str(self.epoch) + ' loss: ' + str(logs['loss']))
if self.epoch > 1:
self.counter = 0
self.counter += 1
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, use_multiprocessing=True, callbacks=[Callback()], verbose=0)
Notice that if you increase SHOW_NUMBER
, the processing will be finished sooner, so if your epochs are big, you should set it more.
In case anyone doesn’t want to implement the progress bar from scratch:
import tensorflow as tf
class SelectiveProgbarLogger(tf.keras.callbacks.ProgbarLogger):
def __init__(self, verbose, epoch_interval, *args, **kwargs):
super().__init__(*args, **kwargs)
self.default_verbose = verbose
self.epoch_interval = epoch_interval
def on_epoch_begin(self, epoch, *args, **kwargs):
self.verbose = (
0
if epoch % self.epoch_interval != 0
else self.default_verbose
)
super().on_epoch_begin(epoch, *args, **kwargs)
model.fit(
x=..., y=...,
batch_size=..., epochs=...,
# Set the verbosity level here! This is important.
callbacks=[SelectiveProgbarLogger(verbose = 1)],
# This disables the default `ProgbarLogger`
verbose=0
)