How to check the weights after every epoc in Keras model

Kiran Baktha picture Kiran Baktha · Feb 4, 2017 · Viewed 9.1k times · Source

I am using the sequential model in Keras. I would like to check the weight of the model after every epoch. Could you please guide me on how to do so.

model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))  
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=5 validation_data=(X_test, y_test))

Thanks in advance.

Answer

Nassim Ben picture Nassim Ben · Feb 6, 2017

What you are looking for is a CallBack function. A callback is a Keras function which is called repetitively during the training at key points. It can be after a batch, an epoch or the whole training. See here for doc and the list of callbacks existing.

What you want is a custom CallBack that can be created with a LambdaCallBack object.

from keras.callbacks import LambdaCallback

model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))  
model.add(Dense(1))
model.add(Activation('sigmoid'))

print_weights = LambdaCallback(on_epoch_end=lambda batch, logs: print(model.layers[0].get_weights()))

model.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy'])
model.fit(X_train, 
          y_train, 
          batch_size=batch_size, 
          nb_epoch=5 validation_data=(X_test, y_test), 
          callbacks = [print_weights])

the code above should print your embedding weights model.layers[0].get_weights() at the end of every epoch. Up to you to print it where you want to make it readable, to dump it into a pickle file,...

Hope this helps