简体   繁体   中英

Error when using a custom metric function in Keras

I have issues when trying to use a custom function to compile a lstm model in Keras.

I have defined a custom metric function called mean_p_e and I want to use it in my model built in Keras.

My code is:

import keras.backend as K
def mean_p_e(y_true, y_pred):
    return K.mean((y_true - y_pred)**2/y_true)

Then I build my model, stored in a json file:

"model": {
    "loss": "mse",
    "optimizer": "adam",
    "save_dir": "saved_models",
    "metric":"mean_p_e",

and I compile:

model.compile(loss=configs['model']['loss'], optimizer=configs['model']['optimizer'], metrics=['accuracy', configs['model']['metric']])

And I get the Following error:

ValueError: Unknown metric function:mean_p_e

What do I need to change to make it works?

If you define configs in the same script, just replace "metric":"mean_p_e" with "metric":mean_p_e in configs , then everything works.

But it seems that configs is something you read from a json file, in this situation, you could try:

model.compile(loss=configs['model']['loss'], 
              optimizer=configs['model']['optimizer'], 
              metrics=['accuracy', locals()[configs['model']['metric']]])

or:

model.compile(loss=configs['model']['loss'], 
              optimizer=configs['model']['optimizer'], 
              metrics=['accuracy', eval(configs['model']['metric'])])

to call a function by its name.

Ref: Calling a function of a module by using its name (a string) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM