问题描述
我尝试通过在输出层之前添加几层来使用可教机器应用程序https://teachablemachine.withgoogle.com/中的google模型。 重新训练模型时,请始终返回此错误:
ValueError:密集层25的输入0与该层不兼容:预期输入形状的轴-1的值为5,但接收到形状为[20,512]的输入
这是我的方法:
重新训练模型时会返回错误:
如果我在不添加新图层的情况下重新训练模型,则可以正常工作。 有人可以告诉我这是什么问题吗?
解决方法
更新后的答案
如果要为预训练的模型在两层之间添加层,则不如使用add方法添加层那么简单。如果这样做会导致意外的行为
错误分析:
如果您像下面那样编译模型(如您指定的那样):
model.layers[-1].add(Dense(512,activation ="relu"))
model.add(Dense(128,activation="relu"))
model.add(Dense(32))
model.add(Dense(5))
模型摘要的输出:
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
sequential_9 (Sequential) (None,1280) 410208
_________________________________________________________________
sequential_11 (Sequential) (None,512) 131672
_________________________________________________________________
dense_12 (Dense) (None,128) 768
_________________________________________________________________
dense_13 (Dense) (None,32) 4128
_________________________________________________________________
dense_14 (Dense) (None,5) 165
=================================================================
Total params: 546,941
Trainable params: 532,861
Non-trainable params: 14,080
_________________________________________________________________
这里的一切看起来都很不错,但仔细观察:
for l in model.layers:
print("layer : ",l.name,",expects input of shape : ",l.input_shape)
输出:
layer : sequential_9,expects input of shape : (None,224,3)
layer : sequential_11,1280)
layer : dense_12,5) <-- **PROBLEM**
layer : dense_13,128)
layer : dense_14,32)
问题,这是因为density_12期望输入shape(None,5),但它应该期望输入shape(None,512),因为我们在Sequence_11中添加了Dense(512),可能是原因会像上面指定的那样添加图层,可能不会更新一些属性,例如sequential_11的输出形状,因此在前向传递过程中,sequence_11的输出和layer_12的输入(在您的情况下为density_25)之间会出现失配
可能的解决方法是:
对于您的问题“在sequence_9和sequential_11之间添加图层”,您可以在sequence_9和sequence_11之间添加任意数量的图层,但始终确保最后添加的图层的输出形状应与sequence_11期望的输入形状相匹配。在这种情况下是1280。
代码:
sequential_1 = model.layers[0] # re-using pre-trained model
sequential_2 = model.layers[1]
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
inp_sequential_1 = Input(sequential_1.layers[0].input_shape[1:])
out_sequential_1 = sequential_1(inp_sequential_1)
#adding layers in between sequential_9 and sequential_11
out_intermediate = Dense(512,activation="relu")(out_sequential_1)
out_intermediate = Dense(128,activation ="relu")(out_intermediate)
out_intermediate = Dense(32,activation ="relu")(out_intermediate)
# always make sure to include a layer with output shape matching input shape of sequential 11,in this case 1280
out_intermediate = Dense(1280,activation ="relu")(out_intermediate)
output = sequential_2(out_intermediate) # output of intermediate layers are given to sequential_11
final_model = Model(inputs=inp_sequential_1,outputs=output)
模型摘要的输出:
Model: "functional_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None,3)] 0
_________________________________________________________________
sequential_9 (Sequential) (None,1280) 410208
_________________________________________________________________
dense_15 (Dense) (None,512) 655872
_________________________________________________________________
dense_16 (Dense) (None,128) 65664
_________________________________________________________________
dense_17 (Dense) (None,32) 4128
_________________________________________________________________
dense_18 (Dense) (None,1280) 42240
_________________________________________________________________
sequential_11 (Sequential) (None,5) 128600
=================================================================
Total params: 1,306,712
Trainable params: 1,292,632
Non-trainable params: 14,080