# Computing cost function

**URL:** <https://dlai.staged-by-discourse.com/t/computing-cost-function/92047>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [January 27, 2022, 10:28am UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047 "2022-01-27T10:28:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASHISH\_KUMAR\_MISHRA](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ashish_kumar_mishra/32/13594_2.png) [@ASHISH\_KUMAR\_MISHRA](https://dlai.staged-by-discourse.com/u/ASHISH_KUMAR_MISHRA)\
**Post date:** [January 27, 2022, 10:28am UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/1 "2022-01-27T10:28:02Z")

</div>

Sir after running the cost function I am getting this error. I have taken the mean of cost also using reduce\_mean.  
AssertionError: Test does not match. Did you get the mean of your cost functions?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://dlai.staged-by-discourse.com/u/balaji.ambresh)\
**Post date:** [January 27, 2022, 2:16pm UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/2 "2022-01-27T14:16:08Z")

</div>

The shape of logits and labels should be of the form (num examples, num classes).

The inputs to the function are (num classes, num examples). Are you taking care of that?

Also, the parameter `from_logits` should be taken care of.

---

<div class="post-metadata">

**Author:** ![ASHISH\_KUMAR\_MISHRA](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ashish_kumar_mishra/32/13594_2.png) [@ASHISH\_KUMAR\_MISHRA](https://dlai.staged-by-discourse.com/u/ASHISH_KUMAR_MISHRA)\
**Post date:** [January 27, 2022, 6:34pm UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/3 "2022-01-27T18:34:30Z")

</div>

So should I transpose the tensors logits and labels.?  
Also could you tell me the role parameter from\_logits play? I could not figure it out from the official documentation page of tensorflow.  
Regards

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://dlai.staged-by-discourse.com/u/balaji.ambresh)\
**Post date:** [January 27, 2022, 6:54pm UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/4 "2022-01-27T18:54:41Z")

</div>

Should you transpose? yes.  
Logits are used when the normalization is not done i.e. you’re using just the raw outcomes. Hopefully, this link helps: [python - from\_logits=True and from\_logits=False get different training result for tf.losses.CategoricalCrossentropy for UNet - Stack Overflow](https://stackoverflow.com/questions/57253841/from-logits-true-and-from-logits-false-get-different-training-result-for-tf-loss)

---

<div class="post-metadata">

**Author:** ![ASHISH\_KUMAR\_MISHRA](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ashish_kumar_mishra/32/13594_2.png) [@ASHISH\_KUMAR\_MISHRA](https://dlai.staged-by-discourse.com/u/ASHISH_KUMAR_MISHRA)\
**Post date:** [January 28, 2022, 9:25am UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/5 "2022-01-28T09:25:55Z")

</div>

Sir by normalization you mean that for last layer in this case performing softmax operation. In forward propagation we have calculated only upto z3. The input in the cost function is logits is it z3? Should I calculate a3 by calling softmax function and then pass it on to cost function?  
Regards

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://dlai.staged-by-discourse.com/u/balaji.ambresh)\
**Post date:** [January 28, 2022, 1:01pm UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/6 "2022-01-28T13:01:40Z")

</div>

Setting the `from_logits` flag to True is sufficient.

---

<div class="post-metadata">

**Author:** ![ASHISH\_KUMAR\_MISHRA](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ashish_kumar_mishra/32/13594_2.png) [@ASHISH\_KUMAR\_MISHRA](https://dlai.staged-by-discourse.com/u/ASHISH_KUMAR_MISHRA)\
**Post date:** [January 29, 2022, 9:02am UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/7 "2022-01-29T09:02:20Z")

</div>

Sir,  
I am taking these three steps:

[Removed code]

I am getting an assertion error:

```auto
AssertionError: Test does not match. Did you get the mean of your cost functions?

```

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://dlai.staged-by-discourse.com/u/balaji.ambresh)\
**Post date:** [January 29, 2022, 6:11pm UTC](https://dlai.staged-by-discourse.com/t/computing-cost-function/92047/8 "2022-01-29T18:11:43Z")

</div>

The loss function you want to use is `tf.keras.losses.categorical_crossentropy()`. It’s provided in the exercise write up in the markdown cell right above the code cell.
