tensorflowforecasting

`tf.keras.metrics.mean_absolute_error(y_true,y_pred)` no longer work with tensorflow v2.16


I have a function that creates a dict for evaluation using metrics like mae, mse,rmse,mape and mase. The error is with "MAE". This was working as of Jan 2024; The error screenshot is attached. Code snippet:

def evaluate_preds(y_true, y_pred):
  # Make sure float32 (for metric calculations)
  y_true = tf.cast(y_true, dtype=tf.float32)
  y_pred = tf.cast(y_pred, dtype=tf.float32)

  # Calculate various metrics
  mae = tf.keras.metrics.mean_absolute_error(y_true, y_pred) #error here
  mse = tf.keras.metrics.mean_squared_error(y_true, y_pred) 
  rmse = tf.sqrt(mse)
  mape = tf.keras.metrics.mean_absolute_percentage_error(y_true, y_pred)
  mase = mean_absolute_scaled_error(y_true, y_pred)
  
  return {"mae": mae.numpy(),
          "mse": mse.numpy(),
          "rmse": rmse.numpy(),
          "mape": mape.numpy(),
          "mase": mase.numpy()}

I troubleshoot using stackoverflow, view documentation but no luck. Steps I followed for setup is:

Error description keras._tf_keras.keras.metrics seem misleading to me.

Any help appreacited!

Thanks,

Jordan Error screenshot


Solution

  • Surprisingly, by importing Keras, it worked. I think in tf 2.x, Keras version also upgraded to 3 from 2

    On Dec 23', importing keras from tensorflow did the trick: from tensorflow import keras. However, it gave the same error when running the notebook on May 03'24. Luckily, import keras worked.