我使用model.save()
保存了一个模型。现在我想重新加载该模型并添加一些层以及调整一些超参数,但是它会抛出AttributeError。
我使用load_model()
函数来加载模型。
我猜我不清楚如何向已保存的模型中添加新的层。如果有人能在这方面给我指导,那就太好了。我是深度学习和keras的新手,所以我的请求可能很傻。
片段:
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()模型中添加图层,但是如何向现有的已保存模型添加图层呢?
fc = Dense(256,activation='relu')(prev_model)
应该改成fc = Dense(256,activation='relu')(prev_model.output)
,否则会出现ValueError: Layer conv2d_308 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.engine.training.Model'>. Full input: [<keras.engine.training.Model object at 0x2282c7c50>]. All inputs to the layer should be tensors.
的错误。 - keineahnung2345