# Question on Exercise 5 Week 5 Programming Assignment

**URL:** <https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628>\
**Category:** Neural Networks and Deep Learning\
**Tags:** week-module-2, coursera-platform\
**Created:** [January 8, 2025, 8:35pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628 "2025-01-08T20:35:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![reinhardt\_scott](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@reinhardt\_scott](https://community.deeplearning.ai/u/reinhardt_scott)\
**Post date:** [January 8, 2025, 8:35pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/1 "2025-01-08T20:35:37Z")

</div>

I am trying to figure out excersize 5 on th eprogramming assignment for week 2 but can’t figure out what I am doing wrong. The professor wrote that dw = 1/m \* np.sum(dz) and dw = 1/m \* X\*dz\_transpose, using np.dot to multiply matricies. For some reason my answers keep coming out way off. The original mathematical equations are ∂𝐽∂𝑤=1𝑚𝑋(𝐴−𝑌)𝑇 and ∂𝐽∂𝑏=1𝑚∑𝑖=1𝑚(𝑎(𝑖)−𝑦(𝑖)).

 ![lab1_ex5](https://global.discourse-cdn.com/dlai/original/3X/b/f/bfef09756c8b8379cf26577ffd0cd9be160833a3.png)

---

<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:** [January 8, 2025, 8:37pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/2 "2025-01-08T20:37:46Z")

</div>

Please show us the results and errors that you are getting when you run the test for `propagate`.

---

<div class="post-metadata">

**Author:** ![reinhardt\_scott](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@reinhardt\_scott](https://community.deeplearning.ai/u/reinhardt_scott)\
**Post date:** [January 8, 2025, 8:58pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/3 "2025-01-08T20:58:46Z")

</div>

## Here is what I got. For cost I used the formula 𝐽=−1/𝑚∑(𝑦(𝑖)log(𝑎(𝑖))+(1−𝑦(𝑖))log(1−𝑎(𝑖))) and for some reason. My copst function came out as “nan” so it is possible I did something wrong there. dw = [[4.8] [13.71]] db = 0.36666666666666653 cost = nan

AssertionError Traceback (most recent call last)  
 in   
17 print ("cost = " + str(cost))  
18  
—\> 19 propagate\_test(propagate)

~/work/release/W2A2/public\_tests.py in propagate\_test(target)  
49 assert type(grads[‘dw’]) == np.ndarray, f"Wrong type for grads[‘dw’]. {type(grads[‘dw’])} != np.ndarray"  
50 assert grads[‘dw’].shape == w.shape, f"Wrong shape for grads[‘dw’]. {grads[‘dw’].shape} != {w.shape}"  
—\> 51 assert np.allclose(grads[‘dw’], expected\_dw), f"Wrong values for grads[‘dw’]. {grads[‘dw’]} != {expected\_dw}"  
52 assert np.allclose(grads[‘db’], expected\_db), f"Wrong values for grads[‘db’]. {grads[‘db’]} != {expected\_db}"  
53 assert np.allclose(cost, expected\_cost), f"Wrong values for cost. {cost} != {expected\_cost}"

AssertionError: Wrong values for grads[‘dw’]. [[5.55]  
[14.985]  
[5.985]] != [[-0.03909333]  
[0.12501464]  
[-0.99960809]]

Expected output

dw = [[0.25071532]  
[-0.06604096]]  
db = -0.1250040450043965  
cost = 0.15900537707692405

---

<div class="post-metadata">

**Author:** ![reinhardt\_scott](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@reinhardt\_scott](https://community.deeplearning.ai/u/reinhardt_scott)\
**Post date:** [January 8, 2025, 9:13pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/4 "2025-01-08T21:13:00Z")

</div>

## When I change all matrix multiplication to np.dot when computing cost I get the error as shown below.

ValueError Traceback (most recent call last)  
 in   
6 X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])  
7 Y = np.array([[1, 1, 0]])  
----\> 8 grads, cost = propagate(w, b, X, Y)  
9  
10 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)  
34 A = np.dot(np.transpose(w), X) + b  
35  
—\> 36 cost = np.dot((-1 / m), np.sum(np.dot(Y, np.log(A)) + np.dot((1 - Y), np.log(1 - A))))  
37 # YOUR CODE ENDS HERE  
38

\< **array\_function** internals\> in dot(\*args, \*\*kwargs)

ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

Expected output

dw = [[0.25071532]  
[-0.06604096]]  
db = -0.1250040450043965  
cost = 0.15900537707692405

---

<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:** [January 8, 2025, 9:18pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/5 "2025-01-08T21:18:51Z")

</div>

You don’t need dot products to compute the cost. You can use them, but if you do, then you need to be cognizant of the rules for dot products: the inner dimensions need to agree, right? So if you have two (1,3) vectors, then you need to transpose one of them. And the order of the operation matters: please have a look at [this thread](https://community.deeplearning.ai/t/week3-programming-assignment-4-4-compute-the-cost/18646/6) to understand why.

But if you use `np.dot`, then the advantage of that is that it does both the multiplication and the addition in one shot. So you don’t need the `np.sum` in that case. And you never need `np.dot` to multiply by a constant like -\frac {1}{m}.

The other way to solve the cost is to use elementwise multiply (“\*”) between the pairs of vectors and then use `np.sum` to add up all the products. That might be more straightforward and that way does not require any transposes.

---

<div class="post-metadata">

**Author:** ![reinhardt\_scott](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@reinhardt\_scott](https://community.deeplearning.ai/u/reinhardt_scott)\
**Post date:** [January 9, 2025, 7:49pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/6 "2025-01-09T19:49:28Z")

</div>

I am still very stuck with this question.

I am trying to do the math myself as I am having a hard time visualizing it.  
If w = [[1], [2]] a (2x1) matrix  
X = [[1, -2, -1], [3, 0.5, -3.2]] a (2x3) matrix  
To compute A via the formula for A, I would take the transpose of w meaning w.T would become [1, 2] which is a (1x2) matrix. w.T o X means that w.T has a shape of (1x2) and X has a shape of (2x3) resulting in a matrix of shape (1x3) and after adding b [8.5, 0.5, -5.9].

I am getting stuck when I take the log of A. taking the log of -5.9 results in the imaginary number -0.301+1.364\*i which is an imaginary number. I am not sure what I am doing wrong here.

---

<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:** [January 9, 2025, 7:51pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/7 "2025-01-09T19:51:27Z")

</div>

But A is the output of sigmoid, right? So all the values should be between 0 and 1 and have logs between -\infty and 0.

---

<div class="post-metadata">

**Author:** ![reinhardt\_scott](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@reinhardt\_scott](https://community.deeplearning.ai/u/reinhardt_scott)\
**Post date:** [January 9, 2025, 8:22pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/9 "2025-01-09T20:22:10Z")

</div>

Oh do you mean apply the sigmoid formula to the numbers in the matrix?  
so [1/(1+e^-(8.5), …] resulting in a matrix of [0.997, 0.622, 0.0027].

---

<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:** [January 9, 2025, 8:33pm UTC](https://community.deeplearning.ai/t/question-on-exercise-5-week-5-programming-assignment/748628/10 "2025-01-09T20:33:02Z")

</div>

Look at the formula for A in the instructions. We wrote the `sigmoid` function earlier. Just call that function with Z as the input:

Z = w^T \cdot X + b  
A = sigmoid(Z)
