# Week 2 assignment

ValueError Traceback (most recent call last)
in
----> 1 params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)
2
3 print ("w = " + str(params[“w”]))
4 print ("b = " + str(params[“b”]))
5 print ("dw = " + str(grads[“dw”]))

in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35 # YOUR CODE STARTS HERE
36
—> 37 grads, cost = propagate(w, b, X, Y)
38 # YOUR CODE ENDS HERE
39

in propagate(w, b, X, Y)
29 # cost = …
30 # YOUR CODE STARTS HERE
—> 31 A = sigmoid(np.dot(w.T, X) + b)
32 cost = -1./mnp.sum((Ynp.log(A)+(1-Y)*np.log(1-A)))
33 # YOUR CODE ENDS HERE

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

The error indicates that the `np.dot` operation fails because the inputs to that function are not compatible with the matrix multiplication operation, therefore something must be wrong with the shape of the inputs.

For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The result matrix has the number of rows of the first and the number of columns of the second matrix. - Wikipedia

1 Like

Dear Albertovilla,

Thank you for your response. The function ‘propagate’ (where ‘A’ is defined) was written correctly before implementing the function ‘optimize’. When I run the following code that is given after defining ‘propagate’ function, the test is passed as shown below:
w = np.array([[1.], [2.]])
b = 2.
X =np.array([[1., 2., -1.], [3., 4., -3.2]])
Y = np.array([[1, 0, 1]])
grads, cost = propagate(w, b, X, Y)

print ("dw = " + str(grads[“dw”]))
print ("db = " + str(grads[“db”]))
print ("cost = " + str(cost))

propagate_test(propagate)

dw = [[0.99845601]

• [2.39507239]]*
db = 0.001455578136784208
cost = 5.801545319394553
All tests passed!

However, After defining the function ‘optimise’, when test function by running the below lines
params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)

print ("w = " + str(params[“w”]))
print ("b = " + str(params[“b”]))
print ("dw = " + str(grads[“dw”]))
print ("db = " + str(grads[“db”]))
print("Costs = " + str(costs))

optimize_test(optimize)

I get this error which is weird b/c it ‘np.dot’ worked before and now its giving the error:

ValueError Traceback (most recent call last)
in
----> 1 params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)
2
3 print ("w = " + str(params[“w”]))
4 print ("b = " + str(params[“b”]))
5 print ("dw = " + str(grads[“dw”]))

in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
36 # YOUR CODE STARTS HERE
37
—> 38 grads, cost = propagate(w, b, X, Y)
39 # YOUR CODE ENDS HERE
40

in propagate(w, b, X, Y)
29 # cost = …
30 # YOUR CODE STARTS HERE
—> 31 A = sigmoid(np.dot(w.T, X) + b)
32 cost = -1./mnp.sum(Ynp.log(A)+(1-Y)*np.log(1-A))
33 # YOUR CODE ENDS HERE

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

Please do suggest where am I doing it wrong. Thank you.

I guess somehow you are modifying some of the parameters, for example, `w`.

In the `propagate` test case you have `assert grads[“dw”].shape == (2, 1)`, so `w.T` cannot be `(2,3)`, it should be `(1, 2)`. What’s the print out of `w` in `optimize`?

I have printed out the values of w, dw, X, b in optimise.
w = [[1.]
[2.]]
dw =[[0.99845601]
[2.39507239]]
X =[[ 1. 2. -1. ]
[ 3. 4. -3.2]]
b = 2.0
so w is of size(2,1) and w.T should be (1,2) and X is of (2,3). So np.dot(w.T,X) should be of shape (1,3). But not sure why it is showing as (2,3).

2 Likes

I am also having the exact same error as Sandeep, which I posted below:

ValueError Traceback (most recent call last)
in
----> 1 params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)
2
3 print ("w = " + str(params[“w”]))
4 print ("b = " + str(params[“b”]))
5 print ("dw = " + str(grads[“dw”]))

in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35 # grads, cost = …
36 # YOUR CODE STARTS HERE
—> 37 grads, cost = propagate(w, b, X, Y)
38
39 # YOUR CODE ENDS HERE

in propagate(w, b, X, Y)
29 # cost = …
30 # YOUR CODE STARTS HERE
—> 31 A = sigmoid(np.dot(w.T, X) + b)
32 cost = (-1/m)*(np.dot(Y, np.log(A).T) + np.dot(1 - Y, np.log(1 - A).T))
33

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

I have not made any hard coded size assertions in my own code and haven’t changed and of the assert statements in the assignment. I am not sure how else I can approach fixing the issue.

1 Like

I have solved this problem. I was writing wrongly the equation for updating the term `b` in `optimize` function. After correcting it worked. There were no errors. Please do check your code carefully.

4 Likes

First time I look into this comment, I didn’t get it. But actually when I look more carefully, that’s it!

I really like your answer, I made a silly mistake on the interation b, now the problem was solved

I fixed this issue however I get a test error stating,

"AssertionError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
117 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
118 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
→ 119 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for d[‘costs’]. {d[‘costs’]} != {expected_output[‘costs’]}"
120
121 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"

AssertionError: Wrong values for d[‘costs’]. [array(0.15900538)] != [array(0.69314718)]
".

For the code written as follows

# (≈ 1 line of code)

``````# initialize parameters with zeros
# w, b = ...
w, b = initialize_with_zeros(X_train.shape[0])

#(≈ 1 line of code)
# params, grads, costs = ...
params, grads, cost = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost = False)
``````

The cost function passes all the tests. I am not able to resolve this and move forward at all. Any suggestions to improve the code? Kindly advise.

I used function propagate instead of optimize

Hi, I am facing the same problem, did you happen to find the solution?