C1_week2 excercise 5(propagate)

after i wrote my cod on the propagte function and use the following command that you are inserted in the notebook to call function
w = np.array([[1.], [2]])
b = 1.5
X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
Y = np.array([[1, 1, 0]])
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)

the following error is appeared:
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:

i have the solution for this problem by chaging this command
<<assert type(grads[“db”]) == np.float64
by another one<< assert (db.dtype)==np.float64
but idon’t have a facility to change in this command because it is deactivated coursera are there any solution to skip this fault?

If the test fails, the solution is not to change the test. It is to understand what is wrong with your code that causes the test to fail. The failure of that assertion is telling you that the type of your db value is wrong. So the first question is “What type is it?” Try putting a print statement like this right after you compute db:

print(f"type(db) = {type(db)}")

What does that show? Why is it wrong?

Notice that the assertion is actually checking the corresponding entry in the grads dictionary, so the other possibility is that you inserted the wrong value in the dictionary.

when i type print(f"type(db) = {type(db)}")
the following answer is showen
type(db) = <class ‘numpy.ndarray’>

when i searched online i found there is difference between the type(variable) or dtype .variable
and the following code will show the difference
first case:
a=np.array([[1.3669,2.3,3],[5,6.23569,7.5]])
print(a.dtype)
assert (a.dtype)==np.float64
the result for th first case:
float64
and there is now error appeared

the second case:
a=np.array([[1.3669,2.3,3],[5,6.23569,7.5]])
print(a.dtype)
assert (type(a))==np.float64

the result for second case:
float64

AssertionError Traceback (most recent call last)
in
1 a=np.array([[1.3669,2.3,3],[5,6.23569,7.5]])
2 print(a.dtype)
----> 3 assert (type(a))==np.float64

AssertionError:

so this is what i talk about it the first case and second case?

the following link will show abreifing for what i mean:python - Difference between numpy.float and numpy.float64 - Stack Overflow

when i entered the following code

print(f"type(db) = {type(db)}")
print(f"dtype(db)={db.dtype}")

the answer is:

type(db) = <class ‘numpy.ndarray’>
dtype(db)=float64

so i want to change the from type(variable) to be variable.dtype to overcome of this problem.

The way to fix this is to figure out why db is an array instead of a scalar float. That is the problem, right? What is the math formula for db? It is this:

db = \displaystyle \frac {1}{m} \sum_{i = 1}^{m} ( a^{(i)} - y^{(i)} )

The problem is probably in how you computed the summation. It is the sum of the elementwise difference between two vectors. The difference is easy and then you use np.sum to add up the elements of the vector. That will give you a scalar unless you specify the argument keepdims = True.

Here are a couple of examples of using np.sum to add up vectors:

np.random.seed(42)
v = np.random.randn(1,4)
print(f"v = {v}")
vsum = np.sum(v)
print(f"vsum = {vsum}")
print(f"type(vsum) = {type(vsum)}")
vsumkeep = np.sum(v, keepdims = True)
print(f"vsumkeep = {vsumkeep}")
print(f"type(vsumkeep) = {type(vsumkeep)}")
v = [[ 0.49671415 -0.1382643   0.64768854  1.52302986]]
vsum = 2.5291682463487657
type(vsum) = <class 'numpy.float64'>
vsumkeep = [[2.52916825]]
type(vsumkeep) = <class 'numpy.ndarray'>

this is my code and i entered keepdims= true becasue i was missed to write it and i writ it now and running the code and the problem is still exist
dw=np.dot(X,(A-Y).T)/m
db=np.sum((A-Y),axis=1,keepdims=True)/m
print(db)
print(f"type(db) = {type(db)}")
print(f"dtype(db)={db.dtype}")

the same result:
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:

I don’t understand: the keepdims is the problem. That’s what I showed in my little code example. So why did you include that?

could you explain more please? you think the problem in the keepdims or what?

thank you.MR.PAULLINOPALOALTO i understood what you mean . you are allright the problem in the keepdims &and the axis so ideleted the keepdims and th axis included in the np.sum and running the code and all tested is passed thank you very much

1 Like