'Can't instantiate abstract class RootMeanSquaredError

I'm working my way through Chollet's "Deep Learning with Python". I've been doing some implementations and I got stuck on this. I thought I mistyped something, but it doesn't appear so.

Does anyone have a clue on what may be going on?

model = get_mnist_model()
model.compile(optimizer="rmsprop",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy", RootMeanSquaredError()])
model.fit(train_images, train_labels,
    epochs=3,
    validation_data=(val_images, val_labels))

test_metrics = model.evaluate(test_images, test_labels)

TypeError: Can't instantiate abstract class RootMeanSquaredError with abstract methods result



Solution 1:[1]

I just got this error too and realized that it was because I defined the result and reset_state methods in new code blocks in the Jupyter notebook. Once I moved them into the same block as the RootMeanSquaredError class definition, the code you posted worked fine without triggering the TypeError.

This should have been obvious but it wasn't until I saw this example that I realized they belong to the class definition.

Solution 2:[2]

Try again after configuring model compilation inside the model definition and use tf.keras.metrics.RootMeanSquaredError() for evaluation metric.

def get_mnist_model():
  model = tf.keras.models.Sequential([
    keras.layers.Dense(512, activation='relu', input_shape=(784,)),
    ..
    ..
    
    keras.layers.Dense(10)
  ])

  model.compile(optimizer="rmsprop",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy", tf.keras.metrics.RootMeanSquaredError()])

  return model

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 042e
Solution 2 TFer2