# Getting error for computing "total loss" in Week 3 TensorFlow assignment

**URL:** <https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** week-module-3, coursera-platform\
**Created:** [April 5, 2024, 9:22am UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906 "2024-04-05T09:22:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagar\_gupta16](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sagar\_gupta16](https://community.deeplearning.ai/u/sagar_gupta16)\
**Post date:** [April 5, 2024, 9:22am UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/1 "2024-04-05T09:22:21Z")

</div>

When I am calculating the total loss I used following code:

_{moderator edit - solution code removed}_

But I am getting below error:

 ![image](https://global.discourse-cdn.com/dlai/original/3X/f/1/f14fc80853d2557b6a5c87d3063fd57e98f5e6e3.png)

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [April 5, 2024, 9:50am UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/2 "2024-04-05T09:50:31Z")

</div>

Hi @sagar_gupta16 ,

The order of the parameters passed to the tf.keras.losses.categorical\_crossentropy()function is incorrect. The first parameter should be label, not logits as you have done in your code.

---

<div class="post-metadata">

**Author:** ![sagar\_gupta16](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sagar\_gupta16](https://community.deeplearning.ai/u/sagar_gupta16)\
**Post date:** [April 5, 2024, 12:44pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/3 "2024-04-05T12:44:33Z")

</div>

Hi @Kic ,  
Even after changing the parameters I am getting same error:

_{moderator edit - solution code removed}_

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [April 5, 2024, 2:34pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/4 "2024-04-05T14:34:00Z")

</div>

Hi @sagar_gupta16 ,

Have you rerun your code after editing? I suggest you rerun the code from the beginning.

---

<div class="post-metadata">

**Author:** ![sagar\_gupta16](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sagar\_gupta16](https://community.deeplearning.ai/u/sagar_gupta16)\
**Post date:** [April 5, 2024, 2:49pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/5 "2024-04-05T14:49:57Z")

</div>

Hi @Kic ,  
I re-ran the all the blocks and getting this error:

ValueError: Shape mismatch: The shape of labels (received (12,)) should equal the shape of logits except for the last dimension (received (2, 6)).

---

<div class="post-metadata">

**Author:** ![sagar\_gupta16](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sagar\_gupta16](https://community.deeplearning.ai/u/sagar_gupta16)\
**Post date:** [April 5, 2024, 3:02pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/6 "2024-04-05T15:02:55Z")

</div>

Hi @Kic ,  
As per my understanding, when we run “one\_hot\_matrix\_test” function the output is “[1. 0. 0. 0. 0. 0.], shape=(6,), dtype=float32” so I believe the problem is in the way we get “new\_y\_train” it’s shape is coming (12,) which is why the other logic is not working.

---

<div class="post-metadata">

**Author:** ![Kic](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kic/32/76339_2.png) [@Kic](https://community.deeplearning.ai/u/Kic)\
**Post date:** [April 5, 2024, 4:28pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/7 "2024-04-05T16:28:02Z")

</div>

Hi @sagar_gupta16 ,

The input arguments of the comput\_total\_cost() function are passed in. If you look at the cell below, you can see the code cell for testing this function, where the logits and labels are constructed. What you can do is to use a print statement at the start of the function to print out the shape of both the labels and logits to check.

---

<div class="post-metadata">

**Author:** ![sagar\_gupta16](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@sagar\_gupta16](https://community.deeplearning.ai/u/sagar_gupta16)\
**Post date:** [April 5, 2024, 4:37pm UTC](https://community.deeplearning.ai/t/getting-error-for-computing-total-loss-in-week-3-tensorflow-assignment/604906/8 "2024-04-05T16:37:49Z")

</div>

Hi @Kic  
Thanks for helping in resolving the issue.
