# Week 2 Exercise 5 - propogate function log returns NAN and dimension mismatch

**URL:** <https://community.deeplearning.ai/t/week-2-exercise-5-propogate-function-log-returns-nan-and-dimension-mismatch/565312>\
**Category:** Neural Networks and Deep Learning\
**Tags:** week-module-2, coursera-platform\
**Created:** [February 4, 2024, 10:58pm UTC](https://community.deeplearning.ai/t/week-2-exercise-5-propogate-function-log-returns-nan-and-dimension-mismatch/565312 "2024-02-04T22:58:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_Johnson1](https://avatars.discourse-cdn.com/v4/letter/d/cab0a1/32.png) [@Derek\_Johnson1](https://community.deeplearning.ai/u/Derek_Johnson1)\
**Post date:** [February 4, 2024, 10:58pm UTC](https://community.deeplearning.ai/t/week-2-exercise-5-propogate-function-log-returns-nan-and-dimension-mismatch/565312/1 "2024-02-04T22:58:15Z")

</div>

When trying to calculate the cost function  
I’m using the resulting A values and taking the log of them, but I’m getting a nan  
Why am I getting a nan? do we need to normalize these numbers?

A = [8.5 0.5 -5.9]  
log(A) = [2.14006616 -0.69314718 nan]

also, I get a ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)  
in the cost function. I’m not sure I understand the 3 (dim 1) != (dim 0)

Thanks

---

<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:** [February 4, 2024, 11:01pm UTC](https://community.deeplearning.ai/t/week-2-exercise-5-propogate-function-log-returns-nan-and-dimension-mismatch/565312/2 "2024-02-04T23:01:48Z")

</div>

It looks like your sigmoid function is incorrect or you’re not applying it. The A values are the output of sigmoid, so that means they should be between 0 and 1, right? If you take the log of a negative number like -5.9, that will not end well.

I’m guessing the dimension mismatch is on the cost calculation. It’s fine to use the dot product for that, but you need to transpose one of the operands or it doesn’t work. If A and Y are both 1 x 3, then just dotting them as is doesn’t work. The alternative is just to use elementwise multiply (“\*”) followed by `np.sum`. [Here’s a thread](https://community.deeplearning.ai/t/when-to-use-np-dot/109202/6) which discusses this in more detail.

---

<div class="post-metadata">

**Author:** ![Derek\_Johnson1](https://avatars.discourse-cdn.com/v4/letter/d/cab0a1/32.png) [@Derek\_Johnson1](https://community.deeplearning.ai/u/Derek_Johnson1)\
**Post date:** [February 4, 2024, 11:20pm UTC](https://community.deeplearning.ai/t/week-2-exercise-5-propogate-function-log-returns-nan-and-dimension-mismatch/565312/3 "2024-02-04T23:20:40Z")

</div>

Thank you Paul!
