ValueError:未知层:CapsuleLayer [英] ValueError: Unknown layer: CapsuleLayer

查看:188
本文介绍了ValueError:未知层:CapsuleLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个名为CapsuleLayer的自定义层.实际模型已在单独的类中定义.我已将权重加载到实际模型中,并将模型保存在.h5文件中.但是,当我尝试使用load_model(filepath)加载模型时,出现错误

I have defined a custom layer named CapsuleLayer. The actual model has been defined in a separate class. I have loaded the weights into the actual model and have saved the model in an .h5 file. However when I try to load the model using load_model(filepath) I get the error

ValueError:未知层:CapsuleLayer

ValueError: Unknown layer: CapsuleLayer

在加载保存的模型时如何将自定义图层合并到我的模型中.

How can I incorporate the custom layer into my model while loading the saved model.

推荐答案

C.f. Keras常见问题解答,在已保存的模型中处理自定义图层(或其他自定义对象)" :

C.f. Keras FAQ, "Handling custom layers (or other custom objects) in saved models":

如果要加载的模型包含自定义图层或其他自定义 类或函数,您可以通过以下方式将它们传递给加载机制: custom_objects参数:

If the model you want to load includes custom layers or other custom classes or functions, you can pass them to the loading mechanism via the custom_objects argument:

from keras.models import load_model
# Assuming your model includes instance of an "AttentionLayer" class
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

或者,您可以使用自定义对象范围:

Alternatively, you can use a custom object scope:

from keras.utils import CustomObjectScope

with CustomObjectScope({'AttentionLayer': AttentionLayer}):
    model = load_model('my_model.h5')

自定义对象的处理方式与load_model相同, model_from_json,model_from_yaml:

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})


对于您来说,model = load_model('my_model.h5', custom_objects={'CapsuleLayer': CapsuleLayer})应该可以解决您的问题.


In your case, model = load_model('my_model.h5', custom_objects={'CapsuleLayer': CapsuleLayer}) should solve your problem.

这篇关于ValueError:未知层:CapsuleLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆