C1W2 Logistic Regression Programming

Please add a tag indicating where you are taking the course. Choose only one of the platform options in the tag section then the week/module:

  • coursera-platform
  • Hi, I’m getting an error message in the propagate function:
  •   File "<ipython-input-44-ede505e9f556>", line 37
        cost = - 1/m *(np.sum(Y * np.log(A)+(1-Y)*np.log(1-A)))
           ^
    SyntaxError: invalid syntax
    
  • Any help would be great. Thank you.

When you get a syntax error at the beginning of a line like that, it usually means there are mismatching parentheses on the previous (non-comment) code line. Most likely a missing “close paren”.

Note that the editor in the notebook is “syntax aware”: just click on a paren and it will highlight the matching one. Or not :weary_cat:

The “meta” point here is that if you can’t find the syntax error, it probably means you are looking in the wrong place. :laughing:

1 Like

The other way to get a syntax error at the beginning of a line is if there is an unexpected indentation. But in my experience, the python intepreter is pretty good about making the error message more specific in that case: it will mention that indentation is the problem.

The message you show is not about indentation, so it most likely means the error is before the line where it “throws”. The interpreter keeps looking forward hoping to find the close paren or close bracket that it is expecting and then it “throws” only when it sees something that conflicts with that.

Thank you very much for your help. I’ll try to look for errors before that line.

The error is gone after I ran the code from the top of the page. But I’m getting another error in the next section:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-a16910aa3a0e> in <module>
      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

<ipython-input-13-93b78fd9318c> in propagate(w, b, X, Y)
     33 
     34     A = sigmoid(np.dot(w.T,X)+b)
---> 35     cost = -np.sum(np.dot(Y,np.log(A))+np.dot(1-Y,np.log(1-A)))
     36     # YOUR CODE ENDS HERE
     37 

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

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

Thank you very much for taking the time to help out.

Hello @jcclui,

To do A dot B, we need to match the last shape value of A with the first shape value of B, meaning that, for your case, with reference to the error message, you want to have (1, 3) and (3, 1).

This means you need to change the shape of the “B” that is in your code. To change the shape that way, we do transpose, and you have done it multiple times in this exercise by using the syntax .T. I understand that it was not shown in the equations, but we just need it. Try .T, but remember that there may be more than one “B” that you will need to transpose for the line of code marked in the error message, because you have more than one dot product.

Good luck!
Raymond

What you want to do is take the elementwise products of two 1 x m vectors and then add up those products to get a scalar value. There are two ways you can do that:

Use * to do “elementwise” multiply, followed by np.sum to do the addition.

Use np.dot to do both operations in “one shot”, but if you do it that way, you’ll need to transpose the second vector so that you dot 1 x m with m x 1, giving a scalar result.

You’ve done a mixed strategy where you did it in two steps but used np.dot without the transpose as the first step.

1 Like

Also note that you had that part correct using the first strategy that I just described in the code we can see in your first post on this thread.

Hi Raymond, thank you very much for taking the time to help out. I’ll look into the shapes of the matrices.

Justin

Thank very much for your help @paulinpaloalto . I’ll try both methods.