Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the weights after every epoc in Keras model

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.

like image 478
Kiran Baktha Avatar asked Feb 04 '17 10:02

Kiran Baktha


People also ask

How do you save weights in Keras after every epoch?

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.

What is ModelCheckpoint in Keras?

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.

Which of the following commands will train the defined model for 25 epochs with batch size as 8?

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 .


1 Answers

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

like image 139
Nassim Ben Avatar answered Oct 21 '22 19:10

Nassim Ben