无法将图层添加到已保存的Keras模型. “模型"对象没有属性“添加" [英] Cannot add layers to saved Keras Model. 'Model' object has no attribute 'add'

查看:106
本文介绍了无法将图层添加到已保存的Keras模型. “模型"对象没有属性“添加"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用model.save()保存了模型.我正在尝试重新加载模型并添加几层并调整一些超参数,但是,它会引发AttributeError.

I have a saved a model using model.save(). I'm trying to reload the model and add a few layers and tune some hyper-parameters, however, it throws the AttributeError.

使用load_model()加载模型.

我想我缺少了解如何将图层添加到保存的图层.如果有人可以指导我在这里,那就太好了.我是深度学习和使用keras的新手,所以我的要求可能很愚蠢.

I guess I'm missing understanding how to add layers to saved layers. If someone can guide me here, it will be great. I'm a novice to deep learning and using keras, so probably my request would be silly.

摘要:

prev_model = load_model('final_model.h5') # loading the previously saved model.

prev_model.add(Dense(256,activation='relu'))
prev_model.add(Dropout(0.5))
prev_model.add(Dense(1,activation='sigmoid'))

model = Model(inputs=prev_model.input, outputs=prev_model(prev_model.output))

及其引发的错误:

Traceback (most recent call last):
  File "image_classifier_3.py", line 39, in <module>
    prev_model.add(Dense(256,activation='relu'))
AttributeError: 'Model' object has no attribute 'add'

我知道添加图层适用于新的Sequential()模型,但是如何添加到现有的保存模型中呢?

I know adding layers works on new Sequential() model, but how do we add to existing saved models?

推荐答案

add方法仅存在于顺序模型( Sequential),这是一个功能更强大但更复杂的功能模型(

The add method is present only in sequential models (Sequential class), which is a simpler interface to the more powerful but complicated functional model (Model class). load_model will always return a Model instance, which is the most generic class.

您可以看一下示例,以了解如何组合不同的模型,但是最终的想法是Model的行为与其他任何层都非常相似.因此,您应该可以:

You can look at the example to see how you can compose different models, but the idea is that, in the end, a Model behaves pretty much like any other layer. So you should be able to do:

prev_model = load_model('final_model.h5') # loading the previously saved model.

new_model = Sequential()
new_model.add(prev_model)
new_model.add(Dense(256,activation='relu'))
new_model.add(Dropout(0.5))
new_model.add(Dense(1,activation='sigmoid'))

new_model.compile(...)

这篇关于无法将图层添加到已保存的Keras模型. “模型"对象没有属性“添加"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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