1

我正在尝试通过在输出层之前添加更多层来使用可教机器应用程序https://teachablemachine.withgoogle.com/中的谷歌模型。当我重新训练模型时,总是返回这个错误:

ValueError:dense_25 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 5,但接收到形状为 [20, 512] 的输入

这是我的方法:

在此处输入图像描述

重新训练模型时,它返回错误:

在此处输入图像描述

如果我在不添加新层的情况下重新训练模型,它工作正常。任何人都可以建议是什么问题?

4

1 回答 1

0

更新的答案

如果您想在预训练模型的两层之间添加层,它并不像使用 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, 224, 3)
layer :  sequential_11 , expects input  of shape :  (None, 1280)
layer :  dense_12 , expects input  of shape :  (None, 5) <-- **PROBLEM**
layer :  dense_13 , expects input  of shape :  (None, 128)
layer :  dense_14 , expects input  of shape :  (None, 32)

这里的问题是dense_12期望输入形状(None,5),但它应该期望输入形状(None,512),因为我们已经将Dense(512)添加到sequential_11,可能的原因是像上面指定的那样添加层可能不会更新很少有属性,例如sequential_11的输出形状,因此在前向传递期间,sequential_11的输出和dense_12层的输入之间存在不匹配(在您的情况下为dense_25)

可能的解决方法是:

对于您的问题“在sequential_9 和sequential_11 之间添加层”,您可以在sequential_9 和sequential_11 之间添加任意数量的层,但始终确保最后添加层的输出形状应与sequ​​ential_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, 224, 224, 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
于 2020-11-07T19:25:41.257 回答