Search code examples
python-3.xtensorflowkerasdeep-learningjupyter-notebook

How to resolve KeyError: 'val_mean_absolute_error' Keras 2.3.1 and TensorFlow 2.0 From Chollet Deep Learning with Python


I am on section 3.7 of Chollet's book Deep Learning with Python. The project is to find the median price of homes in a given Boston suburbs in the 1970's.

https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/3.7-predicting-house-prices.ipynb

At section "Validating our approach using K-fold validation" I try to run this block of code:

num_epochs = 500
all_mae_histories = []
for i in range(k):
    print('processing fold #', i)
    # Prepare the validation data: data from partition # k
    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

    # Prepare the training data: data from all other partitions
    partial_train_data = np.concatenate(
        [train_data[:i * num_val_samples],
         train_data[(i + 1) * num_val_samples:]],
        axis=0)
    partial_train_targets = np.concatenate(
        [train_targets[:i * num_val_samples],
         train_targets[(i + 1) * num_val_samples:]],
        axis=0)

    # Build the Keras model (already compiled)
    model = build_model()
    # Train the model (in silent mode, verbose=0)
    history = model.fit(partial_train_data, partial_train_targets,
                        validation_data=(val_data, val_targets),
                        epochs=num_epochs, batch_size=1, verbose=0)
    mae_history = history.history['val_mean_absolute_error']
    all_mae_histories.append(mae_history)

I get an error KeyError: 'val_mean_absolute_error'

mae_history = history.history['val_mean_absolute_error']

I am guessing the solution is figure out the correct parameter to replace val_mean_absolute_error. I've tried looking into some Keras documentation for what would be the correct key value. Anyone know the correct key value?


Solution

  • The problem in your code is that, when you compile your model, you do not add the specific 'mae' metric.

    If you wanted to add the 'mae' metric in your code, you would need to do like this:

    1. model.compile('sgd', metrics=[tf.keras.metrics.MeanAbsoluteError()])
    2. model.compile('sgd', metrics=['mean_absolute_error'])

    After this step, you can try to see if the correct name is val_mean_absolute_error or val_mae. Most likely, if you compile your model like I demonstrated in option 2, your code will work with "val_mean_absolute_error".

    Also, you should also put the code snippet where you compile your model, it is missing in the question text from above(i.e. the build_model() function)