ValueError:图层model_2需要2个输入,但收到1个输入张量 [英] ValueError: Layer model_2 expects 2 inputs, but it received 1 input tensors

查看:1224
本文介绍了ValueError:图层model_2需要2个输入,但收到1个输入张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this model I built and it is throwing the error in the title at model3.add(graph). From what I have read and understood, the second model which is model3 here expects two inputs at model3.add(graph) but it is only received one. Why does it need 2 inputs? Am I overlooking something? Please help?

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()


推荐答案

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

You are combining a Functional Model(graph) with Sequential Model(model3). Either convert both the model to be Functional Model(like graph) OR convert both the model to be Sequential Model(like model3).

您可以找到用于转换功能模型的解决方案此处

You can find solution for converting Functional model to Sequential Model and vice versa here.

这篇关于ValueError:图层model_2需要2个输入,但收到1个输入张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆