首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在现有模型中添加更多的层(例如。可教机器应用模型)?

如何在现有模型中添加更多的层(例如。可教机器应用模型)?
EN

Stack Overflow用户
提问于 2020-11-05 08:55:48
回答 1查看 466关注 0票数 0

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

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

我的方法是:

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

如果我重新训练模型,而不添加新的层,它的工作很好。有人能告诉我是怎么回事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-07 19:25:41

更新应答

如果您想要在两个层之间为预先训练的模型添加层,它并不像使用add方法添加层那么简单。如果这样做会导致意想不到的行为。

错误分析:

如果您像下面这样编译模型(就像您指定的那样):

代码语言:javascript
运行
复制
model.layers[-1].add(Dense(512, activation ="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(32))
model.add(Dense(5))

示范摘要的产出:

代码语言:javascript
运行
复制
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
_________________________________________________________________

这里的一切看起来都很好,但仔细看一下:

代码语言:javascript
运行
复制
for l in model.layers:
  print("layer : ", l.name, ", expects input  of shape : ",l.input_shape)

产出:

代码语言:javascript
运行
复制
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)

PROBLEM在这里是dense_12期望输入的形状(无,5),但是它应该期望输入形状为(None,512),因为我们已经向sequential_11添加了密集(512),可能的原因是添加了类似于上面指定的层可能不会更新很少的属性,比如sequential_11的输出形状,所以在向前传递时,sequential_11的输出和层dense_12的输入之间会出现不匹配(在您的例子中是dense_25)。

可能的工作是:

对于“在sequential_9和sequential_11之间添加层”的问题,您可以在sequential_9和sequential_11之间添加任意多个层,但始终确保最后添加的层的输出形状与sequential_11所期望的输入形状相匹配。在这种情况下,它是1280。

代码:

代码语言:javascript
运行
复制
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)

示范摘要的产出:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64693961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档