Course 1, Week 2, Exercise 4 - initialize_with_zeros

I feel like this is a simple question I should be able to figure out, but I’m struggling.
For the question mentioned in the title of my question, I write the following:

{moderator edit - solution code removed}

When, in this code block, I run the function

initialize_with_zeros(2)

I am returned
image

But when I run the function in the test block, I get

Would very much appreciate an explanation of the problem and a solution. Thanks!

1 Like

Hi @Nicklaus_Millican ,

You have hard coded the shape of the w to be (2,1) which is incorrect. It should be (dim,1) because this function should return whatever dim of the w array is required.

Ah, thank you!

Does this mean I should use w = np.array(np.zeros(dim)).reshape(dim,1)?

Hi @Nicklaus_Millican ,

There is no need to do a reshape. What is needed is to specify the shape of the array you want when calling np.zeros(). So it would look like this:

np.zeros((dim,1))

If you read the comments for this function, you would see what this function should do, and what the output parameters are expected to be.

I am facing assertion error although I declared w=np.zeros((dim,1)). Any Suggestions

Same here.

[Moderator edit: Code removed.]

This what I have used.

But I am getting this error:
AssertionError Traceback (most recent call last)
in
2 w, b = initialize_with_zeros(dim)
3
----> 4 assert type(b) == float
5 print ("w = " + str(w))
6 print ("b = " + str(b))

AssertionError:

Hi, @Shruthi_Ananthram. The unit test tests (among other things) that the parameter b is of the correct type. In this case, it should be of type float. You have most likely coded it as an integer. For example, the statement x = 3 will result in object x being of type int. The simplest way to produce the type float is as follows: x = 3. including the decimal point after the number. The Python built-in function float also works, e.g. x = float(3), but why waste the keystrokes?

To check if an object is of the proper type, use the Python function type in a print statement. E.g., print(type(x)). Or for something a bit more elaborate, use an f-string in the print statement, such as print(f'x is of type {type(x)}').

Note: Please do not post your solution code. It is a violation of the honor code.

Thank you, kenb.
That was the error and its fixed now.