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

Hi all,

Getting this error when I run the cell to save and train the model:

Epoch 1/15

ValueError Traceback (most recent call last)
in ()
5 history = model.fit(train_generator,
6 epochs=15,
----> 7 validation_data=validation_generator)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, “ag_error_metadata”):
→ 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, 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 860, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
    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 141, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1665, 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 4994, in categorical_crossentropy
    target.shape.assert_is_compatible_with(output.shape)

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

Only error I can think of is that there is something not clicking between the parse data from input function and the generators (perhaps the shape of the labels array?). Thoughts?

6 Likes

Please click my name and message your notebook as an attachment.

1 Like

Hi @damf.383
I guess that your issue is caused by the ‘loss’ function you are using.
I think that in this case the most suitable loss function is

sparse_categorical_crossentropy

because the labels are provided as integers.
Which loss function did you use?
Best regards
Fabio

11 Likes

Thank you for your support.

The problem was that I was using 24 categories (I was counting the pictures that showed up in the Kaggle dataset page which shows only 24 possible letters). Then when I used sparse_categorical_crossentropy the training was showing a nan in the loss (possibly because something was being divided by 0? Not sure why the combination of 24 categories and sparse_categorical_crossentropy was not working). I had tried to change things in the parsing function to have it work with 24 categories and that was when I posted this.

Now I changed it to 26 and used sparse_categorical_crossentropy and it works. Thanks!

1 Like

damn! despite i set as last layers into the sequential model:
tf.keras.layers.Dense(512, activation=‘relu’),
tf.keras.layers.Dense(26, activation=‘softmax’) and then i call the model.compile:

model.compile(optimizer = ‘adam’,
loss = ‘sparse_categorical_crossentropy’,
metrics=[‘accuracy’])

when i am going to train the model I get some errors:
history = model.fit(train_generator, epochs=15)…by analyzing the error messages it is not that clear to me what is the problem… :frowning:

1 Like

I just corrected my last message, the error come on this line:
model.fit(train_generator,batch_size=32,epochs=3,validation_data=validation_generator)

I cannot get yet any solution…

1 Like

Please click my name and send your notebook as an attachment.

1 Like

The issue was, the label was used as a string and not as a numeric type.

2 Likes

@SHAHID_KAMAL In your case, input_shape = (150,150,3) is the problem. Please fix it.

2 Likes

I was able to figure out how to resolve this issue. The data has a problem, which causes Tensorflow to think that there are 25 categories, when in fact there are only 24. For some reason, not a single train or validation sample has a ‘9’ as a label. TF seems to be including a space for it anyway, so your final layer must have 25 nodes, not 24. Plus, you will need to use the correct loss function [removed - moderator]

Note to instructor: you may want to edit the assignment notebook to mention this, or just fill in the correct value of 25.

1 Like

Please read this. It says:

Each training and test case represents a label (0-25) as a one-to-one map for each alphabetic letter A-Z (and > no cases for 9=J or 25=Z because of gesture motions).

and

The American Sign Language letter database of hand gestures represent a multi-class problem with 24 classes of > letters (excluding J and Z which require motion).

which seems to indicate that they might be added in the future.

You can customize your network if you want to get rid of the empty labels.

3 Likes

hi

Still feel confused about why the output should be 26 categories rather than 24.
When I use np.unique to check the label array, there are only 24 values :
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 10., 11., 12., 13.,
14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.])
How does the model figure out there are 26 categories ?

1 Like

When softmax activation is used for the output layer, number of classes a model predicts is defined by the number of units in the output layer of the network.
So, if there are 10 units in the output layer, output class labels can be in range [0, 9].

Based on the data you’ve shown, the highest label is 24. So, there should be 25 labels since we have to count 0. Regarding the difference between 25 and 26, please see my response to Plan_9.

2 Likes

I am having the same issue. Why 26 and not 24?

I know the images being trained are pictures of the alphabet in sign language and that there are 26 English alphabets. But the data present is 24 and not 26.

calling np.unique(training_labels) shows that the label starts from 0 and ends in 24 but there is no 9th alphabet.

In the end we have 24 labels. Why 26? Why 26? why is 26 making my code to work using the ‘sparse_categorical_crossentropy’ ?

1 Like

I scored 89%

Got this error:

Details of failed tests for create_model

Failed test case: your model could not be used for inference. Details shown in 'got' value below:.
Expected:
no exceptions,
but got:
 Received a label value of 24 which is outside the valid range of [0, 9).  Label values: 11 2 7 3 7 21 21 6 24 2 20 7 0 21 2 0 4 13 14 1 7 17 15 3 15 3 10 4 0 2 6 14 22 18 11 20 7 22 13 8 20 7 4 19 5 8 3 5 7 8 7 23 21 15 4 8 10 12 18 5 22 1 7 3 10 7 19 0 21 22 4 17 2 21 10 14 15 10 2 14 23 16 1 8 23 19 4 12 15 21 4 16 0 20 5 13 15 17 13 2

Now should the range have been between 0 and 9 ?

2 Likes

Odds are good that you’re mixing language MNIST and digits MNIST. If you’re sure that this grader feedback is for the right assignment, please click my name and message your notebook as an attachment along with screenshot of the grader feedback (expanded).

1 Like

@shyamg
Please fix the following:

  1. input_shape to the model is incorrect. See this: Images of training generator have shape: (27455, 28, 28, 1)
  2. Loss function is incorrect.
1 Like

Thanks for your help Balaji.
It’s working now.

  • Shyam
1 Like

Wow :exploding_head: @fabioantonini Thanks!!!

this solution works perfect to me.

2 Likes

@Jimena_Montaldo
Please fix the input_shape parameter of the model. It’s not input_shape=(150, 150, 1)