# week2\_Exercise 5 - propagate

**URL:** <https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [September 29, 2023, 7:38am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080 "2023-09-29T07:38:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vemula\_suman](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@vemula\_suman](https://community.deeplearning.ai/u/vemula_suman)\
**Post date:** [September 29, 2023, 7:38am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/1 "2023-09-29T07:38:19Z")

</div>

code:

A=1/1+np.exp(-(np.matmul(w.T,X))+b)  
cost=-((np.dot(Y,np.log(A)))+(np.dot((1-Y),np.log(1-A))))/m

dw=np.matmul(X,((A-Y).T))  
db=np.sum(A-Y )

output:

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

in propagate(w, b, X, Y)  
32 # YOUR CODE STARTS HERE  
33 A=1/1+np.exp(-(np.matmul(w.T,X))+b)  
—\> 34 cost=-((np.dot(Y,np.log(A)))+(np.dot((1-Y),np.log(1-A))))/m  
35  
36 # YOUR CODE ENDS HERE

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

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

please help to write code and any sujjestions

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [September 29, 2023, 7:44am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/2 "2023-09-29T07:44:26Z")

</div>

In your `cost` calculation, you have to use transpose for one term, `Y` or `A`.

---

<div class="post-metadata">

**Author:** ![vemula\_suman](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@vemula\_suman](https://community.deeplearning.ai/u/vemula_suman)\
**Post date:** [September 29, 2023, 9:51am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/3 "2023-09-29T09:51:11Z")

</div>

![image](https://global.discourse-cdn.com/dlai/original/3X/a/c/ac8e9f0c4ed2043599b58b5c8d554af1089f461e.png)

but in the given formula it is not there,please see my image

---

<div class="post-metadata">

**Author:** ![vemula\_suman](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@vemula\_suman](https://community.deeplearning.ai/u/vemula_suman)\
**Post date:** [September 29, 2023, 10:36am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/4 "2023-09-29T10:36:07Z")

</div>

hi Saif ,

but in the cost equation ,there is not matrix transpose term.

 ![image.png](https://global.discourse-cdn.com/dlai/original/3X/a/c/ac8e9f0c4ed2043599b58b5c8d554af1089f461e.png)

Thank you very much for responding

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [September 29, 2023, 2:27pm UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/5 "2023-09-29T14:27:44Z")

</div>

If you choose to use dot product, then you have to transpose one term. There are other ways too for which you don’t need transpose, like using multiply and then sum…

---

<div class="post-metadata">

**Author:** ![vemula\_suman](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@vemula\_suman](https://community.deeplearning.ai/u/vemula_suman)\
**Post date:** [September 29, 2023, 3:25pm UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/6 "2023-09-29T15:25:33Z")

</div>

what should be the output of cost function ,wheather it is nd.array or float value.

please confirm  
thank you

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [September 30, 2023, 5:47am UTC](https://community.deeplearning.ai/t/week2-exercise-5-propagate/458080/7 "2023-09-30T05:47:39Z")

</div>

What do you think it should be? Can you see the expected output below the test cell?
