1

I'm trying to train the model but I am stuck with this error ValueError: Shapes (None, 1) and (None, 24) are incompatible

My code

model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(25, activation=tf.nn.softmax))
model.summary()

Model Summary

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_18 (Conv2D)          (None, 26, 26, 32)        320       
                                                                 
 conv2d_19 (Conv2D)          (None, 24, 24, 64)        18496     
                                                                 
 conv2d_20 (Conv2D)          (None, 22, 22, 128)       73856     
                                                                 
 max_pooling2d_8 (MaxPooling  (None, 11, 11, 128)      0         
 2D)                                                             
                                                                 
 dropout_8 (Dropout)         (None, 11, 11, 128)       0         
                                                                 
 flatten_5 (Flatten)         (None, 15488)             0         
                                                                 
 dense_10 (Dense)            (None, 128)               1982592   
                                                                 
 dropout_9 (Dropout)         (None, 128)               0         
                                                                 
 dense_11 (Dense)            (None, 25)                3225      
                                                                 
=================================================================
Total params: 2,078,489
Trainable params: 2,078,489
Non-trainable params: 0

Complier

model.compile(loss=tf.keras.losses.categorical_crossentropy,
        optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy'])

Fit the model

model.fit(train_img, train_y_values, batch_size=64, epochs=5, 
        verbose=1, validation_data=(test_img, test_y_values))

Error

Epoch 1/5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-1ace0d4f685d> in <module>
      1 model.fit(train_img, train_y_values, batch_size=64, epochs=5, 
----> 2         verbose=1, validation_data=(test_img, test_y_values))

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
        y_true, y_pred, from_logits=from_logits, axis=axis)
    File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

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

train_img shape = (27455, 28, 28)

train_y_value shape = (27455,)

test_img shape = (7172, 28, 28)

test_y_values shape = (7172,)

Somebody help me please

  • 1
    Seems like you're doing classification but your `y` values are not properly prepared. I suspect you have `25` class logits but your `y` are a list of class numbers for each sample. If so you need to 1-hot encode your `y` values so that they are `(N, 25)` as well. – MYousefi Oct 14 '22 at 01:30
  • @MYousefi Thank you for your suggestion. My y values are the number of each pixel which has a total of 784 pixels for 1 picture – Nitipon Khachornphop Oct 15 '22 at 17:55
  • Can you elaborate on what your numbers represent for the pixels and what the final layer output is representing? – MYousefi Oct 17 '22 at 03:27
  • How many categories of images class in your dataset? –  Nov 23 '22 at 11:28

1 Answers1

1

The problem comes from a mismatch of the last layer of your model (None, 25) and the labels (None, 1).

This can be fixed by changing the loss function to "sparse_categorical_crossentropy"

See What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? for more details.

L. Sanna
  • 6,482
  • 7
  • 33
  • 47