C3W2 Assignment Labels Shape

When running the create_model function I have the prediction shapes matching as expected, but the unit test still fails due to a shape mismatch of the output and the target.

Are the labels supposed to be formatted in a way that is not noted in the instructions?

I have not taken that course, but perhaps what this means is that you’ve hard-coded some dimension.

One concrete step you could take would be to open the file unittests.py and see what the meaning of the variable target is in the failing test. That should shed some light on where to look for the error.

You mention “labels” so one other thing to consider is whether this is a conflict between one hot encoding and “categorical” encoding.

I did use one hot encoding in order to get the predictions to match the shape of (32, 5) as the default label encoding results in a shape of (32, 1) which does not match the “expected output”.

Ok, that sounds like the labels are categorical. There are ways to deal with that in TF, right? You can convert back and forth or you can use the appropriate loss functions. I think it’s sparse_categorical_crossentropy that expects the labels in categorical form.

One would hope that the instructions here make it clear which path they expect you to take.

Aha! Thank you.

I was using one hot encoding along with categorical_crossentropy but the tests are expecting sparse_categorical_crossentropy which would require the categorical labels. I believe these would yield the same model results but the tests are expecting a specific answer. Does that sound correct?

As I mentioned, I am not familiar with this exercise, so I’m just discussing how things work in general. You’ll have to translate that into figuring out what is actually expected here. Models produce output in softmax form, so it will be m x C where m is the number of samples and C is the number of classes that your classifier detects. If you have categorical labels, then you have two basic choices:

  1. Convert the categorical labels to one hot form and use the cross entropy loss function that expects that.
  2. Leave the labels as is and use the cross entropy loss function that expects that.

Did you look at the test code to see what it expects?

Hi.
Just make sure that you pass all the other test cases prior to create_model, especially exercise 3: fit_label_encoderwhere you use a StringLookup layer to instantiate and adapt the label encoder, making sure that you set the right value for num_oov_indices.
Once you do that, your model should work correctly as long as the loss is sparse_categorical_crossentropy.

1 Like

Thanks for the responses. The tests expected the labels to be left as is, so I was wrong in converting to one hot form initially.

1 Like