Search code examples
kerasconv-neural-networksequentialword-embeddingtf.keras

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


I built this model and it was working fine.

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

I recently reran it and I'm getting an error

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.

I have checked similar post here but none is very similar to mine. I have tried their suggestions like adding the axis to Concatenate() or using concatenate instead but nothing changed.

[embedding_matrix] is a 2d array

Solution

  • Error is thrown because input_layer is Layer and not Tensor. You are passing Embedding "layer" as input to Conv1D, in this case you have not provided any input to embedding layer.

    Change this one:

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

    and add input tensor to this layer:

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


    Also I think you are trying to Concatenate outputs from three separate filters, if that is the case then:

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

    this part would come outside loop.