Python/Keras - Saving model weights after every N batches
keras
Solution
You can create your own callback (https://keras.io/callbacks/). Something like:
from keras.callbacks import Callback
class WeightsSaver(Callback):
def __init__(self, N):
self.N = N
self.batch = 0
def on_batch_end(self, batch, logs={}):
if self.batch % self.N == 0:
name = 'weights%08d.h5' % self.batch
self.model.save_weights(name)
self.batch += 1
I use `self.batch` instead of the `batch` argument provided because the later restarts at 0 at each epoch.
Then add it to your fit call. For example, to save weights every 5 batches:
model.fit(X_train, Y_train, callbacks=[WeightsSaver(5)])
Problem
I'm new to Python and Keras, and I have successfully built a neural network that saves weight files after every Epoch. However, I want more granularity (I'm visualizing layer weight distributions in time series) and would like to save the weights after every N batches, rather than every epoch. Does anyone have any suggestions?