Week3: Programming excersise/Initialization

Hello,
for this exercise, we had:

The size of the input layer is: n_x = 5
The size of the hidden layer is: n_h = 4
The size of the output layer is: n_y = 2

Then in 4.2, we had to initialize the parameters. Here is my initialization:

{moderator edit - solution code removed}

which follows this geometry described in the code:
W1 – weight matrix of shape (n_h, n_x)
b1 – bias vector of shape (n_h, 1)
W2 – weight matrix of shape (n_y, n_h)
b2 – bias vector of shape (n_y, 1)

However, running the code I get this output:

W1 = [[-4.16757847e-03 -5.62668272e-04 -2.13619610e-02 1.64027081e-02
-1.79343559e-02]
[-8.41747366e-03 5.02881417e-03 -1.24528809e-02 -1.05795222e-02
-9.09007615e-03]
[ 5.51454045e-03 2.29220801e-02 4.15393930e-04 -1.11792545e-02
5.39058321e-03]
[-5.96159700e-03 -1.91304965e-04 1.17500122e-02 -7.47870949e-03
9.02525097e-05]]
b1 = [[0.]
[0.]
[0.]
[0.]]
W2 = [[-0.00878108 -0.00156434 0.0025657 -0.00988779]
[-0.00338822 -0.00236184 -0.00637655 -0.01187612]]
b2 = [[0.]
[0.]]

AssertionError Traceback (most recent call last)
in
8 print("b2 = " + str(parameters[“b2”]))
9
—> 10 initialize_parameters_test(initialize_parameters)

~/work/release/W3A1/public_tests.py in initialize_parameters_test(target)
52 assert type(parameters[“b2”]) == np.ndarray, f"Wrong type for b2. Expected: {np.ndarray}"
53
—> 54 assert parameters[“W1”].shape == expected_output[“W1”].shape, f"Wrong shape for W1."
55 assert parameters[“b1”].shape == expected_output[“b1”].shape, f"Wrong shape for b1."
56 assert parameters[“W2”].shape == expected_output[“W2”].shape, f"Wrong shape for W2."

AssertionError: Wrong shape for W1.



which is completely different from the expected output. I would appreciate any help to realize what it’s happening here.

You are hard-coding the dimensions. That’s a mistake. You should use the shapes that are actually passed in as the parameters here. It should work for any inputs, right?

The whole point of structuring the code around functions is that the code is “modular” and can be reused to solve many different problems, not just the specific problem we have in front of us.

If all we cared about was one problem and we were willing to write everything from scratch every time, then we wouldn’t need functions. The advantage of functions is that they take parameters which tell them what you want in a particular case, but the code works in different cases as well: you just call the function with different parameters.

Hi @paulinpaloalto. Many Thanks for the advise. I got the point here and also in my previous question. Problem solved!