1

I am trying to train DenseNet121 (among other models) in tensorflow/keras and I need to keep track of accuracy and val_accuracy. However, running this does not log the val_accuracy in the model's history:

clf_model = tf.keras.models.Sequential()
clf_model.add(pre_trained_model)
clf_model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
clf_model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = clf_model.fit(train_processed, epochs=10, validation_data=validation_processed)

output (no val_accuracy, I need val_accuracy):

Epoch 1/10
1192/1192 [==============================] - 75s 45ms/step - loss: 2.3908 - accuracy: 0.4374
Epoch 2/10
 451/1192 [==========>...................] - ETA: 22s - loss: 1.3556 - accuracy: 0.6217

When I tried to pass val_accuracy to the metrics as follows:

clf_model = tf.keras.models.Sequential()
clf_model.add(pre_trained_model)
clf_model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
clf_model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy','val_accuracy'])
history = clf_model.fit(train_processed, epochs=10, validation_data=validation_processed)

I get the following error:

ValueError: Unknown metric function: val_accuracy. Please ensure this object is passed to the `custom_objects` argument. 
See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

Any idea what am I doing wrong?

Update

It turned out the test dataset was empty.

SinkingTitanic
  • 181
  • 1
  • 12
  • val_accuracy is not a metric that you pass to model.compile. – Dr. Snoopy Sep 21 '21 at 21:01
  • 1
    I don't see anything wrong with your code. You can try to print the keys of the history `history.history.keys()` to see what is inside the history. Or is your validation set actually empty? Maybe try setting `validation_split`. – wong.lok.yin Sep 22 '21 at 05:14
  • I'm voting to close this as not reproducible. As per the OP, _It turned out the test dataset was empty._ – Trenton McKinney Oct 10 '21 at 15:32
  • Hi! Did you try again with "clf_model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy',val_accuracy])" (editing under assumption val_accuracy is a function ) . Reference : https://stackoverflow.com/questions/65234398/error-while-using-custom-metrics-with-multi-class-classificaiton-with-keras –  Oct 11 '21 at 20:13

0 Answers0