votes up 2

The model is not configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `model.compile()` method.

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

outputs = self.model.evaluate(x, y, **kwargs)
    if not isinstance(outputs, list):
      outputs = [outputs]
    for name, output in zip(self.model.metrics_names, outputs):
      if name in ['accuracy', 'acc']:
        return output
    raise ValueError('The model is not configured to compute accuracy. '
                     'You should pass `metrics=["accuracy"]` to '
                     'the `model.compile()` method.')


@keras_export('keras.wrappers.scikit_learn.KerasRegressor')
class KerasRegressor(BaseWrapper):
  """Implementation of the scikit-learn regressor API for Keras.
  ""

Ways to fix

votes up 3 votes down

This exception is raised when calling the score method on Keras model that is not compiled with metrics=['accuracy']. This means the Model can not display the accuracy when accuracy is not given as a metrics for the model.

Here is how to reproduce this exception:

  • First install tensorflow and numpy
$ pip install tensorflow numpy

import numpy as np
import keras
from keras.testing_utils import get_test_data
from keras.wrappers import scikit_learn
def build_fn_clf(hidden_dim):
  model = keras.models.Sequential()
  model.add(keras.layers.Dense(INPUT_DIM, input_shape=(INPUT_DIM,)))
  model.add(keras.layers.Activation('relu'))
  model.add(keras.layers.Dense(hidden_dim))
  model.add(keras.layers.Activation('relu'))
  model.add(keras.layers.Dense(NUM_CLASSES))
  model.add(keras.layers.Activation('softmax'))
  model.compile(optimizer='sgd', loss='categorical_crossentropy',)
  return model
INPUT_DIM = 5
HIDDEN_DIM = 5
TRAIN_SAMPLES = 10
TEST_SAMPLES = 5
NUM_CLASSES = 2
BATCH_SIZE = 5
EPOCHS = 1


clf = scikit_learn.KerasClassifier(build_fn=build_fn_clf,
                                   hidden_dim=HIDDEN_DIM,
                                   batch_size=BATCH_SIZE,
                                   epochs=EPOCHS)


np.random.seed(42)
(x_train, y_train), (x_test, _) = get_test_data(train_samples=TRAIN_SAMPLES,
                                                              test_samples=TEST_SAMPLES,
                                                              input_shape=(INPUT_DIM,),
                                                              num_classes=NUM_CLASSES)


clf.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=0)


score = clf.score(x_train, y_train, batch_size=BATCH_SIZE)
print("Score {0:.2f}%".format(score*100))

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:26: DeprecationWarning: KerasClassifier is deprecated, use Sci-Keras (https://github.com/adriangb/scikeras) instead.
2/2 [==============================] - 0s 8ms/step - loss: 1.5903
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-16c35987ea4f> in <module>()  34 clf.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=0)  35  ---> 36 score = clf.score(x_train, y_train, batch_size=BATCH_SIZE)  37 print("Score {0:.2f}%".format(score*100)) 
/usr/local/lib/python3.7/dist-packages/keras/wrappers/scikit_learn.py in score(self, x, y, **kwargs)  317 if name in ['accuracy', 'acc']:  318 return output --> 319 raise ValueError('The model is not configured to compute accuracy. '  320 'You should pass `metrics=["accuracy"]` to '  321 'the `model.compile()` method.') 
ValueError: The model is not configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `model.compile()` method.

Fixed version of the code:

To fix this exception include the argument metrics=['accuracy'] when calling the compile method of the model.

Here is a fixed version of the above code. Notice the compile method in the build_fn_clf function.

import numpy as np
import keras
from keras.testing_utils import get_test_data
from keras.wrappers import scikit_learn
def build_fn_clf(hidden_dim):
  model = keras.models.Sequential()
  model.add(keras.layers.Dense(INPUT_DIM, input_shape=(INPUT_DIM,)))
  model.add(keras.layers.Activation('relu'))
  model.add(keras.layers.Dense(hidden_dim))
  model.add(keras.layers.Activation('relu'))
  model.add(keras.layers.Dense(NUM_CLASSES))
  model.add(keras.layers.Activation('softmax'))
  model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
  return model
INPUT_DIM = 5
HIDDEN_DIM = 5
TRAIN_SAMPLES = 10
TEST_SAMPLES = 5
NUM_CLASSES = 2
BATCH_SIZE = 5
EPOCHS = 1


clf = scikit_learn.KerasClassifier(build_fn=build_fn_clf,
                                   hidden_dim=HIDDEN_DIM,
                                   batch_size=BATCH_SIZE,
                                   epochs=EPOCHS)


np.random.seed(42)
(x_train, y_train), (x_test, _) = get_test_data(train_samples=TRAIN_SAMPLES,
                                                              test_samples=TEST_SAMPLES,
                                                              input_shape=(INPUT_DIM,),
                                                              num_classes=NUM_CLASSES)


clf.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=0)


score = clf.score(x_train, y_train, batch_size=BATCH_SIZE)
print("Score {0:.2f}%".format(score*100))

Output:



/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:26: DeprecationWarning: KerasClassifier is deprecated, use Sci-Keras (https://github.com/adriangb/scikeras) instead.
2/2 [==============================] - 0s 7ms/step - loss: 0.6794 - accuracy: 0.9000 Score 90.00%

Dec 22, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix