Why syntax error in last cost statement cost=np.squeeze()

Programming exercise 2 in section 4.5
why the SyntaxError: invalid syntax in your line of code:
cost = np.squeeze(np.array(cost))

m = X.shape[1]

rows 1, cols 0

Here was my code that proceeded the error. Did I incorrectly code cost so that the syntax error occurred? Array dimensions incorrect? Thank you

YOUR CODE STARTS HERE

A=sigmoid(np.dot(w.T,X))
ma= A.shape[1]
#grads,
cost=(-np.dot(Y,np.log(A))-np.dot((1-Y),np.log(1-A)))/m
mb=Y.shape[1]

YOUR CODE ENDS HERE

BACKWARD PROPAGATION (TO FIND GRAD)

YOUR CODE STARTS HERE

dw=(np.dot(X,(A-Y).T))/m
db=(np.sum((A-Y))/m

YOUR CODE ENDS HERE

cost = np.squeeze(np.array(cost))

Check the code in your graded block which is right before that statement. Do you see any issue there?

I have questions how to represent in Python dw = 1/m X dz^T
where dz = (A-Y)
Is this a dot product np.dot(X,dz^T) or Hadamard mult X*dz^T?
Thanks

When you get a syntax error at the beginning a line, it almost always means that you have a missing close parethesis or close bracket on the previous line. Also check your indentation, although if that’s the problem the error message will usually mention that explicitly.

Thanks, I see the missing paren, db=(np.sum((A-Y)))/m

But still question the correct Python code for dw=(np.dot(X,dz^T))/m
Is dot product correct here for dw=(np.dot(X,(A-Y).T))/m

Yes, they give you the formula in the instructions. The only ambiguity is to realize that when they write matrices adjacent with no multiplication sign, it means “dot product”. When they mean “elementwise multiply”, they will explicitly use “*” to indicate that.

Thanks so much. That troubled me

Another problem with array dimensions in exercise 4.6 week 2 programming assignment:
ValueError Traceback (most recent call last)
in
3 X =np.array([[1., 2., -1.], [3., 4., -3.2]])
4 Y = np.array([[1, 0, 1]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)
41 #cost=-1/m*(np.sum([np.dot(Y,np.log(A)),np.dot((1-Y),np.log(1-A))],axis=1, keepdims=True)
42 #cost=-1/m*(np.sum([[np.dot(Y,np.log(A))],[np.dot((1-Y),np.log(1-A))]],axis=1, keepdims=True)
—> 43 cost=1/m*(-np.dot(Y,np.log(A))-np.dot((1-Y),np.log(1-A)))
44 mc=cost.shape
45 #mb=Y.shape[1]

<array_function internals> in dot(*args, **kwargs)

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

@Nahammond the error is pointing you in the right direction to solve it, check the shapes of the items you are multiplying, e.g. Y and np.log(A). Their dimensions are incorrect for a proper matrix multiplication.

Matrix multiplication rule from Wikipedia:

image

Also the shape of one of your elements in np.dot doesn’t look right i.e. (4,) must be wrong so check how you calculate it.

Thank you! I know linear algebra. No excuse! But I did not know where the error was