Shapes (None, 1) and (None, 24) are incompatible and IndexError: index 24 is out of bounds for axis 1 with size 24

Hi everyone!
I’ve noticed that there are already numerous topics about this problem here. However, despite trying all the advice given, I’m still stuck.

Initially, I received the error message Shapes (None, 1) and (None, 24) are incompatible when using loss='categorical_crossentropy' and optimizer='rmsprop'.

In my attempt to address this, I implemented the following code snippet:

  num_unique_classes = len(np.unique(training_labels))
  training_labels_encoded = tf.keras.utils.to_categorical(training_labels, num_classes=num_unique_classes)
  validation_labels_encoded = tf.keras.utils.to_categorical(validation_labels, num_classes=num_unique_classes)

But then, I encountered a new error: IndexError: index 24 is out of bounds for axis 1 with size 24 .
I also experimented with using 'sparse_categorical_crossentropy' along with Dense(26, activation='softmax') and Dense(24, activation='softmax'). However, this led to the error message: InvalidArgumentError: Graph execution error.

I would greatly appreciate any insights or help that you can provide. Thank you in advance!

I’m not a mentor for this course, but if I recall correctly, the data set has 24 labels, but these are selected from a range of 26 (because there are some missing codes).

So len(unique(…)) may not be a successful technique.

1 Like

Hello @Elizabeth_Ivanova ,

You arenot using the required optimizer and as @TMosh has said , the snippet is not needed actually.
Alphabets z and j are missing . So there will be 24 labels only.

I am a mentor of this Specilization. Send me your notebook via dm such that I can check where it went wrong. By clicking on my profile picture, you will see an option to message. There you can attach your notebook. Then we can discuss the issues here.

With regards,
Nilosree Sengupta

Thank you for your response!

I appreciate your input. I’ve indeed tried experimenting with various num_classes values, including 24, 23, and 25. However, in each case, I encountered the persistent error: IndexError: index 24/23/25 is out of bounds for axis 1 with size 24/23/25.

Not sure that it will help but I received such mistakes when trying to put something in something which is smaller or doesn’t have enough place for it or different values.
You can read more here python - index 4 is out of bounds for axis 1 with size 4 Code with Double Sum - Stack Overflow
Maybe try to check num_unique_classes = len(np.unique(training_labels)) or experiment with smaller values for layers or check tf.keras.utils.to_categorical  |  TensorFlow v2.14.0.
I don’t understand what task you want to solve but if it c2w4 assignment about MultiTask Classification (# GRADED FUNCTION: train_val_generators) in this task we must only expand the array.
Try to check do you import ''rmsprop" because I sometimes edited tasks for this purpose.
But maybe you want to solve # GRADED FUNCTION: parse_data_from_input of the c2w4 assignment?
We practice using # Convert the label into one-hot arrays ys = tf.keras.utils.to_categorical(labels, num_classes=total_words) in C3W4_Lab and this tree for C2W4 if I’m correct.

Hello @Elizabeth_Ivanova ,

I have seen your notebook.

Part 1 : # GRADED FUNCTION: train_val_generators :

  1. Remove that snippet at first of all. As I and Tom were saying.
  1. So remember you don’t have the training_labels_encoded validation_labels_encoded
    Now you replace wherever you have used the above with just normal labels.

This solves your index error & gives you the desired output.

Part 2 : def create_model():

  1. You have taken input_shape=(150, 150, 1). It should be (28,28,1) right?
  2. As I had said before, your optimizer is not correct. Use adam.

Hope this helps.

With regards,
Nilosree Sengupta

Your advice worked like a charm! Big thanks for your help :slight_smile:

Best regards,

1 Like

Hello @Elizabeth_Ivanova ,

You’re welcome!!
Happy learning!!

With regards,
Nilosree Sengupta

I faced the same issue and I also had expected results for previous functions.

I had to update both training_labels and validation_labels inside train_val_generators() function thanks to_categorical Keras util:

training_labels = tf.keras.utils.to_categorical(training_labels, 24)
validation_labels = tf.keras.utils.to_categorical(validation_labels, 24)

As result shape for labels became Labels of training generator have shape: (27455, 26) instead of expected Labels of training generator have shape: (27455,)

But this trick helped me to pass C2W4 Assignment successfully.

Very late to the party here, but looking at the documentation for tf.keras.utils.to_categorical, these are the inputs expected:

y: Array-like with class values to be converted into a matrix (integers from 0 to num_classes - 1).
num_classes: Total number of classes. If None, this would be inferred as max(y) + 1.
dtype: The data type expected by the input. Default: ‘float32’.

Do you get a full score on this assignment?

I’ve met the same issue. Without tf.keras.utils.to_categorical, I can’t get the model trained. My last layer is like tf.keras.layers.Dense(26, activation = 'softmax') and it complains ValueError: Shapes (None, 1) and (None, 26) are incompatible

After I use tf.keras.utils.to_categorical for the training_labels and validation_labels, I can train the model ok, and pass the assignment grading, but did not get a full score. Received 89/100 for this assignment, and with the following message:

All tests passed for parse_data_from_input!
All tests passed for train_val_generators!
Details of failed tests for create_model

Failed test case: your model could not be used for inference. Details shown in ‘got’ value below:.
no exceptions,
but got:
in user code:

File "/opt/conda/lib/python3.7/site-packages/keras/engine/", line 1366, in test_function  *
    return step_function(self, iterator)
File "/opt/conda/lib/python3.7/site-packages/keras/engine/", line 1356, in step_function  **
    outputs =, args=(data,))
File "/opt/conda/lib/python3.7/site-packages/keras/engine/", line 1349, in run_step  **
    outputs = model.test_step(data)
File "/opt/conda/lib/python3.7/site-packages/keras/engine/", line 1306, in test_step
    y, y_pred, sample_weight, regularization_losses=self.losses)
File "/opt/conda/lib/python3.7/site-packages/keras/engine/", line 201, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/opt/conda/lib/python3.7/site-packages/keras/", line 141, in __call__
    losses = call_fn(y_true, y_pred)
File "/opt/conda/lib/python3.7/site-packages/keras/", line 245, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/opt/conda/lib/python3.7/site-packages/keras/", line 1665, in categorical_crossentropy
    y_true, y_pred, from_logits=from_logits, axis=axis)
File "/opt/conda/lib/python3.7/site-packages/keras/", line 4994, in categorical_crossentropy

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