Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras callbacks keep skip saving checkpoints, claiming val_acc is missing

I'll run some larger models and want to try intermediate results.

Therefore, I try to use checkpoints to save the best model after each epoch.

This is my code:

model = Sequential()
model.add(LSTM(700, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(700, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(700))
model.add(Dropout(0.2))
model.add(Dense(Y_modified.shape[1], activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='val_acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')
model.fit(X_modified, Y_modified, epochs=100, batch_size=50, callbacks=[checkpoint])

But I am still getting the warning after the first epoch:

/usr/local/lib/python3.6/site-packages/keras/callbacks.py:432: RuntimeWarning: Can save best model only with val_acc available, skipping.
  'skipping.' % (self.monitor), RuntimeWarning)

To add metrics=['accuracy'] to the model was in other SO questions (e.g. Unable to save weights while using pre-trained VGG16 model) the solution, but here the error still remains.

like image 618
xentity Avatar asked Oct 12 '18 09:10

xentity


People also ask

How do you save checkpoints in keras?

Callback to save the Keras model or model weights at some frequency. 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.

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?

The ModelCheckpoint callback class allows you to define where to checkpoint the model weights, how to name the file, and under what circumstances to make a checkpoint of the model. The API allows you to specify which metric to monitor, such as loss or accuracy on the training or validation dataset.

What is ModelCheckpoint period?

Setting period=3 will attempt to save a model every 3 batches. If you want it to save at the end of every epoch, set period='epoch' . If save_best_only=True it will check to see if the validation accuracy is higher this time than last time and only save that model.


2 Answers

You are trying to checkpoint the model using the following code

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='val_acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')

ModelCheckpoint will consider the argument monitor to take the decision of saving the model or not. In your code it is val_acc. So it will save the weights if there is a increase in the val_acc.

Now in your fit code,

model.fit(X_modified, Y_modified, epochs=100, batch_size=50, callbacks=[checkpoint])

you haven't provided any validation data. ModelCheckpoint can't save the weights because it don't have the monitor argument to check.

In order to do check pointing based on val_acc you must provide some validation data like this.

model.fit(X_modified, Y_modified, validation_data=(X_valid, y_valid), epochs=100, batch_size=50, callbacks=[checkpoint])

If you don't want to use validation data for whatever be the reason and implement check pointing, you have to change the ModelCheckpoint to work based on acc or loss like this

# Save the checkpoint in the /output folder
filepath = "output/text-gen-best.hdf5"

# Keep only a single checkpoint, the best over test accuracy.
checkpoint = ModelCheckpoint(filepath,
                            monitor='acc',
                            verbose=1,
                            save_best_only=True,
                            mode='max')

Keep in mind that you have to change mode to min if you are going to monitor the loss

like image 81
Sreeram TP Avatar answered Oct 26 '22 23:10

Sreeram TP


I had the same issue, just edit 'val_acc' to 'val_accuracy'

like image 25
mj.beyrami Avatar answered Oct 27 '22 00:10

mj.beyrami