I got stuck at Exercise 5 - propagate of week 2 programming assignment. All of the values of expected outcomes are correct but only the data type cannot pass the check. See the error below. How to solve this?
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:
Maybe you are using Python’s sum
function in your db
. Use Numpy’s sum like np.sum
.
Best,
Saif.
If Saif’s suggestion doesn’t solve the problem for you, then you need to step back and debug it. Any time you have a type mismatch, then the first question to answer is “Ok, what type is it?” Then the next question will be “How did it get that way?” Try adding a print statement in your logic right after you compute db:
print(f"type(db) = {type(db)}")
If it says that it is a numpy array, then the question is what shape is it? If it’s a 1 x 1 array, then you can easily convert it to a scalar float, which is what that assertion is checking for.
The following 3 print statements are used
print(f"type(db) = {type(db)}")
print(db)
print(db.shape)
and get the results:
type(db) = <class ‘numpy.ndarray’>
[-0.12500405]
(1,)
And then, db = float(db) is used but the issue is still not solved.
My output for the same print statement is:
Type(db) = <class 'numpy.float64'>
Shape db: ()
db: -0.12500404500439652
So, your type is numpy.ndarray
which is not the same as numpy.float64
and my shape of db
is () which means it is a scalar but yours is (1,) which means it is a vector. So, as Paul said, “How did it get that way?”.
In the calculation of db
, we use A, Y, and m
. Y and m
are given to us, so, there may be something wrong in your implementation of db
and/or A
. And, if there is something wrong with A
, it will also effect the dw
.
Add this statement in your code:
print(f"Type(dw) = {type(dw)}")
print(f'dw: {dw}')
My output is:
Type(dw) = <class 'numpy.ndarray'>
dw: [[ 0.25071532]
[-0.06604096]]
If your output is same as mine, then it means there is something wrong in your implementation of db
. However, if your output is different, then you have to check the implementation of A
.
Best,
Saif.
The value of your answer looks correct, but it’s a 1D array with 1 element and it should be a scalar, since b is a scalar. If you used np.sum
to do the required summation, are you sure that you did not use the keepdims
or axis
arguments? If you omit those then the result will be a scalar.
Here’s a demonstration of the effect using these arguments has:
A = np.random.randn(1,4)
print(f"type(A) {type(A)}")
print(f"A.shape = {A.shape}")
print(f"A = {A}")
sumA = np.sum(A, axis = 1, keepdims = True)
print(f"type(sumA) {type(sumA)}")
print(f"sumA.shape = {sumA.shape}")
print(f"sumA = {sumA}")
sumA = np.sum(A, axis = 1)
print(f"type(sumA) {type(sumA)}")
print(f"sumA.shape = {sumA.shape}")
print(f"sumA = {sumA}")
sumA = np.sum(A)
print(f"type(sumA) {type(sumA)}")
print(f"sumA = {sumA}")
Running the above gives this result:
type(A) <class 'numpy.ndarray'>
A.shape = (1, 4)
A = [[ 0.24016688 -0.92565357 -0.43283935 0.28682346]]
type(sumA) <class 'numpy.ndarray'>
sumA.shape = (1, 1)
sumA = [[-0.83150259]]
type(sumA) <class 'numpy.ndarray'>
sumA.shape = (1,)
sumA = [-0.83150259]
type(sumA) <class 'numpy.float64'>
sumA = -0.8315025876596674
So you can see that the value of the sum turns out the same of course, but the type and shape are controlled by the axis
and keepdims
arguments. It looks like you must have used the axis = 1
and no keepdims
combination. If you omit all the keyword arguments, you’ll get what you need here.
Of course it’s always a good idea when you see a function for the first time to read the documentation. By googling “numpy sum” you get this page.