Search code examples
pythontensorflowkerasdeep-learningconv1d

Why do I get a Conv2D error trying to run Conv1D layer?


I am trying to write a simple 1 dimensional convolution with a regression (1 dimensional float) output.

model = Sequential()
model.add(Conv1D(filters=1, kernel_size=8, activation='relu'))
model.add(Dense(1, 'softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x = x_train, y = y_train, epochs=3)

This gives me the error:

TypeError: Exception encountered when calling layer "conv1d" (type Conv1D).
Input 'filter' of 'Conv2D' Op has type float32 that does not match type int32 of argument 'input'.
Call arguments received:
• inputs=tf.Tensor(shape=(None, 30931, 4), dtype=int32)

Even if my code is wrong, how is it possible I am getting a Conv2D error without even having a Conv2D layer?

x_train is a numpy array of 3361 training examples, each 1d array of length 30931, with 4 channels of np.int32 data. shape = (3361,30931, 4)

y_train is a numpy array of 3361 np.float64 values I am training my network to recognize.

Should this format of input data work? Or do I need to transform it or use another data type?

Do I need an input_shape parameter in my Conv1D layer? If so, what should it be?

I realize this is oversimplified, and plan a much more complex network to train against many more examples, but just want this running first.


Solution

  • Your x_train data should be of the data type float. Also, you usually flatten your 2D data into 1D or apply some global pooling operation before feeding it into a softmax output layer:

    import tensorflow as tf
    
    x_train = tf.random.normal((10,30931, 4), dtype=tf.float32)
    y_train = tf.random.uniform((10,), maxval=2, dtype=tf.int32)
    y_train = tf.keras.utils.to_categorical(y_train, 2)
    
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv1D(filters=1, kernel_size=8, activation='relu'))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(2, 'softmax'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    model.fit(x = x_train, y = y_train, epochs=3)
    

    Regarding your error message, both the Conv1D and Conv2D layer use the tf.nn.convolution operation internally. Interestingly, the problem is caused by the parameter filter, which has a float data type and cannot handle integer inputs.