I think you are better to check the shape of each variable. Easiest way is, you probably know, to use print(). For example, by inserting print(X.shape) before the matrix operation, you can check the shape of X. If you want to get multiple variables in multiple locations, just adding print may be slightly confusing. In that case, you can concatenate String and value like print("X.shape = "+str(X.shape)).
Then, you can see the shape of variables at anytime.
By the way, in your case, the error is quite simple. The shape of (A-Y).T is (3,1) and the shape of X is (2,3). It does not work. It ONLY works if the first parameter is (2,3) and the second parameter is (3,1). The shape of output is (2,1) obviously.
But X & Y are an input code given in test code. A is calculated using w & b which were also given in input code. And I have calculated A using the below formula:
A=sigmoid(np.dot(w.T,X)+b)
So why the shape is not matching?
Also i wanted to know how did you identify that shape of (A-Y).T is 3,1 and X is 2,3 without seeing my code or checking the output. I want to know how can i debug error by looking at it and understanding which part of the code is giving error.
But X & Y are an input code given in test code
It may be given by a test program, but you can check in your code. Just put print() statement in your code should work.
So why the shape is not matching?
This is basic math. if you want to use dot product of a and b, the shape needs to be (a,m) (m,b).
Otherwise, no one can calculate this. So, Order is important. Your oder is not correct. You need to be very careful which should be the first term.
Also i wanted to know how did you identify that shape of (A-Y).T is 3,1 and X is 2,3 without seeing my code or checking the output
You have info already. → shapes (3,1) and (2,3) not aligned:
Anyway, the first step for debugging is to put print(() statement to see what is happening in your case.
Hope this helps.
You have not implemented the formula for dw correctly. Nobu showed you the formula above and note the dot product there is X \cdot (A - Y)^T. That is not what you wrote in your code. You wrote (A - Y)^T \cdot X. One of the important things to know about matrix multiplication (dot product style) is that it is not commutative. As we see in this case, the operation is not even possible in the opposite order because the dimensions don’t work. But if you reverse it, then you have 2 x 3 dot 3 x 1 which gives a 2 x 1 output, which both works and is the correct dimension since w (and hence dw) is 2 x 1 in this particular test case.
Hi Paul,
I got it and have changed it to X.(A-Y).T. But now again getting assertion error:
AssertionError Traceback (most recent call last)
in
7 assert type(grads[“dw”]) == np.ndarray
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64
10
11
AssertionError:
However, if I run the test code by removing assert function, I get correct output matching the expected output.
One step at a time.
What is the type of your db value?
Please see below, to know type of db, I used type(grads[“db”])) in the test code and then its giving error:
ValueError Traceback (most recent call last)
in
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 print ("dw = " + str(grads[“dw”]))
ValueError: too many values to unpack (expected 2)
Well, it looks like something is wrong with your propagate function. It’s supposed to return 2 return values, but it looks like it returns more. So how did that happen? You should not need to modify the “return” statement in propagate: that was just given to you as part of the template.
Since you asked the type of db so I was trying to return the type of dw as part of function as well.
So if I check the type of db in the test code then it is returning this. I used type(grads[“db”])
dw = [[ 0.25071532] [-0.06604096]] db = [-0.12500405] cost = 0.15900537707692405
Out[12]:
numpy.ndarray
I am getting the below error in optimise function. Please suggest.
Cost after iteration 0: 0.238508
w = [[0.6340393 ]
[2.09473468]]
b = 0.6073747140171403
dw = [[ 0.40419013]
[-0.10398609]]
db = [-0.20219683]
Costs = [array([0.23850807])]
Cost after iteration 0: 8.702318
Cost after iteration 100: 0.870414
AssertionError Traceback (most recent call last)
in
7 print("Costs = " + str(costs))
8
----> 9 optimize_test(optimize)
~/work/release/W2A2/public_tests.py in optimize_test(target)
73 assert type(costs) == list, “Wrong type for costs. It must be a list”
74 assert len(costs) == 2, f"Wrong length for costs. {len(costs)} != 2"
—> 75 assert np.allclose(costs, expected_cost), f"Wrong values for costs. {costs} != {expected_cost}"
76
77 assert type(grads[‘dw’]) == np.ndarray, f"Wrong type for grads[‘dw’]. {type(grads[‘dw’])} != np.ndarray"
AssertionError: Wrong values for costs. [array([8.70231798]), array([0.87041371])] != [5.80154532, 0.31057104]
Hi Talha,
Check the way you are computing the values for w. Also, are you following the linearity for grads and cost?
Did you successfully pass a test for propagate() ?
I see the shape of “db” is not correct. This implies, you still have a problem at “assert type(grads[“db”]) == np.float64”.
No. Still getting the error.
The course is poorly designed. All these things were not even explained in the lectures. I had rated the course with 1 star and gave this feedback there. It has become frustrating. I am stuck in this assignment for almost 1 week.
w=w-learning_rate*dw
Thats how I am doing it.
Hi Talha,
The course expects you to have some pre-knowledge on python basics, hands-on machine learning and a few other basics (already mentioned when you enroll for it in the beginning).
Though, there are certain loopholes that are consistently get covered either by the staff or issues being dicussed by the mentors at this platform. I think, a bit of practice and carefully understanding what is required will sucessully make you a good learner on DLS.
We all are here to help you out. The thing is that we are fellow students like you and in the same phase, a level higher. We also read, as and when required. Please don’t get anxious when you don’t get results even after putting some strenous efforts. We all have gone through the same phase and have practised persistently.
If you have any suggestions for the course, you can definitely contact the Coursera team directly. But, please avoid putting any negative feedback on the platform to discourage other learners. Hope, you will understand my point of view.
Thanks and keep learning in good spirit!
Put the braces at the right places. They make a difference.
I already have knowledge on Python basics and have already completed deep learning courses in R. I do have a background. The assignments and weekly lectures have a huge gap and thats what I am highlighting.
I have shared my feedback already on coursera but didnt get any update on it from their end. I have been writing back and forth on this forum for last 7-10 days on this assignment without any progress.
Hi Talha,
Good to know that you have the knowledge of what is all required. Well, we all have been trying to help you throughout. What I can see here is that you are not able to share your file to any of the mentors because of some strict nomenclatures fixed under your company’s policy guidelines. My suggestion is that try sending the file to any of the mentors through someone else’s laptop/PC. We are not able to get any idea of what is happening in your notebook and so, it’s getting difficult for us as well. Only you can avert this situation for all of us.
You don’t need to return the type in order to see it. Just put print statements in propagate:
print(f"type(db) = {type(db)}")
print(f"type(dw) = {type(dw)}")
Nobu pointed to the problem earlier: you can see from the output you show that db is a 1 x 1 array and not a scalar. Notice the brackets around db in the output you showed:
That is why that assertion is failing. That must mean you used keepdims = True in your np.sum call to compute db.
Hi, I only get an assertion error for model_test(model). For all other functions before, all tests passed, so I don’t understand where the error comes from.
The error I get ist:
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)
131 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"
132 assert d[‘w’].shape == (X.shape[0], 1), f"Wrong shape for d[‘w’]. {d[‘w’].shape} != {(X.shape[0], 1)}"
→ 133 assert np.allclose(d[‘w’], expected_output[‘w’]), f"Wrong values for d[‘w’]. {d[‘w’]} != {expected_output[‘w’]}"
134
135 assert np.allclose(d[‘b’], expected_output[‘b’]), f"Wrong values for d[‘b’]. {d[‘b’]} != {expected_output[‘b’]}"
AssertionError: Wrong values for d[‘w’]. [[ 0.14449502]
[-0.1429235 ]
[-0.19867517]
[ 0.21265053]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]