load_model with a metric

199 Views Asked by At

I save a model with a metric I defined as it is done here . Were they do the following:

def get_lr_metric(optimizer):
    def lr(y_true, y_pred):
        return optimizer.lr
    return lr

optimizer = keras.optimizers.Adam()
lr_metric = get_lr_metric(optimizer)

model.compile(
    optimizer=optimizer,
    metrics=['accuracy', lr_metric],
    loss='mean_absolute_error', 
    )

It works great. However, when I try to load this module:

keras.models.load_model(model_path, custom_objects = {'get_lr_metric': get_lr_metric})

I get: 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().

Trying the solution here:

def get_lr_metric(y_true, y_pred):
    return 1

keras.models.load_model(model_path, custom_objects = {'get_lr_metric': get_lr_metric})

shows the same error message.

I use tensorflow 2.3.0 (keras 2.4.0) with Python 3.8 on Windows 10.

How should I load the model?

0

There are 0 best solutions below