# Week 2 assignment

**URL:** <https://community.deeplearning.ai/t/week-2-assignment/10231>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [May 29, 2021, 6:00pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231 "2021-05-29T18:00:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandeep\_Kumar\_Kalva](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@Sandeep\_Kumar\_Kalva](https://community.deeplearning.ai/u/Sandeep_Kumar_Kalva)\
**Post date:** [May 29, 2021, 6:00pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/1 "2021-05-29T18:00:18Z")

</div>

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./m_np.sum((Y_np.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)

I cannot find how to solve this error. Please help me in understanding this. Thanks.

---

<div class="post-metadata">

**Author:** ![albertovilla](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/albertovilla/32/4530_2.png) [@albertovilla](https://community.deeplearning.ai/u/albertovilla)\
**Post date:** [May 29, 2021, 8:00pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/2 "2021-05-29T20:00:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sandeep\_Kumar\_Kalva](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@Sandeep\_Kumar\_Kalva](https://community.deeplearning.ai/u/Sandeep_Kumar_Kalva)\
**Post date:** [May 30, 2021, 7:52am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/3 "2021-05-30T07:52:03Z")

</div>

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)_

_assert type(grads[“dw”]) == np.ndarray_  
_assert grads[“dw”].shape == (2, 1)_  
_assert type(grads[“db”]) == np.float64_

_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./m_np.sum(Y_np.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.

---

<div class="post-metadata">

**Author:** ![albertovilla](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/albertovilla/32/4530_2.png) [@albertovilla](https://community.deeplearning.ai/u/albertovilla)\
**Post date:** [May 30, 2021, 1:36pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/4 "2021-05-30T13:36:20Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![Sandeep\_Kumar\_Kalva](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@Sandeep\_Kumar\_Kalva](https://community.deeplearning.ai/u/Sandeep_Kumar_Kalva)\
**Post date:** [May 30, 2021, 3:22pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/5 "2021-05-30T15:22:40Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Joda](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Joda](https://community.deeplearning.ai/u/Joda)\
**Post date:** [May 30, 2021, 4:51pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/6 "2021-05-30T16:51:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Kumar\_Kalva](https://avatars.discourse-cdn.com/v4/letter/s/df788c/32.png) [@Sandeep\_Kumar\_Kalva](https://community.deeplearning.ai/u/Sandeep_Kumar_Kalva)\
**Post date:** [May 31, 2021, 5:37am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/7 "2021-05-31T05:37:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jimmyhe](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jimmyhe](https://community.deeplearning.ai/u/jimmyhe)\
**Post date:** [June 7, 2021, 1:59pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/8 "2021-06-07T13:59:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harrisonliang](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@harrisonliang](https://community.deeplearning.ai/u/harrisonliang)\
**Post date:** [July 1, 2021, 9:00am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/9 "2021-07-01T09:00:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Prashanth\_Kamath](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Prashanth\_Kamath](https://community.deeplearning.ai/u/Prashanth_Kamath)\
**Post date:** [October 23, 2021, 9:40pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/10 "2021-10-23T21:40:16Z")

</div>

Hi @albertovilla,

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)
# Gradient descent 
# 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.

---

<div class="post-metadata">

**Author:** ![Purvaja\_Soochit](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Purvaja\_Soochit](https://community.deeplearning.ai/u/Purvaja_Soochit)\
**Post date:** [February 14, 2022, 2:28am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/11 "2022-02-14T02:28:58Z")

</div>

I used function propagate instead of optimize

---

<div class="post-metadata">

**Author:** ![mrqasimasif](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/mrqasimasif/32/30586_2.png) [@mrqasimasif](https://community.deeplearning.ai/u/mrqasimasif)\
**Post date:** [May 25, 2023, 11:51am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/12 "2023-05-25T11:51:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [May 25, 2023, 11:56am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/13 "2023-05-25T11:56:47Z")

</div>

Please post your full error.

---

<div class="post-metadata">

**Author:** ![Faisal\_Bin\_Basha](https://avatars.discourse-cdn.com/v4/letter/f/7ba0ec/32.png) [@Faisal\_Bin\_Basha](https://community.deeplearning.ai/u/Faisal_Bin_Basha)\
**Post date:** [August 2, 2023, 2:43pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/14 "2023-08-02T14:43:43Z")

</div>

Hi, even I have this exact same issue I am not sure where my mistake is ?

 ![logistic_regression](https://global.discourse-cdn.com/dlai/original/3X/5/f/5f5d664fe1b7e808582ec8d5943a13fbcc740d46.png)

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [August 2, 2023, 3:50pm UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/15 "2023-08-02T15:50:47Z")

</div>

I’ve replied to your original post [here](https://community.deeplearning.ai/t/propogate-c1-w2-assignment/405755/2).

---

<div class="post-metadata">

**Author:** ![saifkhanengr](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/saifkhanengr/32/152858_2.png) [@saifkhanengr](https://community.deeplearning.ai/u/saifkhanengr)\
**Post date:** [August 3, 2023, 6:00am UTC](https://community.deeplearning.ai/t/week-2-assignment/10231/17 "2023-08-03T06:00:00Z")

</div>

Closed on August 3, 2023, 6:00am UTC


