且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载权重后如何在keras中添加和删除新层?

更新时间:2023-12-02 12:28:22

您可以获取上一个模型的output 并创建一个新模型.下层保持不变.

You can take the output of the last model and create a new model. The lower layers remains the same.

model.summary()
model.layers.pop()
model.layers.pop()
model.summary()

x = MaxPooling2D()(model.layers[-1].output)
o = Activation('sigmoid', name='loss')(x)

model2 = Model(input=in_img, output=[o])
model2.summary()

检查如何使用模型从 keras.applications 迁移学习?

编辑更新:

新的错误是因为您试图在全局 in_img 上创建新模型,而在之前的模型创建中实际上并未使用该模型.您实际上是在定义本地 in_img代码>.所以全局的in_img显然没有连接到符号图中的上层.它与加载重量无关.

The new error is because you are trying to create the new model on global in_img which is actually not used in the previous model creation.. there you are actually defining a local in_img. So the global in_img is obviously not connected to the upper layers in the symbolic graph. And it has nothing to do with loading weights.

为了更好地解决这个问题,你应该使用 model.input 来引用输入.

To better resolve this problem you should instead use model.input to reference to the input.

model3 = Model(input=model2.input, output=[o])