abhishah901
abhishah901

Reputation: 568

ValueError: Shapes (None, 1) and (None, 3) are incompatible

I have a 3 dimensional dataset of audio files where X.shape is (329,20,85). I want to have a simpl bare-bones model running, so please don't nitpick and address only the issue at hand. Here is the code:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(32, return_sequences=True, stateful=False, input_shape = (20,85,1)))
model.add(tf.keras.layers.LSTM(20))
model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
model.summary()
print("Train...")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=50, validation_data=(X_test, y_test))

But then I had the error mentioned in the title: ValueError: Shapes (None, 1) and (None, 3) are incompatible

Here is the model.summary()

Model: "sequential_13"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_21 (LSTM)               (None, 20, 32)            15104     
_________________________________________________________________
lstm_22 (LSTM)               (None, 20)                4240      
_________________________________________________________________
dense_8 (Dense)              (None, 3)                 63        
=================================================================
Total params: 19,407
Trainable params: 19,407
Non-trainable params: 0
_________________________________________________________________
Train...

For this, I followed this post and updated Tensorflow to the latest version, but the issue persists. This post is completely unrelated and highly unreliable.This post although a bit relatable is unanswered for a while now.

Update 1.0:

I strongly think the problem has something to do with the final Dense layer where I pass nb_classes as 3, since I am classifying for 3 categories in y.

So I changed the Dense layer's nb_classes to 1, which ran the model and gives me this output, which I am positive is wrong.

Train...
9/9 [==============================] - 2s 177ms/step - loss: 0.0000e+00 - accuracy: 0.1520 - val_loss: 0.0000e+00 - val_accuracy: 0.3418

<tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0>

Update 2.0:

I one hot encoded the ys and resolved the shape issue. But now the above output with <tensorflow.python.keras.callbacks.History at 0x7f50f1dcebe0> persists. Any help with this? Or should I post a new question for this? Thanks for all the help.

How should I proceed, or what should I be changing?

Upvotes: 33

Views: 125388

Answers (6)

prograils
prograils

Reputation: 2376

I faced a similar error while debugging https://github.com/rdmpage/solving-captchas-code-examples/blob/master/train_model.py

ValueError: Shapes (None, 3) and (None, 4) are incompatible

That code uses 32 classes of letters (to classify):

model.add(Dense(32, activation="softmax"))

but I decided to start with just 4 classes - for simplicity (to get the idea how it all works):

model.add(Dense(4, activation="softmax"))

So I tried to train the model with a lowest number of letter pictures possible. I made an empty folder "extracted_letter_images" and then made 4 subfolders in it - with the names "2", "3", "4", "5".

In each subfolder I have put some small amount of corresponding letter images:

"2" - 8 images
"3" - 3 images
"4" - 2 images
"5" - 1 image

Totally 14 images.

Than I suddenly discovered that this function:

(X_train, X_test, Y_train, Y_test) = train_test_split(data, labels, test_size=0.25, random_state=0)

expects some minimal amount of letter images - in my "extracted_letter_images" folder.

I have made such a printing:

print("X_train.shape: ", X_train.shape)
print("Y_train.shape: ", Y_train.shape)
print("X_test.shape: ", X_test.shape)
print("Y_test: ", Y_test.shape)

which showed the following:

X_train.shape:  (10, 20, 20)
Y_train.shape:  (10,)
X_test.shape:  (4, 20, 20)
Y_test:  (4,)

but - after that I deleted one file in the "2" folder. So the overall amount of image files (in the "extracted_letter_images" folder) decreased by 1 and got equal to 13.

Here's the printing:

X_train.shape:  (9, 20, 20)
Y_train.shape:  (9,)
X_test.shape:  (4, 20, 20)
Y_test:  (4,)

and I also got this error:

ValueError: Shapes (None, 3) and (None, 4) are incompatible

What did happen? Why did the error arise?

My guess - this somehow depends on this parameter:

test_size=0.25

in

(X_train, X_test, Y_train, Y_test) = train_test_split(data, labels, test_size=0.25, random_state=0)

Probably (I am not sure, that's just a guess), to avoid such error, you must provide enough letter images (overall in the "extracted_letter_images" folder) so that 'train_test_split' could work properly.

That's 'train_test_split' internal alghorithm which forms 'X_train' and 'X_test' shapes and these shapes correlate with each other through 'test_size' parameter.

So to summarize - you must provide enough training data - to avoid 'Shapes are incompatible' error.

Upvotes: 0

karmveer soni
karmveer soni

Reputation: 1

Issue was with the wrong variables used after One Hot Encoding for Classification problem.

trainY = tf.keras.utils.to_categorical(y_train, num_classes=9)
testY = tf.keras.utils.to_categorical(y_test, num_classes=9)

Modeling was done with y_train and y_test as below:

model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=50,
          batch_size = 128)

Correction did below and it worked as expected:

trainY = tf.keras.utils.to_categorical(y_train, num_classes=9)
testY = tf.keras.utils.to_categorical(y_test, num_classes=9)

Upvotes: 0

Susaj S N
Susaj S N

Reputation: 1077

model.add(tf.keras.layers.Dense(nb_classes, activation='softmax'))

The nb_classes should be same as y_train.shape[1]

Upvotes: 2

user2097824
user2097824

Reputation: 161

Another thing to check, is whether your labels are one-hot-coded, or integers only. See this post: https://www.kaggle.com/general/197993

The error arose because 'categorical_crossentropy' works on one-hot encoded target, while 'sparse_categorical_crossentropy' works on integer target.

Upvotes: 8

Harpreet
Harpreet

Reputation: 175

Check if the last Dense Layer(output) has same number of classes as the number of target classes in the training dataset. I made similar mistake while training the dataset and correcting it helped me.

Upvotes: 14

Zabir Al Nazi Nabil
Zabir Al Nazi Nabil

Reputation: 11198

The first problem is with the LSTM input_shape. input_shape = (20,85,1).

From the doc: https://keras.io/layers/recurrent/

LSTM layer expects 3D tensor with shape (batch_size, timesteps, input_dim).

model.add(tf.keras.layers.Dense(nb_classes, activation='softmax')) - this suggets you're doing a multi-class classification.

So, you need your y_train and y_test have to be one-hot-encoded. That means they must have dimension (number_of_samples, 3), where 3 denotes number of classes.

You need to apply tensorflow.keras.utils.to_categorical to them.

y_train = to_categorical(y_train, 3)
y_test = to_categorical(y_test, 3)

ref: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical

tf.keras.callbacks.History() - this callback is automatically applied to every Keras model. The History object gets returned by the fit method of models.

ref: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History

Upvotes: 44

Related Questions