You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Keras函数式API为已训练模型添加层?微调时遇输入不兼容报错

解决Keras动态修改训练后模型时的输入不兼容问题

你碰到的这个ValueError: Input 0 is incompatible with layer dense_1问题,我太熟悉了——核心原因是原模型的输出层(那个Dense(10))在初始化时就绑定了固定的输入形状,它原本是接在Flatten层的输出后面,而你插入新的Dense(32)层之后,新的输入张量形状和它预期的完全不匹配,自然就触发了兼容性错误。

下面给你两种可行的解决思路,都是基于Keras Functional API的特性来处理:

方法1:直接重构模型并迁移权重(最简洁)

这种方法会重新创建输出层,适配新的模型结构,同时把原模型已训练好的权重迁移到新模型的对应层:

# 获取原模型Flatten层的输出节点
flatten_output = model1.layers[-2].output

# 插入你想要的新Dense层
x = Dense(32, activation='relu')(flatten_output)

# 重新创建输出层(关键!不要复用原模型的输出层)
new_outputs = Dense(10, activation='softmax')(x)

# 构建新的完整模型
new_model = Model(inputs=model1.input, outputs=new_outputs)

# 迁移原模型的权重:把原模型中对应层的权重复制到新模型
for layer in new_model.layers[:-2]:  # 前三层(Input、BatchNormalization、Flatten)直接复用权重
    target_layer = model1.layers[new_model.layers.index(layer)]
    layer.set_weights(target_layer.get_weights())

# 别忘了重新编译模型
new_model.compile(optimizer=Adam(lr=1e-5), loss='categorical_crossentropy', metrics=['categorical_accuracy'])

方法2:保留原输出层的权重(如果需要复用已训练的输出层参数)

如果你想保留原输出层已经训练好的权重,可以先提取原权重,再给新创建的输出层赋值:

# 获取原模型的关键节点和权重
flatten_output = model1.layers[-2].output
original_output_weights = model1.layers[-1].get_weights()

# 构建新的模型结构
x = Dense(32, activation='relu')(flatten_output)
# 创建适配新输入形状的输出层
new_output_layer = Dense(10, activation='softmax')
new_outputs = new_output_layer(x)

new_model = Model(inputs=model1.input, outputs=new_outputs)

# 迁移原模型前面层的权重
for layer in new_model.layers[:-2]:
    target_layer = model1.layers[new_model.layers.index(layer)]
    layer.set_weights(target_layer.get_weights())

# 给新输出层设置原输出层的权重(这里因为输出维度都是10,形状匹配,所以可行)
new_output_layer.set_weights(original_output_weights)

# 编译模型
new_model.compile(optimizer=Adam(lr=1e-5), loss='categorical_crossentropy', metrics=['categorical_accuracy'])

额外提示

  • 如果只想微调新插入的层和输出层,不想改动前面已训练好的层,可以在迁移权重后设置这些层不可训练:
    for layer in new_model.layers[:-2]:
        layer.trainable = False
    
  • 每次修改模型结构后必须重新编译,否则训练时会使用Keras默认的优化器和损失函数,和你原来的配置不一致。

内容的提问来源于stack exchange,提问作者vladimir.gorea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:20:33