ValueError:图层model_2需要2个输入,但收到1个输入张量

时间:2020-04-03 07:12:44

标签: python-3.x tensorflow keras conv-neural-network masking

我建立了这个模型,它在model3.add(graph)的标题中引发了错误。根据我的阅读和理解,这里的第二个模型model3model3.add(graph)上有两个输入,但是只收到一个。为什么需要2个输入? 我在俯视什么吗?请帮忙吗?

inputs3 = model.inputs[:2]  # We are getting all layers EXCEPT last 2 layers
layer_output3 = model.get_layer('Encoder-12-FeedForward-Norm')).output  #this is a layer from a pretrained BERT model
removed_layer = RemoveMask()(layer_output3)    #the previous layer contains masks which are not compatible with a CNN layer in Keras
conv_blocks = [] 
filter_sizes = (2,3,4)
for fx in filter_sizes:
    conv_layer = Conv1D(100, kernel_size=fx,
                                    activation= 'softsign'), data_format='channels_first')(removed_layer)  
    maxpool_layer = MaxPooling1D(pool_size=2)(conv_layer)
    flat_layer = Flatten()(maxpool_layer)
    conv_blocks.append(flat_layer)
conc_layer = concatenate(conv_blocks, axis=1)
restored_layer = RestoreMask()([conc_layer, layer_output3])
graph = Model(input=inputs3, outputs=restored_layer)

model3 = Sequential()
model3.add(graph)
model3.add(Dropout(0.1))
model3.add(Dense(3, activation='softmax'))
model3.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model3.summary()

1 个答案:

答案 0 :(得分:0)

您正在将功能模型(图形)与顺序模型(模型3)组合在一起。要么将两个模型都转换为功能模型(如图形),要么将两个模型都转换为顺序模型(如model3)。

您可以找到将功能模型转换为顺序模型,反之亦然here的解决方案。