# C3W2 Assignment Labels Shape

**URL:** <https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412>\
**Category:** Natural Language Processing in TensorFlow\
**Tags:** week-module-2\
**Created:** [March 7, 2025, 12:20am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412 "2025-03-07T00:20:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjh226](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@sjh226](https://community.deeplearning.ai/u/sjh226)\
**Post date:** [March 7, 2025, 12:20am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/1 "2025-03-07T00:20:41Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/dlai/original/3X/a/e/ae9d3c4639e2f51671cd80e2be9a0fdd3e02dc0f.png)

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

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 7, 2025, 12:44am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/2 "2025-03-07T00:44:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sjh226](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@sjh226](https://community.deeplearning.ai/u/sjh226)\
**Post date:** [March 7, 2025, 1:07am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/3 "2025-03-07T01:07:38Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 7, 2025, 1:14am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/4 "2025-03-07T01:14:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sjh226](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@sjh226](https://community.deeplearning.ai/u/sjh226)\
**Post date:** [March 7, 2025, 1:17am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/5 "2025-03-07T01:17:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [March 7, 2025, 1:21am UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/6 "2025-03-07T01:21:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lukmanaj](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/lukmanaj/32/161609_2.png) [@lukmanaj](https://community.deeplearning.ai/u/lukmanaj)\
**Post date:** [March 7, 2025, 6:41pm UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/7 "2025-03-07T18:41:12Z")

</div>

Hi.  
Just make sure that you pass all the other test cases prior to `create_model`, especially exercise 3: `fit_label_encoder`where 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`.

---

<div class="post-metadata">

**Author:** ![sjh226](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@sjh226](https://community.deeplearning.ai/u/sjh226)\
**Post date:** [March 10, 2025, 2:43pm UTC](https://community.deeplearning.ai/t/c3w2-assignment-labels-shape/781412/8 "2025-03-10T14:43:32Z")

</div>

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.
