ValueError: Input 0 is incompatible with layer model: expected shape=(None, 14999, 7), found shape=(None, 7)
Asked Answered
O

1

10

I'm trying to apply Conv1D layers for a classification model which has a numeric dataset. The neural network of my model is as follows:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (14999,7)))
model.add(tf.keras.layers.Conv1D(16,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv1D(32,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.Conv1D(64,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Conv1D(128,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.Conv1D(256,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu'))
model.add(tf.keras.layers.MaxPooling1D(2))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512,activation = 'relu'))
model.add(tf.keras.layers.Dense(128,activation = 'relu'))
model.add(tf.keras.layers.Dense(32,activation = 'relu'))
model.add(tf.keras.layers.Dense(3, activation = 'softmax'))

And the model's input shape is (14999, 7).

model.summary() gives the following output

Model: "sequential_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_24 (Conv1D)           (None, 14997, 8)          176       
_________________________________________________________________
conv1d_25 (Conv1D)           (None, 14995, 16)         400       
_________________________________________________________________
max_pooling1d_10 (MaxPooling (None, 7497, 16)          0         
_________________________________________________________________
dropout_9 (Dropout)          (None, 7497, 16)          0         
_________________________________________________________________
conv1d_26 (Conv1D)           (None, 7495, 32)          1568      
_________________________________________________________________
conv1d_27 (Conv1D)           (None, 7493, 64)          6208      
_________________________________________________________________
max_pooling1d_11 (MaxPooling (None, 3746, 64)          0         
_________________________________________________________________
dropout_10 (Dropout)         (None, 3746, 64)          0         
_________________________________________________________________
conv1d_28 (Conv1D)           (None, 3744, 128)         24704     
_________________________________________________________________
conv1d_29 (Conv1D)           (None, 3742, 256)         98560     
_________________________________________________________________
max_pooling1d_12 (MaxPooling (None, 1871, 256)         0         
_________________________________________________________________
dropout_11 (Dropout)         (None, 1871, 256)         0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 478976)            0         
_________________________________________________________________
dense_14 (Dense)             (None, 512)               245236224 
_________________________________________________________________
dense_15 (Dense)             (None, 128)               65664     
_________________________________________________________________
dense_16 (Dense)             (None, 32)                4128      
_________________________________________________________________
dense_17 (Dense)             (None, 3)                 99        
=================================================================
Total params: 245,437,731
Trainable params: 245,437,731
Non-trainable params: 0

The code for model fitting is:

model.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
history = model.fit(xtrain_scaled, ytrain_scaled, epochs = 30, batch_size = 5, validation_data = (xval_scaled, yval_scaled))

While executing, I'm facing the following error:

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 14999, 7), found shape=(None, 7)

Could anyone help to sort out this issue?

Onomastics answered 11/4, 2021 at 15:49 Comment(0)
U
6

TL;DR:

Change

model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (14999,7)))

to

model.add(tf.keras.layers.Conv1D(8,kernel_size = 3, strides = 1,padding = 'valid', activation = 'relu',input_shape = (7)))

Full answer:

Assumption: I am guessing the reason you chose to write 14999 in the input shape is because that's your batch size / total size of training data

Problem with this:

  • Tensorflow assumes the input shape does not include the batch size
    • By specifying a 2D input_shape you're making Tensorflow expect a 3D input of shape (Batch_size, 14999, 7). But your input is clearly of size (Batch_size, 7)

Solution:

Change the shape from (14999, 7) to just (7)

  • TF will now be expecting the same shape that you are providing

PS: Don't be worried about informing your model of how many training examples you have in the dataset. TF Keras works with the assumption it can be shown unlimited training examples.

Unrestrained answered 11/4, 2021 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.