ValueError:Layer conv1d was called with an input that isn't a symbolic tensor.All inputs to the layer should be tensors

ValueError:Layer conv1d was called with an input that isn't a symbolic tensor.All inputs to the layer should be tensors

我构建了这个模型并且运行良好。

###Building the Model. 
input_layer= Embedding(num_words, 300, input_length=35, weights=[embedding_matrix],trainable=True)
conv_blocks = []
filter_sizes = (2,3,4)
for fx in filter_sizes:
    conv_layer= Conv1D(100, kernel_size=fx, activation='relu', data_format='channels_first')(input_layer)
    maxpool_layer = MaxPooling1D(pool_size=4)(conv_layer)
    flat_layer= Flatten()(maxpool_layer)
    conv_blocks.append(flat_layer)
#conc_layer=concatenate(conv_blocks, axis=1)
conc_layer=Concatenate(axis=-1)([conv_blocks])
graph = Model(inputs=input_layer, outputs=conc_layer)

model = Sequential()
model.add(graph)
model.add(Dropout(0.2))
model.add(Dense(3, activation='sigmoid'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()

我最近重新运行它,但出现错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/am/embassy/vol/x6/jetbrains/apps/PyCharm-P/ch-0/191.6183.50/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/am/embassy/vol/x6/jetbrains/apps/PyCharm-P/ch-0/191.6183.50/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/kosimadukwe/PycharmProjects/untitled/WordEmb.py", line 128, in <module>
conv_layer= Conv1D(100, kernel_size=fx, activation='relu', data_format='channels_first')(input_layer)   #filters=100, kernel_size=3
  File "/home/kosimadukwe/PycharmProjects/untitled/venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 414, in __call__
self.assert_input_compatibility(inputs)
  File "/home/kosimadukwe/PycharmProjects/untitled/venv/lib/python3.7/site-packages/keras/engine/base_layer.py", line 285, in assert_input_compatibility
str(inputs) + '. All inputs to the layer '
ValueError: Layer conv1d_1 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.embeddings.Embedding'>. Full input: [<keras.layers.embeddings.Embedding object at 0x7fae61513c18>]. All inputs to the layer should be tensors.

我在这里检查过类似的 post,但 none 与我的非常相似。我尝试了他们的建议,例如将轴添加到 Concatenate() 或使用 concatenate 代替,但没有任何改变。

[embedding_matrix] is a 2d array

错误被抛出,因为 input_layerLayer 而不是 Tensor。 您正在将 Embedding "layer" 作为输入传递给 Conv1D,在这种情况下,您没有向嵌入层提供任何输入。

改这个:

input_layer= Embedding(num_words, 300, input_length=35, weights=[embedding_matrix],trainable=True)

并将输入张量添加到该层:

input_layer= Embedding(num_words, 300, input_length=35, weights=[embedding_matrix],trainable=True)(input_tensor)


此外,我认为您正在尝试 Concatenate 来自三个独立过滤器的输出,如果是这样的话:

conc_layer=Concatenate(axis=-1)([conv_blocks])
graph = Model(inputs=input_layer, outputs=conc_layer)

这部分会出现在循环之外。