Tensorflow error when working with metrics module

how do i resolve the tensorflow issue “contrib module” not found when i am trying to use metric functions like mean squared error, mean relative error etc?
for instance if i use the function mean_absolute_error in tf.keras.metrics.mean_absolute_error(y_true,y_pred) or even tf.keras.metrics.MeanAbsoluteError() class as mention in tf 2.12 version i get the “No module named ‘tensorflow.contrib’” error.

if i remove the tf and user keras.metrics.mean_absolute_error(y_true,y_pred) i get "no argument mentioned keep_dims. I use tensorflow 2.6 version and python 3.7.9

please help me. much appreciated

@programmer12,

Welcome to the Tensorflow Forum!

“No module named ‘tensorflow.contrib’” error.

Contrib module moved out of the main TensorFlow library after 2.x.

if i remove the tf and user keras.metrics.mean_absolute_error(y_true,y_pred) i get "no argument mentioned keep_dims.

Could you please provide standalone code to reproduce the issue ?

Thank you!

Hi this is the code to reproduce my error -

from tensorflow import keras
from keras.metrics import mean_absolute_error,mean_absolute_percentage_error,mean_squared_error

def evaluate_metrics(ytrue,ypred):
ytrue = np.float32(ytrue)
ypred = np.float32(ypred)
mae=mean_absolute_error(ytrue,ypred)
mape=mean_absolute_percentage_error(ytrue,ypred)
mse=mean_squared_error(ytrue,ypred)
rmse=tf.sqrt(mse)
mase=mase(ytrue,ypred)
return {
“mae”:mae,
“mape”:mape,
“mse”:mse,
“rmse”:rmse,
“mase”:mase
}

#creating python variables to testing the function

ytrue = np.array([9226.48582088, 8794.35864452, 8798.04205463, 9081.18687849,
8711.53433917, 8760.89271814, 8749.52059102, 8656.97092235,
8500.64355816, 8469.2608989])

ypred = np.array([57107.12067189, 58788.20967893, 58102.19142623, 55715.54665129,
56573.5554719 , 52147.82118698, 49764.1320816 , 50032.69313676,
47885.62525472, 45604.61575361])

#calling the function using the above variables

evaluate_metrics(ytrue,ypred)

please kindly help. I am studying time series topic

@programmer12,

To calculate the mase metric correctly, you should use the formula

mase = mae / tf.reduce_mean(tf.abs(ytrue[1:] - ytrue[:-1]))

Please refer to the working code below

import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.metrics import mean_absolute_error, mean_absolute_percentage_error, mean_squared_error

def evaluate_metrics(ytrue, ypred):
    ytrue = np.float32(ytrue)
    ypred = np.float32(ypred)
    mae = mean_absolute_error(ytrue, ypred)
    mape = mean_absolute_percentage_error(ytrue, ypred)
    mse = mean_squared_error(ytrue, ypred)
    rmse = tf.sqrt(mse)
    mase = mae / tf.reduce_mean(tf.abs(ytrue[1:] - ytrue[:-1]))
    return {
        "mae": mae,
        "mape": mape,
        "mse": mse,
        "rmse": rmse,
        "mase": mase
    }

#creating python variables to testing the function

ytrue = np.array([9226.48582088, 8794.35864452, 8798.04205463, 9081.18687849,
                  8711.53433917, 8760.89271814, 8749.52059102, 8656.97092235,
                  8500.64355816, 8469.2608989])

ypred = np.array([57107.12067189, 58788.20967893, 58102.19142623, 55715.54665129,
                  56573.5554719, 52147.82118698, 49764.1320816, 50032.69313676,
                  47885.62525472, 45604.61575361])

#calling the function using the above variables

evaluate_metrics(ytrue, ypred)

Output:

{'mae': <tf.Tensor: shape=(), dtype=float32, numpy=44397.26>,
 'mape': <tf.Tensor: shape=(), dtype=float32, numpy=505.44952>,
 'mse': <tf.Tensor: shape=(), dtype=float32, numpy=1989516000.0>,
 'rmse': <tf.Tensor: shape=(), dtype=float32, numpy=44603.992>,
 'mase': <tf.Tensor: shape=(), dtype=float32, numpy=279.50198>}

Thank you!

1 Like

Hi i tried this exact code and the error i am getting is -
reduce_mean() got an unexpected keyword argument ‘keep_dims’

also i have further noticed that i am even unable to write any keras import function

for instance the below code used to work before and now its giving me the same error as the above which is “No module named ‘tensorflow.contrib’”
like anything connected to keras whether tf.keras or so forth , i am getting the similar error. Note that before in the same notebook environment the code used to run , maybe some version got changed or something?
from keras import layers

model1=keras.Sequential([

layers.Flatten(input_shape=(28,28)),

layers.Dense(128,activation=‘relu’),

layers.Dense(10,activation=‘softmax’)

])

model1.summary()

Hi i tried this exact code and the error i am getting is -
reduce_mean() got an unexpected keyword argument ‘keep_dims’

also i have further noticed that i am even unable to write any keras import function

for instance the below code used to work before and now its giving me the same error as the above which is “No module named ‘tensorflow.contrib’”
like anything connected to keras whether tf.keras or so forth , i am getting the similar error. Note that before in the same notebook environment the code used to run , maybe some version got changed or something?
from keras import layers

model1=keras.Sequential([

layers.Flatten(input_shape=(28,28)),

layers.Dense(128,activation=‘relu’),

layers.Dense(10,activation=‘softmax’)

])

model1.summary()

@programmer12,

Please try with the latest version of Tensorflow.

To use keras, you will need to have the Tensorflow package installed. Once Tensorflow is installed, just import keras as shown below

from tensorflow import keras

Thank you!