问题描述
我正在尝试构建一个级联或级联(实际上甚至不知道这是否是正确的定义)模型集.为简单起见,我的基本模型如下所示.
I'm trying to build a concatenated or cascaded(actually don't even know if this is the correct definiton) set of models.For the simplicity my base models are looking like below.
----Input----
|
L1-1
|
L1-2
|
Dense
|
Softmax
我对其中的7个模型进行了交叉验证训练,并尝试以级联方式将它们包装起来,例如:
I got 7 of these models trained with cross-validation and trying to wrap up them in a cascade fashion such as:
-----------------------Input---------------------
| | | | | | |
L1-1 L1-2 L1-3 L1-4 L1-5 L1-6 L1-7
| | | | | | |
L2-1 L2-2 L2-3 L2-4 L2-5 L2-6 L2-7
| | | | | | |
|_______|_______|_______|_______|_______|_______|
| Concatenated |
|___________________Dense Layer_________________|
|
SoftMax
每个密集层中的每个神经元都有512
个神经元,因此最终,密集层中的神经元总数为7*512=3584
个.
Each one of Dense Layers got 512
neurons so in the end Concatenated Dense Layer would have a total of7*512=3584
neurons.
我所做的是:
- 训练所有模型并将其保存在名为
models[]
的列表中. - 在所有模型中放置底部的Softmax层.
- Trained all models and saved them in a list named as
models[]
. - Popped the bottom Softmax Layer in all models.
然后我尝试将它们连接起来,但收到错误消息:
Then I try to concatenate them but got the error:
Layer merge was called with an input that isn't a symbolic tensor.
形成级联后,我要做的是冻结除Concatenated Dense Layer
之外的所有中间层,并对其进行一些调整.但我坚持所有细节中所述.
What I'm gonna do after forming the cascade is freezing all the intermediate layers except Concatenated Dense Layer
and tuning it up a little bit. But I'm stuck at as explained in all the details.
推荐答案
您需要为此使用功能性API模型.这种模型适用于张量.
You need to use the functional API model for that. This kind of model works with tensors.
首先,您定义一个公共输入张量:
First you define a common input tensor:
inputTensor = Input(inputShape)
然后,使用此输入调用每个模型以获取输出张量:
Then you call each model with this input to get the output tensors:
outputTensors = [m(inputTensor) for m in models]
然后将这些张量传递到连接层:
Then you pass these tensors to the concatenate layer:
output = Concatenate()(outputTensors)
output = Dense(...)(output)
#you might want to use an Average layer instead of these two....
output = Activation('softmax')(output)
最后,您定义了从开始张量到结束张量的完整模型:
Finally, you define the complete model from start tensors to end tensors:
fullModel = Model(inputTensor,output)
这篇关于串联或级联多个预训练的keras模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!