Course 1 week 2: bias 'b' value type in model vs initialize_with_zeros

I just solved it thanks to your advice!
Since there was no indication whatsoever in the assignment of having to define another function ‘initialize_with_zeros_test’, I did not do it, and that was the mistake. I thought it was a function defined in the source. So, I have defined the function and use the expected_output_b you mentioned in your previous post. In case this might be of any help to anybody.
I would suggest some indication of this step in future corrections of the assignment.
Thank you so much for your help!

@Enrique ,

Apologies for not being clear in the previous message.
We dont have to define initialize_with_zeros_test method. It is provided by team.

I was merely suggesting you to check the source code of the function to verify the cause of the error. Then based on the analysis, you can fix the actual issue.

So, please do not write your own initialize_with_zeros_test method

I was able to figure out how to overcome the problems both in initialize_with_zeros, and in the model. First one was just a typo in initialize_with_zeros. The problem in the model was not actually the argument used inside of the function initialize_with_zeros, but that I was not calling properly the train set during the propagation of the model. In case is of help to anybody. Thank you so much for your help!

1 Like

@Enrique ,
As posted in my pervious post, we should not define initialize_with_zeros_test.
Can you please add a new cell in your notebook and add the following 2 lines of code and share the actual output (the output will be code)

Hello @thiyanesh!

Thank you so much for your interest in this topic :smiley:
Sorry for my away from keyboard, I was offline over the weekend.

Just as you have pointed out in your post, given how the code and tests work, you need to change the w and b params inside def model to use the new values from params[w] and params[b], after the call to optimize (and thus also after the call initialize_with_zeros has been made):

I guess that might have been what was missing from the solution code when some of you encountered this particular data type error from running the tests.

If you read the comments carefully, it specifically specifies the following steps:

# (≈ 1 line of code)   
# initialize parameters with zeros 
# w, b = ...

#(≈ 1 line of code)
# Gradient descent 
# parameters, grads, costs = ...

# Retrieve parameters w and b from dictionary "parameters"
# w = ...
# b = ...

# Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = ...
# Y_prediction_train = ...

A solution could thus be built with the following lines:

w, b = 
parameters, grads, costs =
w =
b =
Y_prediction_test =
Y_prediction_train =

In that case, there is no need to update or change inside

d = {"costs": costs,
     "Y_prediction_test": Y_prediction_test, 
     "Y_prediction_train" : Y_prediction_train, 
     "w" : w, 
     "b" : b,
     "learning_rate" : learning_rate,
     "num_iterations": num_iterations}

Is it any clearer now @Enrique and @thiyanesh?

As to how to make sure you have the latest version, I recommend you to follow the steps in

I was able to finally figure it out, but thanks for the reply. It turns out that it was just a couple of small details and typos. Unfortunately, too much time spent in these little things. For the rest, I think I got it right. I appreciate your time and interest guys!

1 Like

I am glad to hear it worked out. It is usually the small details and typos that are the hardest to catch. Good luck on your Deep Learning journey :slight_smile:

@jonaslalin ,
Thank you for your time and explanation.
Yes, your explanation is clear and fully understandable.

Have a safe and great time ahead :slight_smile:

1 Like

I am glad to hear that @thiyanesh. Good luck with future exercises! :slight_smile:

Suki, Matlab programmer bollixed by complexity of python formats:

I’ve done the above
#w, b

and now receive error message that w is not defined
print ("w = " + str(w))
35 print ("b = " + str(b))
36 #print ("w shape: " + str(w.shape))

NameError: name ‘w’ is not defined
I’ve spent weeks on just initializing variables! Very frustrated

And, continuedm I tried this and still get w, b not defined:

#w, b

Hi @Nahammond,
I’m happy to help, but posting solutions on this forum isn’t permitted so please kindly refrain.
Take a look at this link for np.zeros()
Here, shape for w should be (dim, 1) as noted.
For b, it is simply a scaler. We need only one parameter as a bias. But note that it needs to be a float, not an interger.
Does that help?
Let me know.

@ suki
No, doesn’t help since I have done these things. 6 weeks just trying to initialize variables! I don’t want to quit. But I’m spending hours googling, searching

Now a new error that w and b are not defined
I submitted code hoping you could point out the error

Can you at least tell me sequence of statements:
do the numpy statements come inside the def statement
does the def return statement need to be
return or return w,b

Here is my definition of w and b, a new try:

The script isn’t even concerned with indentation of def statement. and the problem doesn’t include a return so I added it. I also rant it without a return statement

I also set outside of def proc:

Hours and hours go by. I try different variants

Does dim need to be initialized?

I get this error when I run the next routine. Does this help pin down the problem of why w and b are not defined?

TypeError: cannot unpack non-iterable NoneType object
Python returns this hint:
A common cause of this error is when you try to unpack values from a function that does not return a value. So the course statement “def initialize_with_zeros(dim):” is not returning value

That problem solved: The reason: I must have cut these 2 (3) lines out of the code:
dim = 2

But when I run the next routine I get assertion error wrt to b and cant seem to be able to type in np.float64(b):
w, b = initialize_with_zeros(dim)
----> 4 assert type(b) == float

Don’t give up! It’s okay. Totally understand it’s frustrating.
Check out this tutorial on how to define a function in Python.
Indentation is important in python. Python uses indentation to identify where the definition starts and ends.

def myFunc(x):
     return x+1

For example above function will take x as an argument and return x+1. The indentation before the return is necessary for this to work.

So in this function, dim is the argument the function takes as an input. No modification to this value is necessary.

For w, you don’t actually want this to be int. You can set it to float or just skip defining it since it’s default is to return zeros as float.

def initialize_with_zeros(dim):
     w = ....
     b = ....
     return w, b

You only have to fill in .... with np.zeros() with appropriate shape for w, i.e. (dim, 1), and a zero as a float for b.
Does it help?