简体   繁体   中英

Keras concatenate is not defined when loading the model

In one of my lambda layers, I used from keras.layers import concatenate to concatenate two tensors and it worked without any problem during training and I successfully saved the model files.

However, when I'm loading the model, it throws me this error:

NameError: name 'concatenate' is not defined

Does anyone know what might be wrong? I've imported concatenate before I load the model.

The lambda layer looks like this:

def concat_l1_l2(vests):
    l1, l2 = vests
    l1 = K.l2_normalize(l1, axis=-1)
    l2 = K.l2_normalize(l2, axis=-1)
    return concatenate([l1, l2])

Maybe the following will solve your problem. Try to pass your costum function to the load function of keras, ie

load(model_path,{"concat_l1_l2":concat_l1_l2})

I had the same issue when loading a model from a json file, try the following line (it worked for me):

from keras.layers import concatenate
model_from_json(model_file, custom_objects={'concatenate': concatenate})

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