Antonio López Ruiz
Antonio López Ruiz

Reputation: 1466

Keras incompatible shapes

I have a training and testing set with the following dimensions:

X_train.shape = (1,10767,11) and y_train.shape = (1,10767,3)

I am trying to implement a CNN in order to predict y_train. My model's architecture is as follows:

model = keras.models.Sequential()

model.add(Conv1D(32, kernel_size=5, strides=2, activation='relu', input_shape= (None,11)))
model.add(Conv1D(64, kernel_size=2, strides=1, activation='relu'))       
model.add(Dense(64, activation='relu'))
model.add(Dense(3, activation='sigmoid'))   # Final Layer using Softmax
epochs = 10
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate,momentum=0.9,decay=decay, nesterov=False)
model.compile(loss='mean_squared_error', optimizer='sgd', metrics = ['mse'])

However, when I use fit, after my first epoch prints, I get the following error:

Incompatible shapes: [1,5381,3] vs. [1,10767,3]

I have tried to add a Flatten layer before my last dense layer, however, the problem is that the shape is not fully defined, making me change my input_shape to (10767,11). However, while fitting I still obtain the error:

expected dense_99 to have 2 dimensions, but got array with shape (1, 10767, 3)

which is my last dense layer.

If I try to reduce dimensions in the input shape and in my data, it will say that it expected a dimension of 3 and I gave a dimension of 2.

Upvotes: 0

Views: 1014

Answers (1)

ad2004
ad2004

Reputation: 809

I believe the issue with the the y_train data shape. I modified the shape to be:

y_train=np.ones((1,5381,3))

and did not get errors. Since you are striding by 2 in the first convolution layer, the number of 1D steps is reduced by factor of 2 (minus the kernel size of 5) throughout the network, it appears. I hope this helps.

Upvotes: 1

Related Questions