W2_A2_Ex-5_forward and backward propagation

Having difficulty with the forward propagation and the backward propagation (grad) function. I am getting errors. Trying to use the following code :-1:
cost = - 1 / m*( np.dot( Y, np.log(A)) + np.dot( (1-Y), np.log (1 - A)) )
Need to understand the concept and how to go about rationalizing this problem so I can solve it. Please guide me, I am not a natural programmer.

HI @PhilU

Welcome to the community!

You here computing the Cost function, The error here because the dot product rules in this code so you we want to compute the dot product(outer product) to get [1,1] array, so you want transpose y before computing the dot product
Note also you want to transpose the vector (1-Y)


If I transpose the vectors Y and (1-Y) then they become ( m , n) vectors?
I am still having trouble with the code currently.
Please see below code and error message.

File β€œβ€, line 45
db = 1 / m * (A - Y)
SyntaxError: invalid syntax

b is a scalar so I did not use the np.dot() function. However both A and Y as well are vectors. Is this ok?
Thanks for your help?

Hello, PhilU,

There’s a syntax error. That means you have missed any symbol or parenthesis in the previous line of code. Check it once and you will get the solution.

Hello Rashmi,
Its difficult for me to identify the syntax error.
Can you inform me what it is ?

Hello, PhilU.

It’s against the code of conduct to help you with the code. You have to to identify on your own. When any line of code throws a syntax error, one should check the previous line of code to debug it.

Ok. Thanks for the hints. The problem with the code was with my brackets.
That error message has been resolved.
The ValueError signifies that there is something wrong with my code.
Python is unable to broadcast

in propagate(w, b, X, Y)
42 # db = …
β€”> 44 dw = np.dot(1/m, (np.dot(X,(A - Y).T)))
46 db = np.dot ( 1/m , (A-Y))

ValueError: operands could not be broadcast together with shapes (1,2) (1,3)

X is a (2,3) vector and Y is a (1,3) vector. A is also a (1,3) vector.
The dot product of X and (A - Y).T is : (2,3) x (3,1) which is a (2,1) vector.
Is this analysis right ?

Hello, PhilU.

The operands here will work only if the inner dimension will match each other (it’s one of the conditions for the dot product).

X is a (2,3) vector and Y is a (1,3) vector. A is also a (1,3) vector.
The dot product of X and (A - Y).T is : (2,3) x (3,1) which is a (2,1) vector.
m is a scalar so no need for dot product just do straight multiplication.
Is this analysis right ?

Hello, PhilU.

Very sorry, oversaw your previous mentioning. You were right. But there’s one problem the way you are calling the function for dw.

The instruction says this:


You need to check, how are you using the formula. I think you are missing a bracket here (for 1/m) and that is changing the whole equation.