C2T4: Grade explanation

i uploaded my notebook file with the model achieved training accuracy more than 99% and the validation accuracy more than 98%.

During the development, i used the following lines for ensuring the categorical model building:
training_labels = to_categorical(training_labels, num_classes=25)
validation_labels = to_categorical(validation_labels, num_classes=25)
Otherwise, during the building process i received the error: ValueError: Shapes (None, 1) and (None, 25) are incompatible

My notebook grade is 89/100 with the following error:
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:. Expected: no exceptions, but got: in user code: File “/opt/conda/lib/python3.7/site-packages/keras/engine/training.py”, line 1366, in test_function * return step_function(self, iterator) File “/opt/conda/lib/python3.7/site-packages/keras/engine/training.py”, line 1356, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File “/opt/conda/lib/python3.7/site-packages/keras/engine/training.py”, line 1349, in run_step ** outputs = model.test_step(data) File “/opt/conda/lib/python3.7/site-packages/keras/engine/training.py”, line 1306, in test_step y, y_pred, sample_weight, regularization_losses=self.losses) File “/opt/conda/lib/python3.7/site-packages/keras/engine/compile_utils.py”, line 201, in call loss_value = loss_obj(y_t, y_p, sample_weight=sw) File “/opt/conda/lib/python3.7/site-packages/keras/losses.py”, line 141, in call losses = call_fn(y_true, y_pred) File “/opt/conda/lib/python3.7/site-packages/keras/losses.py”, line 245, in call ** return ag_fn(y_true, y_pred, **self._fn_kwargs) File “/opt/conda/lib/python3.7/site-packages/keras/losses.py”, line 1665, in categorical_crossentropy y_true, y_pred, from_logits=from_logits, axis=axis) File “/opt/conda/lib/python3.7/site-packages/keras/backend.py”, line 4994, in categorical_crossentropy target.shape.assert_is_compatible_with(output.shape) ValueError: Shapes (None, 1) and (None, 25) are incompatible .

Could someone explain me what is the error and what is the problem with the notebook?

1 Like

there is an error related your loss code, you have used categorical_crossentropy, it is looking for multi-class classification, so which one would be more ideal???

Check if you have chosen correct activation for your last dense layer.

another thing to notice while getting this error is that if you have used the correct input shape.

the create CNN model clearly mentions to

Note that you should use no more than 2 Conv2D and 2 MaxPooling2D layers to achieve the desired performance.

I have given all the pointers for you to look for correcting your CNN model.


Hi DP,

thanks for your answer. I went through my code and made additional checks based on your suggestions, but still do not have a clue what is wrong. Bottom line - my model performs well, but per understanding there is a test case that failed on my notebook. May i get this test case to learn and try to understand the problem?
I attach my notebook as well.
{moderator edit: code link removed}

1 Like

@danielden, please do not share your code in public. That’s not allowed by the Code of Conduct.

You can send it to a mentor (if invited to do so) by using the “Message” button when you click on their forum ID.


You didn’t reply to my this question have you used categotical_crossentropy which is incorrect. You can send notebook via personal DM. Click on my name and then message

I told you already binary crossentropy or categorical crossentropy both are incorrect choice for this assignment as you are creating model for multi class classification.

Binary crossentropy is used when you have two classes. categorical crossentropy is used when one sample can have multiple classes or labels are soft probabilities.

so both doesn’t seem to fit the category, can you dig little bit more and let me know, what would be better choice if these both cannot be used, and why it should be used?

Errors in your assignment

  1. Parse_data_from input
    you have not recalled your css_reader file in the list form as data but then while recalling for loop row in css_reader, you recalled using data creating error while append for images.

while reshaping images rather than using len try use reshaping using np.shape for image.

  1. Train_val_generators

the instructions clearly mentions you to use np.exapand_dims to label your training and validation set which you used but before that you imported tensorflow and then used to_categorical which was not required. your axis selection is incorrect.

You generator output is not as the expected out for labels of value generator

  1. For create_model
    I would not say your codes are incorrect here but choosing a higher dense layer will always cause your training and validation loss to be higher which you do not want for which you imported tf.keras for the rmsprop which was again not required.

did you try training your model with Adam optimizer??
Your loss choice is still incorrect, although it is a variant of the one your have written, can you tell me the reason and which loss would be ideal for this model??

Do these corrections, let me know if your issue is resolved


Hi DP,
thanks a lot for the answer and explanations. I made the changes based on that and now my grade is 100.
Have a nice day,

1 Like