# Wk 3, Prog. exercise 6: do I have to reshape the "logits" and "labels"?

**URL:** <https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [October 9, 2022, 1:02pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687 "2022-10-09T13:02:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Ascan](https://avatars.discourse-cdn.com/v4/letter/r/bc8723/32.png) [@Robert\_Ascan](https://dlai.staged-by-discourse.com/u/Robert_Ascan)\
**Post date:** [October 9, 2022, 1:02pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/1 "2022-10-09T13:02:02Z")

</div>

In the exercise it is mentioned that _" It’s important to note that the “`y_pred`” and “`y_true`” inputs of tf.keras.losses.categorical\_crossentropy are expected to be of shape ( **number of examples, num\_classes** ). "_

Do I understand it correctly that I have to reshape the logits (being the “y\_pred”) – output of forward propagation (output of the last LINEAR unit), of shape (6, num\_examples), as well as the “labels”, into matrices of shape (num\_examples, 6) inside the tf.keras.losses.categorical\_crossentropy()?

If I pass the labels (y\_true) and logits (y\_pred) as they are I get the following error (see the attached screenshot). Note that I did tf.reduce\_sum the cost function.

 ![Screenshot 2022-10-09 at 15.51.54](https://global.discourse-cdn.com/dlaistaging/original/3X/a/a/aab3d199e840806e28f18e9ac2e9c8bdcad7df56.jpeg)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [October 9, 2022, 3:06pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/2 "2022-10-09T15:06:00Z")

</div>

Yes, if you study how they defined the forward propagation logic, you can see that the input data is arranged the way that Prof Ng has used up to this point: the first dimension is “features” and the second dimension is “samples”. That means by the output layer, we get “classes” as the first dimension and “samples” as the second. So you need to fix that, but using “reshape” is not the way to do that. You should use “transpose”. Those are two different things. You can end up with the correct shape using “reshape”, but the contents are not correct. The definition of transpose is to flip the matrix about the major axis, which is what you need in this case.

---

<div class="post-metadata">

**Author:** ![Robert\_Ascan](https://avatars.discourse-cdn.com/v4/letter/r/bc8723/32.png) [@Robert\_Ascan](https://dlai.staged-by-discourse.com/u/Robert_Ascan)\
**Post date:** [October 9, 2022, 3:22pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/3 "2022-10-09T15:22:17Z")

</div>

Thanks, @paulinpaloalto! I am still getting a wrong answer (see the screenshot below). I did tf.math.reduce\_sum( … ). I left the default axis=None. I can’t figure out where the bug is!?

 ![Screenshot 2022-10-09 at 18.13.09](https://global.discourse-cdn.com/dlaistaging/original/3X/f/b/fbc491e6b85587af78c13de03c4db7a2e489a267.jpeg)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [October 9, 2022, 3:33pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/4 "2022-10-09T15:33:11Z")

</div>

Did you do the transpose on both labels and logits? Are you sure you did it using `tf.transpose` instead of `tf.reshape`? Did you include `from_logits = True` to take account of the fact that the output of the final layer is linear activation?

---

<div class="post-metadata">

**Author:** ![Robert\_Ascan](https://avatars.discourse-cdn.com/v4/letter/r/bc8723/32.png) [@Robert\_Ascan](https://dlai.staged-by-discourse.com/u/Robert_Ascan)\
**Post date:** [October 9, 2022, 3:50pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/5 "2022-10-09T15:50:58Z")

</div>

Yes, I did tf.linalg.matrix\_transpose() on both. I missed the “from\_logits = True”. It worked now. Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![Arul\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arul\_Raj](https://dlai.staged-by-discourse.com/u/Arul_Raj)\
**Post date:** [October 11, 2022, 5:32am UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/6 "2022-10-11T05:32:15Z")

</div>

> [@Robert\_Ascan](#):
>
> tf.linalg.matrix\_transpose

I too am facing difficulty in passing this.  
I used the tf.nn.softmax on logits → transpose → cross\_ectropy with from\_logits=True.  
I get following output  
tf.Tensor(10.425234, shape=(), dtype=float32).

Not sure where I am going wrong

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [October 11, 2022, 2:38pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/7 "2022-10-11T14:38:21Z")

</div>

The point of `from_logits = True` is that it tells the cost function to do the `softmax` internally. So you’ve done softmax _twice_, which is why it doesn’t work. Actually it’s even a little worse than that: if you did `softmax` _before_ the transpose, then the `softmax` was also computed on the wrong axis. It matters, right?

---

<div class="post-metadata">

**Author:** ![Arul\_Raj](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Arul\_Raj](https://dlai.staged-by-discourse.com/u/Arul_Raj)\
**Post date:** [October 11, 2022, 5:38pm UTC](https://dlai.staged-by-discourse.com/t/wk-3-prog-exercise-6-do-i-have-to-reshape-the-logits-and-labels/216687/8 "2022-10-11T17:38:37Z")

</div>

I tried without softmax as well.  
The problem was I was using pred and actual in reverse. Interchanged and it worked.  
Thanks for helping me out.
