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.
To save weights every epoch, you can use something known as callbacks in Keras. checkpoint = ModelCheckpoint(.....) , assign the argument 'period' as 1 which assigns the periodicity of epochs. This should do it.
ModelCheckpoint callback is used in conjunction with training using model. fit() to save a model or weights (in a checkpoint file) at some interval, so the model or weights can be loaded later to continue the training from the state saved.
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 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
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