10

I am trying to obtain the validation accuracy and the choose to save the model having the best accuracy.But after the 1st epoch,it is showing the checkpoint error.

filepath="tumor_detection-{epoch:02d}-{val_acc:.2f}.hdf5"

# save the model with the best validation (development) accuracy till now
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')

%%time
model.fit(xtrain,ytrain,batch_size=32,epochs=30,validation_data=(xval,yval),callbacks=[checkpoint])```

It is then showing an error as follows:
Epoch 1/30
51/51 [==============================] - ETA: 0s - loss: 0.4651 - accuracy: 0.7725
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _get_file_path(self, epoch, logs)
   1243         # placeholders can cause formatting to fail.
-> 1244         return self.filepath.format(epoch=epoch + 1, **logs)
   1245       except KeyError as e:

KeyError: 'val_acc'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
9 frames
<decorator-gen-60> in time(self, line, cell, local_ns)

<timed eval> in <module>()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _get_file_path(self, epoch, logs)
   1245       except KeyError as e:
   1246         raise KeyError('Failed to format this callback filepath: "{}". '
-> 1247                        'Reason: {}'.format(self.filepath, e))
   1248     else:
   1249       # If this is multi-worker training, and this worker should not

KeyError: 'Failed to format this callback filepath: "tumor_detection-{epoch:02d}-{val_acc:.2f}.hdf5". Reason: \'val_acc\'
Shreyasi Ghosh
  • 101
  • 1
  • 2
  • 4

5 Answers5

16

Replace both

'val_acc'

in file_path and checkpoint with

'val_accuracy'

and it will work.

2

This is possibly a duplicate of this question.

I had the same problem when training with a smaller dataset, where the validation split for the training data was 0.

universvm
  • 353
  • 2
  • 11
1

I added validation_split=0.1 and worked for me:

...
my_callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=2),
    tf.keras.callbacks.ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.h5'),
    tf.keras.callbacks.TensorBoard(log_dir='./logs'),
]

history = model.fit(X_train, y_train, validation_split=0.1, epochs=200, callbacks=my_callbacks)
mm_
  • 1,566
  • 2
  • 18
  • 37
0

It works if you remove validation_steps from mode.fit(). It worked for me particularly, when training with small data sets.

Anil
  • 1
-2

replace this

val_accuracy

with this

val_acc

I Try this and it works

Dilan
  • 2,610
  • 7
  • 23
  • 33
Ska
  • 1
  • 1