Can't load keras model with more than 1 metric


I have issues with tensforflow when I try to load a model which contains more than 1 metric. It gives the following error:

ValueError: Unable to restore custom object of type _tf_keras_metric
currently. Please make sure that the layer implements get_configand
from_config when saving. In addition, please use the
custom_objects arg when calling load_model().

I have tried to search for solutions, but I can only find examples with 1 metric, which also works for me, but I need mulitple metrics. Hope you guys can help!

My code:

      keras.metrics.BinaryAccuracy(threshold = 0.5),
      tfa.metrics.F1Score(num_classes=4, threshold=0.5)      

VOCAB_SIZE = 25000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=VOCAB_SIZE, output_mode='int', pad_to_max_tokens = True)


model = tf.keras.Sequential([encoder,
        tf.keras.layers.Embedding(input_dim=len(encoder.get_vocabulary())+1, output_dim=64, mask_zero=True),
        tf.keras.layers.Dense(32, activation='sigmoid', activity_regularizer=tf.keras.regularizers.L2(0.005)),
        tf.keras.layers.Dense(4, activation='sigmoid')      

              optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0005),
              metrics= METRICS),
history =, label_train,
                    validation_split = 0.2,
                    batch_size = 32,
                    verbose = 1,
                    shuffle = True)

# --- Save trained model --- #'CNN_model_fit_1.2', save_format = 'tf')

# --- Load model --- #
from keras.models import load_model
def BinaryAccuracy(label_test, test_predictions):
    return 1
def HammingLoss(label_test, test_predictions):
    return 1
def F1Score(label_test, test_predictions):
    return 1

model_new = load_model("CNN_model_fit_1.2", custom_objects={'binary_accuracy':BinaryAccuracy,'hamming_loss':HammingLoss,'f1_score':F1Score})

pred = model_new.predict(X_test)


I cannot test your code because you have not provided shape and size of X_train but here is an idea:

Tensorflow addons is a separate library from Tensorflow , hence its metrics can be thought of as custom objects.

#Add tf.metrics before the name of the metrics
model_new = load_model("CNN_model_fit_1.2", custom_objects={'hamming_loss': tfa.metrics.HammingLoss,'f1_score': tfa.metrics.F1Score})

Answered By – akshit.C

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published