如何使用自定义优化器加载保存了keras的模型 [英] How do I load a keras saved model with custom Optimizer
问题描述
我已经使用自定义优化器编译并训练了keras模型.我保存了模型,但是当我尝试加载模型时,它抛出错误,指出ValueError: Unknown optimizer: MyOptimizer
.我试图将MyOptimizer作为自定义对象传递,例如:models.load_model('myModel.h5', custom_objects={'optimizer':MyOptimizer})
,但仍然会引发错误.如何使用自定义对象将模型加载为keras模型?
I have compiled and trained a keras model with a custom optimizer. I saved the model but when I try to load the model, it throws an error stating ValueError: Unknown optimizer: MyOptimizer
. I tried to pass MyOptimizer as a custom object something like : models.load_model('myModel.h5', custom_objects={'optimizer':MyOptimizer})
and it still throws an error. How do I load the model a keras model with custom Objects?
推荐答案
我遇到了同样的问题:)
I ran into the same problem :)
我通过向模型加载models.load_model('myModel.h5', compile=False)
使它起作用.
I made it work by loading the model with models.load_model('myModel.h5', compile=False)
.
来自keras 源代码:
如果在保存的模型中找到优化器,则该模型已经编译.否则,该模型将无法编译,并且将显示警告.当
compile
设置为False时,将省略编译而不会发出任何警告.
If an optimizer was found as part of the saved model, the model is already compiled. Otherwise, the model is uncompiled and a warning will be displayed. When
compile
is set to False, the compilation is omitted without any warning.
加载未编译的模型后,我可以使用自定义优化器再次对其进行编译.
After the uncompiled model is loaded, I can compile it again with my custom optimizer.
这篇关于如何使用自定义优化器加载保存了keras的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!