Label dimension for Multi-class classification


For multi-class classification problems, we need to create a categorical matrix for the labels right?
Give that we have n categories as outputs, we will have n dimensional output layer for out neural network which requires a n-dimensional categorical label vector for training.

For Assignment in Course 3 W2, we are expected to have a 1 dimensional label vector. How do we train using that?
There was a similar problem with Course2 W4 assignment

Could someone please help me out with it?

Thanks in advance,
Sridhar A

Hello Sridhar,

Can you please screenshot of Course 3 W2 where you need this 1 dimensional vector

also share screenshot of Course 2 W4 where you encountered problem!!!

Also you need to dimensionality of a vector can be addressed in different way based on model architecture, like reshaping the outputs. But I can explain more precisely if I get to know where you are having issue with these assignments.


Should I be passing a 1D vector as the assignment requires us to do?

The model I plan to train on this data has this architecture to handle multi-class classification

Can I know how did you convert labels to sequence?

Also I want to know if your all previous grader cells have passed with the expected output.

This is from a different assignment?

See to_categorical

Read this link to know how label encoding influences the choice of loss function used to train the model.

I used to_categorical which is why I got a 5 dimensional label vector.

But the assignment is expecting me to have a 1D label vector for this multi-class classification problem

1 Like

please remove the codes from the post comment.


Hello Sridhar

Convert labels to sequences

the below statement is in the instructions above your grader cell

  • In the previous function you used the pad_sequences function which returns numpy arrays. Here you will not be using it since you don’t need to pad the labels so you need to make the conversion to numpy arrays yourself.

Convert sequences to a numpy array. Don’t forget to substact 1 from every entry in the array!

For this you do not need to use tf.keras.utils again as you already used Tokenizer()
Read the below instruction

  • Using Keras’ Tokenizer yields values that start at 1 rather than at 0. This will present a problem when training since Keras usually expects the labels to start at 0. To work around this issue you could use an extra neuron in the last layer of your model. However this approach is rather hacky and not very clear. Instead you will substract 1 from every value of the labels that the function returns. Remember that when using numpy arrays you can simply do something like np.array - 1 to accomplish this since numpy allows for vectorized operations.

So the only thing you need to do is convert the label_seq from the above code line with np array again here while subtracting 1

Here we are not converting multi class label but only vectoring the sequence.


Please remove the codes from the post comment.

So we will have to add a new cell to do that conversion?

I did that and it worked but for Course2 W4 assignment we had a similar situation, but in that case we return an imageGenerator from the function. And we cannot do the conversion to the categorical in the new cell for that

Why is this conversion not expected by default in the assignment?

Hello Sridhar,

The reason why it didn’t do in this grader cell is mentioned in the instructions

In the previous function you used the pad_sequences function which returns numpy arrays. Here you will not be using it since you don’t need to pad the labels so you need to make the conversion to numpy arrays yourself.

Previous grader cell used pad_sequence function for numpy array return but for this grader cell we are not using this function.

Hello Sridhar,

There are few issues with your models

  1. The embedding layer needs correction.
    Please follow this link, to make correction
    tf.keras.layers.Embedding  |  TensorFlow v2.13.0

in the link, you will come across input_dim, output_dim and input_length, defining your embedding layer and the values for these are already mentioned at the beginning of the assignment.

  1. The next major issue with your model is your activation choice. This is multi class classification, do you feel you are using the right activation for the last dense layer??
    There is a clear mention about this in the instructions
  • The last layer should be a Dense layer with 5 units (since there are 5 categories) with a softmax activation.
  1. while you are using a dense unit too higher can create problem in your model training as you see epoch is only 30, so you wouldn’t get desired accuracy. which would be preferred first dense layer according to the epoch??

  2. Your loss choice is incorrect. The instructions mentions

  • You should also compile your model using an appropiate loss function and optimizer. Please choose these without needing to edit the following code cell that contains

Categorical_crossentropy produces a one-hot array containing the probable match for each category, where as
sparse_categorical_crossentropy produces a category index of the most likely matching category.

So which one would be ideal.

Do these corrections. Let me know if you clear the test and choose appropriate comment in your post thread to close that thread.

You can ask me if you still have issue.


Thanks for the suggestion. I see the mistake I had with the compile now.
Thanks for the help.

Sridhar A