赞
踩
出现该错误是因为要保存的model中包含了自定义的层(Custom Layer),导致加载模型的时候无法解析该Layer
解决该问题的方法是在load_model函数中添加custom_objects参数,该参数接受一个字典,键值为自定义的层:
ner_model = load_model(model_path + 'ner_crf_bilstm_model.h5', custom_objects={"CRF": CRF,"crf_loss": crf_loss, "crf_accuracy": crf_accuracy})
或者你可以使用对象的方法
from keras.utils import CustomObjectScope
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
model = load_model('my_model.h5')
Custom objects handling works the same way for load_model, model_from_json, model_from_yaml:
from keras.models import model_from_json
model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer})
如果有两个以上的自定义网络层的解决方法如下:
custom_ob = {'AttLayer1': custom_layer.Attention,'AttLayer2': custom_layer.Attention}
model = load_model('lstm.h5', custom_objects=custom_ob)
或者
with CustomObjectScope(custom_ob):
model = load_model('lstm.h5')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。