【问题标题】:ValueError: Layer model_2 expects 2 inputs, but it received 1 input tensorsValueError: 层 model_2 需要 2 个输入,但它接收到 1 个输入张量
【发布时间】:2020-07-15 07:41:44
【问题描述】:

我有我构建的这个模型,它在 model3.add(graph) 的标题中抛出错误。根据我的阅读和理解,这里的第二个模型model3 期望model3.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()

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2021-12-20
      相关资源
      最近更新 更多