C1_w3 assignment question 3

Hey so, I answered question 3 and got to the following part:

Note:

Actual values are not checked here in the unit tests (due to random initialization).

w3_unittest.test_initialize_parameters(initialize_parameters)

So does this mean that I have to change my results in order to match the expected output?
Because afterwards I get the following:

AssertionError Traceback (most recent call last)
in
1 # Note:
2 # Actual values are not checked here in the unit tests (due to random initialization).
----> 3 w3_unittest.test_initialize_parameters(initialize_parameters)

~/work/w3_unittest.py in test_initialize_parameters(target_initialize_parameters)
185
186 for test_case in test_cases:
→ 187 result = target_initialize_parameters(test_case[“input”][“n_x”], test_case[“input”][“n_y”])
188
189 try:

in initialize_parameters(n_x, n_y)
14 ### END CODE HERE ###
15
—> 16 assert (W.shape == (1, 1))
17 assert (b.shape == (1, 1))
18

AssertionError:

So I don’t know if I am right or if I am wrong. Thanks <3

1 Like

Hi, Janina. I’m not a mentor for M4ML, but notice what that assertion is doing: it is checking the shape of your W value, not the actual numeric values. It should be a 1 x 1 numpy array, but apparently it isn’t. That is what that failure message is telling you. So what shape is it? You can add a print statement to your function to find out:

print(f"W.shape = {W.shape}")

What do you see when you do that? Then the question is why did it not turn out to be 1 x 1? There must be something wrong with the way you specified the dimensions to the random function.

1 Like


I also have an error in this exercise. Can you please give a hint what i did wrong
Thank you

1 Like

Just like the previous question on this thread, that assertion is telling you that the shape of the W value that your code produced is not correct. The shape of a numpy vector or matrix or array is the number of dimensions it has and the sizes of those various dimensions. So the first question is “what shape is it actually?” I showed how to figure that out in my previous reply. Then the next question is “why did it turn out incorrectly?” Once you see what the incorrect shape is, that should be a pretty good clue as to where your mistake is. Most likely you are passing the wrong arguments to the “random” function that generates that values.

2 Likes



I don’t know why I’m getting errors even though the W.shape is (1,1). Can anyone Elaborate

1 Like

Pay attention to the order of args in np.random.randn() method (check the order in the comment and then in your implementation :))

5 Likes

I see that. Thanks for the tip :smiley:

1 Like

Hello folks,

@paulinpaloalto: I have checked my matrix size and it is correct. W is a 1x1 matrix.

@Maryia_Znak: I have also checked the use of arguments in the function but still I am getting the assertion error messagemwhen running the test unit code block.

Please find below how the function is written.

{moderator edit - solution code removed}

1 Like

Please read the error trace more carefully. In your case it is not the W value that has the incorrect shape: it is the b value. The assertion about the shape of W did not fail and the little arrow on the exception trace points at the assertion about the shape of b, right? Once you realize that, it’s also easy to find the bug. :nerd_face:

2 Likes

Thanks, Paul. I managed to make it work earlier yesterday but still don’t fully comprehend why “1” works and why “n_x” (which equals to 1) would not work.

1 Like

The point is that we are writing general code here, which should work for any input values. Just because n_x happens to be 1 in the one test case that you can see does not mean it will always be one, right? There are multiple test cases in the unit test for this function and some of them must use a different value of n_x. So you are making an invalid assumption: an assumption that does not generalize.

2 Likes

Paul - that answers my question. Thank you for the clarification. Appreciate the support :handshake::handshake:

1 Like

Maryia_Znak
I had the same problem.
I read your comment.
I fixed it.
Thank you so much!
Alison

1 Like

This comment helped fix my code.
Thank you.
I am taking the google crash course in Python next on your recommendation.
Thank you.
Alison
M4ML C1_W3

1 Like