Confusing output: AssertionError on Exercise 2 - layer_sizes

The expected output of this exercise is:

Expected output

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
All tests passed!

My code outputs these values (included below), but I get this error with different expected values:

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
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-23-a625d2d64083> in <module>
      5 print("The size of the output layer is: n_y = " + str(n_y))
      6 
----> 7 layer_sizes_test(layer_sizes)

~/work/release/W3A1/public_tests.py in layer_sizes_test(target)
     23 
     24     assert type(output) == tuple, "Output must be a tuple"
---> 25     assert output == expected_output, f"Wrong result. Expected {expected_output} got {output}"
     26 
     27     print("\033[92mAll tests passed!")

AssertionError: Wrong result. Expected (7, 4, 5) got (5, 4, 2)

Hi,

A test is being run where the tester passes you

    X -- input dataset of shape (input size, number of examples)
    Y -- labels of shape (output size, number of examples)

While the size of the hidden layer is always 4, you have to deduce the size of the input and output layers from X and Y. It’s not always 5 and 2.

2 Likes

There is more than one test case here. It looks like you are “hard-coding” the answers, instead of deriving them from the shapes of the inputs, as David suggests. “Hard-coding” is always a mistake, unless they specifically tell you to do that. We are trying to write general code here which works with any inputs.

2 Likes

Thanks for the quick replies, actually I’m not hard-coding them (except n_h, as stated in the text). I believe I can’t send more details about my solution, but what confused me is the text below the test results saying the expected outputs should be 5, 4, 2 and that seems what I’ve got with my answer. I’ll reset the notebook and try again.

I just changed the variable, If I may write that, and the accepted answer was the following:

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
All tests passed!

Just to leave here as more people can have similar questions.

It sounds like the bug was not literal “hard-coding”, but that you must have been referencing a global variable instead of the actual parameter(s) passed to the function.

1 Like