W3_'Initialize Parameters'_test fails


I am having an issue with “Initialize Parameters” in week 3 assignment. I am getting the expected output but not passing all of the tests.

This is my code for the parameters.

W1 = np.random.randn(n_h, n_x) * 0.01
b1 = np.zeros((n_h, n_y))
W2 = np.random.randn(n_y, n_h) * 0.01
b2 = np.zeros((n_y, n_y))

Thank you.

Your code for b1 and b2 is incorrect. The second dimension should be hard-coded to be 1. The b values are column vectors by definition. You just happen to get lucky that n_y is 1 in that one particular test case, but there are other tests it is invoking from public_tests.py which don’t have n_y as 1.

In other words, just because you pass one test case is not good enough. You need to pass them all and there’s a reason why you don’t. For future reference, you can examine the other test cases by clicking “File → Open” and then opening the file name that I mentioned. Sometimes that is a useful debugging step to understand what values they are using and comparing those between the tests you pass and the ones you don’t.

Got it. I originally had them set to 1 and it still didn’t work. I finally figured out what was causing the problem.

Originally, I had b2 = np.zeros(1, 1))
Then, tried b2 = np.zeros(n_y, n_y)

Both of these are wrong. Correct answer was:

b2 = np.zeros(n_y, 1)

Thank you for your help!

Well, that’s close, but still not correct. The syntax of np.zeros is that it takes a single “tuple” as the argument, right? So if I want a 3 x 4 array of zeros, I would say this:

A = np.zeros((3,4))

See the difference between that and what you wrote above?

Note that when we invoke np.random.randn, it has different syntax. Why, you ask? The answer is “because”. Seriously. This is the beauty of Open Source Software: it’s free, but you have no guarantee of any conceptual consistency. The two functions were probably implemented by different people at different times with different things in mind. We just have to read the documentation and “deal with it”. :nerd_face:

Oh, sorry, I see you actually had that part of the syntax correct in the original code you showed. So that wasn’t the problem: it was just picking the correct dimensions.

1 Like

I am also having issues with the initializing of parameters. I believe my b2 is correct: np.zeros((n_y, 1)), but I get an error that something is wrong with my b2:

Any advice would be appreciated!


It’s not the b2 value that the error message is complaining about. Here are my output values from that test case:

W1 = [[-0.00416758 -0.00056267]
 [-0.02136196  0.01640271]
 [-0.01793436 -0.00841747]
 [ 0.00502881 -0.01245288]]
b1 = [[0.]
W2 = [[-0.01057952 -0.00909008  0.00551454  0.02292208]]
b2 = [[0.]]

Note that the b1 and b2 values agree, but the others do not. Also note that your values for W1 and W2 are all positive, whereas mine are both positive and negative. That probably indicates that you made the most common mistake on this section: using the wrong random function. You probably used “rand” instead of “randn”. Note that they literally wrote out the correct code for you in the instructions: you just have to fill in the dimensions.

this my output and it is as expected but it doesn’t work

The error message is telling you what is wrong: your W1 is the wrong shape. Note that there are two separate test cases: one you can see in the notebook and one that is in the file public_tests.py. Maybe you hard-coded the dimensions to match the test case in the notebook, instead of making the code general by using the actual parameter values that are passed to the function.

Take a look at the test case in public_tests.py and you’ll see that W1 for that test case should be 5 x 3. So put a print statement in your initialize routine that prints the shape of W1:

print(f"W1.shape {W1.shape}")

What do you see when that other test case runs?

1 Like

thanks I solved it

There is an important lesson to be learned here: it’s always a mistake to “hard-code” things unless they specifically tell you to do that. Our goal is always to write general purpose functions that can be reused with different dimensions. If you’re going to hard-code everything, then what is the point of making it a separate function, right?

I didn’t wrote it in this way W1 = np.random.randn(n_h,n_x) * 0.01 and this was the mistake

Yes of course , I have red this sentence " * Make sure your parameters’ sizes are right. Refer to the neural network figure above if needed." and I write the code according to the network figure

Sorry, I don’t understand. Your code produces the correct answers for the test case that you can see in the notebook.

Oh, well, as long as you pass now and I hope you understand the nature of what you did wrong, then we should just move on. There is plenty more to learn here … :nerd_face:

1 Like

Yes , sir