C1 W3 Assignment re layer_sizes(X,Y)

With regard to the “def layer_sizes(X,Y)”, my code returns the “expected output”, but flunks the w3 unittest. I cant see why?

The size of the input layer is: n_x = 1 The size of the output layer is: n_y = 1

Expected Output
The size of the input layer is: n_x = 1
The size of the output layer is: n_y = 1

In [30]:

w3_unittest.test_layer_sizes(layer_sizes)

Wrong size of the input layer n_x for the test case, where array X has a shape (5, 100). Expected: 5. Got: 1. Wrong size of the output layer n_y for the test case, where array Y has a shape (3, 100). Expected: 3. Got: 1. 2 Tests passed 2 Tests failed

2 Likes

You must have hard-coded the values to match that one test case. We are trying to write general code here which will work for any input values. That doesn’t work if you hard-code the answers to match one particular test case. As you can see from the error, the grader uses a test case with 5 input features and 3 outputs. It is always a mistake to hard-code anything, unless you literally have no other choice and they specifically tell you do to that as in the case of n_h here. But for n_x and n_y, you can derive those from the shapes of the input values, right?

1 Like

Thank you for your response. But I didn’t hard code it. This is my code. Since it’s wrong, I guess its ok to write it here:
n_x = shape_X[0]
n_y = shape_Y[0]

1 Like

Well, that counts as “hard-coding” because the variables shape_X and shape_Y are not defined within the scope of the layer_sizes function, right? Referencing global variables is just another form of hard-coding.

Note that this is not a beginning python course. If the notion of the “scope” of a variable is not a familiar concept to you, then it might be a good idea to google “python scoping model” and do a bit of reading.

1 Like

Well, I’m not the most experienced coder you know, but I do understand scope.
The hint says: Use shapes of X and Y to find n_x and n_y: So I do need a little hand-holding here. The rest of the assignment is kind of fun, but I cant finish it without this, apparently simple for most, step.

1 Like

Well, look at the earlier code that sets the values of shape_X and shape_Y: how does it do that? It uses the “shape” attribute of the relevant variables, right? So you need to repeat that logic within the local scope of the layer_sizes function using the actual input parameters and using their “shape” attributes.

2 Likes

Got it. So, X.shape works and shape_X doesn’t. The was too subtle for me. With that change, the rest of assignment runs perfectly. Thank you.

2 Likes

You’re worrying me with this comment, especially given that you mentioned a couple of posts ago that you are familiar with the concept of scope. What is subtle about where a variable is actually defined? That’s the point about “scope” and “global”: shape_X is just a variable and is not defined within the body of the layer_sizes function, right? So it has literally nothing to do with what is happening there. But X is a parameter to the layer_sizes function and X.shape is an “attribute” of the variable X which is local to the scope of layer_sizes. Scope is critical and not something you can gloss over.

1 Like

Hi Michael,

if you haven’t already solved this, the problem is not the scope of a variable, but a matrix multiplication in the forward_propagation function. You are probably using W*X when in fact you need to use W@X (dot product). I had the same problem. I think the instructions are a bit misleading - Compute Z multiplying arrays w , X and adding vector b.

Hope that helps!

Ivan

1 Like

This thread is about the layer_sizes function, but it’s good to hear that you figured out your issues with forward_propagation. Of course the point is that there are two types of matrix multiplication: elementwise and dot product, so you need to understand the context in order to make the correct choice. Here’s a thread which talks about that distinction in more detail.

1 Like

Hi paulinpaloalto,

My bad - I got the same error in the forward_propagation function and was opening topics to see if I can find something useful. Somehow I mixed up topics and thought that the Michael’s error was raised in the same function.

Regards,

Ivan

1 Like

Do you have any idea why the original error message says nothing suggesting that its due to hard coding? i wouldnt be able to guess from this. not that i got it wrong, the error message isnt even remotely intuitive at all.

Wrong size of the input layer n_x for the test case, where array X has a shape (5, 100). Expected: 5. Got: 1. Wrong size of the output layer n_y for the test case, where array Y has a shape (3, 100). Expected: 3. Got: 1. 2 Tests passed 2 Tests failed

The test just tells you what the expected values are and what the incorrect values are that your code generates. From that information, you need to debug the problem. I was the one who suggested that the problem was referencing global variables, because I have experience helping people debug problems in this assignment.

It’s not reasonable to expect the error messages from the tests to do the debugging for you. There can be lots of reasons for wrong answers.