I have invalid syntax

Hello. I have invalid syntax
dw = (1/m)*np.dot(((A-Y).T),X)
SyntaxError: invalid syntax
Help me please

Hi @plohih_love ,

Question: In this case, in the dot product, are the shapes aligned?


1 Like

I need to get the vector as in the training laboratory


tic = time.process_time()
dot = np.dot(W,x1)
toc = time.process_time()
print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000 * (toc - tic)) + “ms”)
gdot = [25.89872885 27.60612795 18.06528476]
As a result, a vector with dimension W

I don’t see the invalid syntax here. I do think that the dot product has an issue due to the params passed, but not a syntax one. Could you share your code via DM so that I look at previous lines as well?



1 Like

If you can’t find a syntax error, it usually means you are looking in the wrong place :nerd_face: …

That kind of error thrown at the beginning of a line usually means you have a missing close paren or close bracket on the previous non-comment line.

1 Like

Thanks @paulinpaloalto , I thought I was in need of a new set of glasses :slight_smile: … but just in case, I did ask @plohih_love for the code to follow your advice :slight_smile:

Now, regarding the current line of code:

dw = (1/m)*np.dot(((A-Y).T),X)

… don’t you think there may be an issue in the dot parameters?


Yes, just from a “dimensional analysis” perspective, that is wrong. (A - Y)^T will have dimension m x 1 whereas X is n_x x m.

@plohih_love have a look again at the instructions where they give the mathematical formula for dw and it’s a bit different than what that code does. From a notational perspective, note that what Prof Ng means by dw is:

dw = \displaystyle \frac {\partial J}{\partial w}

Thank you. You were right: the previous line without comments is missing a closing parenthesis or closing parenthesis.

thanks again. I was frustrated by this mistake, so I began to doubt my knowledge of vector algebra and therefore changed the order, although the original code was: dw = (1/m)*np.dot(X,((A-Y).T)).

A lot of people find this behavior surprising, but you have to think about it from the point of view of the python interpreter: it gets to the end of a line and knows that the syntax is incomplete and that it still needs a closing delimiter. But the syntax allows statements to continue onto the next line, right? So it continues forward onto the next line and then it “throws” there when it sees something that conflicts with the closing parenthesis that it is looking for.

Now that you’ve found the syntax error, if you have not reviewed the dot product, you’ll get to a new error. If you did review the order of the params in that dot product, then you should have good results.

Good day. Yes i got good results on dw,db , but bad on cost:
dw = [[ 0.25071532]
db = -0.1250040450043965
cost = -2.1428184225087668

I did the calculation: cost = -(1/m)*(np.dot(Y,(np.log(A)).T)+np.dot((1-Y),(1-np.log(A)).T))

I found a mistake))) Correctly write np.log(1-A), wrong 1-np.log(A)

Thanks everyone