Home > Net >  ValueError: Shapes (None,) and (None, 1) are incompatible
ValueError: Shapes (None,) and (None, 1) are incompatible

Time:03-31

I have the following model :

model = Sequential()
model.add(layers.InputLayer(input_shape=(5,)))
model.add(layers.Dense(20, activation='relu'))
model.add(layers.Dense(30, activation='relu'))
model.add(layers.Dense(1))
model.compile(loss=['mean_absolute_error'], optimizer='adam',
metrics=[metrics.MeanAbsoluteError(), metrics.MeanRelativeError(normalizer=[1])])

My training/testing sets have the following shape :

x_train.shape
(114589, 5)

y_train.shape
(114589, 1)

x_test.shape
(49110, 5)

y_test.shape
(49110, 1)

I'm getting this error

ValueError: Shapes (None,) and (None, 1) are incompatible
       

This error has occurred after I added a new metric to my model tf.keras.metrics.MeanRelativeError. What is causing this please? and how do I fix it?

CodePudding user response:

This seems to be a bug to me, or at least this behavior is not well documented. The problem occurs because y_pred is squeezed internally (for some reason). As a workaround you can use a custom metric with the same logic as used for MeanRelativeError:

import tensorflow as tf

def _mean_relative_error(normalizer):
  def mean_relative_error(y_pred, y_true):
    return tf.math.divide_no_nan(tf.abs(y_true - y_pred), normalizer)
  return mean_relative_error

model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=(5,)))
model.add(tf.keras.layers.Dense(20, activation='relu'))
model.add(tf.keras.layers.Dense(30, activation='relu'))
model.add(tf.keras.layers.Dense(1))
model.compile(loss='mae', optimizer='adam', metrics=[tf.keras.metrics.MeanAbsoluteError(), _mean_relative_error(normalizer=[1])])
model.fit(tf.random.normal((10, 5)), tf.random.normal((10, 1)), epochs=2, batch_size=3)
Epoch 1/2
4/4 [==============================] - 1s 7ms/step - loss: 0.7566 - mean_absolute_error: 0.7209 - mean_relative_error: 0.7209
Epoch 2/2
4/4 [==============================] - 0s 6ms/step - loss: 0.6665 - mean_absolute_error: 0.6625 - mean_relative_error: 0.6625
<keras.callbacks.History at 0x7f04dcfa4390>

You can experiment a bit with the normalizer parameter, since the docs point out that normalizer should have the same shape as y_pred.

  • Related