Attribution errors for dw, db Course 1 assignment 2call to propagate routine

@suki I keep getting attribution errors in propagate for dw and db

The first error I fixed:
AttributeError Traceback (most recent call last)
in
3 X =np.array([[1., 2., -1.], [3., 4., -3.2]])
4 Y = np.array([[1, 0, 1]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

Looks like I corrected dw error by statement:
dw=np.ndarray(shape=(2,1), dtype=float, order=‘F’)

Now db is the problem even after I added db=np.float64:
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:

Hi, Nancy.

You posted this against Course 2, but I’m guessing that you are actually talking about the Logistic Regression exercise in Course 1 Week 2. The first question is what the type of your db value actually is. Put the following print statement in your propagate routine right after you compute db:

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

When I run the test cell for propagate with that print statement in place, here’s what I get:

type(db) = <class 'numpy.float64'>

If you get something different than that, then the question is why?

@paulinpaloalto Thank you Paul. I also get type(b) =float64 in a print statement.

I experimented declaring type both before and after assignment of variable. Which should I have done:
type(a) = float64
a =0

or reverse order?

I also have a print statement of type(b). So it is float64.

Since propogate ran before without the need to specify type, should I be looking for errors proceeding this line? Did this happen because I reran this for 4 hours last night?
The instructions say enter 2 lines of code. And an exercise on gitlab does not specify type.
Also since dw was declared in np.array, why the need to declare as np.ndarray?

When I start on this again today, I will reerun everything from the beginning.

Thank you, again

You have to understand how assignment statements work in python. Every time you make an assignment, the variable name points to a new object in memory. Try this and watch what happens:

a = 1.
print(f"type(a) = {type(a)}")
a = 1
print(f"type(a) = {type(a)}")

Do you see the subtle difference between those two assignment statements? Do you understand why the types came out the way they did? The not so subtle point being that what the type of a was before the assignment statement is immaterial to what it is after the assignment statement. The point of an assignment statement is that it changes things, right?

I think you are making this way more complicated than it needs to be. Look at the exception trace in your original post on this thread. It is an assertion which fails because db is not of type np.float64, right? So what type is db in your code? How did it get that way? Just put this print statement in your code after you compute db:

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

What do you get?

@paulinpaloalto
db becomes nd.array

db = np.sum((A - Y), axis=1, dtype=float,keepdims=True) / m
print(f"type(db) = {type(db)}")

Answer:
type(db) = <class ‘numpy.ndarray’>

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

also tried with same error:
db=0.0
db=np.float64
db = np.sum((A - Y))

Very frustrating. Apologies for printing code
also tried in np.sum, dtype=np.float64

The answer is easy: just remove the axis and keepdims arguments on the np.sum call that computes db. Since b is a scalar, you want db to be a scalar as well. That is the general rule: the gradient of an object must have the same type and “shape” as the base object. Of course “shape” only applies when the base object is an nd.array, not to scalars.

Tried 3 different ways, result still is db is type ndarray

trial 1) db = np.sum((A - Y), dtype=np.float64, keepdims=True)/m

type(db) = <class 'numpy.ndarray'>

@paulinpaloalto

Tried 3 different ways, result still is db is type ndarray

trial 1) db = np.sum((A - Y), dtype=np.float64, keepdims=True)/m
type(db) = <class ‘numpy.ndarray’>
AssertionError
7 assert type(grads[“dw”]) == np.ndarray
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64

trial 2) db = np.sum((A - Y), keepdims=True)/m
type(db) = <class ‘numpy.ndarray’>

AssertionError
7 assert type(grads[“dw”]) == np.ndarray
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64

trial 3)
db = np.float64
db = np.sum((A - Y))/m
type(db) = <class ‘numpy.ndarray’>

AssertionError
7 assert type(grads[“dw”]) == np.ndarray
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64

What do you mean 3 different ways? Did you actually read what I just said in my previous reply? Just remove the axis and keepdims arguments. That is what is causing you the problem.

@paulinpaloalto

Long time Matlab coder where you explictly define dimensions of arrays. Just learning Python.

Here is my code which works just fine:

db = np.sum(A - Y)/m

One simple line. That looks equivalent to your “trial 3”. I think if your “trial 3” didn’t work, then it just means there is something flawed about your experimental methodology. You do realize that just typing in new code doesn’t actually do anything, right? You have to actually run that cell with “Shift-Enter”. Just calling the function again after typing does nothing: it just runs the old code. It’s very easy to construct an example and prove this to yourself.

@paulinpaloalto
Worked, but the sigmoid statement is wrong:

ValueError: invalid array_struct
→ 55 A= sigmoid(np.dot(w.T, X) + b)

I redid without b in sigmoid function:
A= sigmoid(np.dot(w.T, X)
Wrong values for grads[‘dw’]. [[15049.85471226]
[30465.32023283]] != [[0.99845601]
[2.39507239]]

So how would one go about debugging that? It’s the same in python as it is in MATLAB, right? The type of one of the variables referenced in that statement is incorrect. There should be no array_structs anywhere to be seen here. I don’t even know what that is in python, so I’ll have to go look it up to get an idea of what you might have done wrong.

So either use a debugger and set a breakpoint on that line and examine the types or put in print statements right before the failing line:

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

And so forth.

But based on your last statement, it sounds like b is the culprit. It should be a python scalar float value. So why is it an array_struct instead? Whatever that is …

@paulinpaloalto
No, there is no need to declare type in Matlab. And the specification of the arrary defines the ‘shape’ or dimensions

**So a function f(a,b) cannot be specified in Python as **
f(a+b)
So I tried:
FROM PYTHON>ORG
variable = int(var1)+int(var2)
print("The sum of variables ", variable)

So I tried:
** z= float(np.dot(w.T, X))+float(b)**

Result:
58 z= float(np.dot(w.T, X))+float(b)
** 59 #np.sum(np.dot(w.T, X),b)**
** 60 print(f"type(z) = {type(z)}")**

TypeError: only size-1 arrays can be converted to Python scalars
z= np.sum(np.dot(w.T, X),b)
57 A= sigmoid(z)
So how to add b to np.dot(w.T, X)
is the problem

ipython-input-35-cd1bfebaa935> in propagate(w, b, X, Y)
54 b=np.float
55 print(f"type(b) = {type(b)}")
—> 56 A= sigmoid((np.dot(w.T, X))+b)
57 cost=(-np.dot(Y,np.log(Y))-np.dot((1-Y),np.log(1-Y)))/m
58 #something = np.multiply(Y, np.log(A)) + np.multiply((1 - Y), np.log(1 - A))

TypeError: unsupported operand type(s) for +: ‘float’ and 'type’

Please refer me to where in any python manual or tutorial it tells you that saying:

b = np.float

is a useful thing to do? It sets b to be a variable of class “type”, which may be useful in some contexts, but not here. The way you set b to be a scalar float in python is to assign it a value which happens to be … wait for it … a scalar float. Like this:

b = 42.

Note that this statement has an entirely different effect:

b = 42

See the difference? This is actually one significant difference between MATLAB and python. In MATLAB, the two statements above would have the same effect. They don’t in python. Watch this:

b = np.float
print(f"type(b) = {type(b)}")
b = 42.
print(f"type(b) = {type(b)}")
b = 42
print(f"type(b) = {type(b)}")

That gives this result:

type(b) = <class 'type'>
type(b) = <class 'float'>
type(b) = <class 'int'>

The reason is that in MATLAB the default type of a scalar is float. In python it is integer.

The larger point being that we start out by initializing b like this:

b = 0.0

So it starts as a scalar float. And then we update it on each iteration by saying:

b = b - learning_rate * db

where db is also a scalar float. So the RHS of that assignment statement has type “scalar float”, so that means that the LHS will have that type after the assignment as well.

So this is how it’s all supposed to work. Why is it going off the rails in your implementation?

This worked:
b=0.0
print(f"type(b) = {type(b)}")
z=0.0
** a=np.dot(w.T, X)**
** z=a+b**
** print(f"type(z) = {type(z)}")**
** A= sigmoid(z)**

Now I’m ok, just getting the wrong numbers for dw and db. Which I can take from here

type(b) = <class ‘float’>
type(z) = <class ‘numpy.ndarray’>
type(db) = <class ‘numpy.float64’>
dw = [[15049.85471226]
[30465.32023283]]
db = 7707.033188162647

Thank you Paul, very much for your help

Yikes! Those values are quite a bit off. What are your A values? I added a print statement to my code and here’s what I get:

A = [[0.99987661 0.99999386 0.00449627]]
dw = [[0.99845601]
 [2.39507239]]
db = 0.001455578136784208
cost = 5.801545319394553
A = [[0.99987661 0.99999386 0.00449627]]
All tests passed!

@paulinpaloalto
Huge indeed!
A = [[8.10308393e+03 1.62754791e+05 4.51658094e-03]]

Thank you for your values and running this. I placed as comments in my code to work on tomorrow.

So maybe it’s the sigmoid function
or a = np.dot(w.T,X)
I’ll check tomorrow: w, X, a
Also maybe the normalization didn’t work

There is clearly something wrong with your sigmoid. The outputs of sigmoid are between 0 and 1 by definition, right? Are you sure your sigmoid code passes the tests in the notebook?