Week 4 Assignment 1 Exercise 3.1 Initialize_parameters

Hi I’m not sure where my code is wrong. I’m using the same code to initialize W1, and it’s correct for W1, but not W2. The shape is also correct I think. But it’s not giving the correct W2 output using the random function. I wonder if there is a bug.

def initialize_parameters(n_x, n_h, n_y):

W1 = [[ 0.01624345 -0.00611756 -0.00528172]
[-0.01072969 0.00865408 -0.02301539]]
b1 = [[0.]
[0.]]
W2 = [[0.00419195 0.0068522 ]]
b2 = [[0.]]
Error: Wrong output for variable W2.
1 Tests passed
1 Tests failed

AssertionError: Not all tests were passed for initialize_parameters. Check your equations and avoid using global variables inside the function.

Expected output

``````W1 = [[ 0.01624345 -0.00611756 -0.00528172]
[-0.01072969  0.00865408 -0.02301539]]
b1 = [[0.]
[0.]]
W2 = [[ 0.01744812 -0.00761207]]
b2 = [[0.]]
``````

You may want to double-check your code used to initialize W2 and b2.

Thanks. It was just missing one letter

i got same failure result as yours. then i found i used “np.random.rand()” instead of “np.random.randn()” for W2. do you know why this letter “n” can cause this difference?

@Jia_Wei_Zhang, the instruction says,

**Use this random initialization for the weight matrices: `np.random.randn(shape)*0.01` with the correct shape

So, we probably are using the same formula for W1 and W2. To answer your question, ‘n’ designates the number of layers in the hidden layer and the output layer.

Same error as I had:). numpy.random.randn samples numbers from a standard normal distribution it seems. random.rand() samples from a uniform distribution between 0 and 1, so the numbers generated would be different.

1 Like