Course 1, Week 2, Exercise 8, logistic_regression_model wrong even though all the tests passed

Hi to everyone,
I have this problem:

Even though I passed all the tests before, when I run the ‘logistic_regression_model’ cell it gives me the error: ‘ValueError: cannot reshape array of size 50 into shape (3,1)’

I passed the test with 100/100 anyways, but I really want to understand why there is this error.

Thank to everyone in advance,

That probably means that you are referencing global variables in your model function instead of the actual parameters that are passed in. You should only reference local variables in your functions, which includes the “formal parameters” declared in the function definition.

Thank you for the reply.
Sorry but I don’t understand the concept of ‘referencing global variables’ in this case. Even though I know what a global variable is. Can you please explain it a bit better? Maybe with an example.
I’m stuck on the same problem in the week 3, exercise 8, on the initialization of the parameters:

Well, that was just a suggestion of one common type of mistake that causes mismatching dimensions. That may not be the mistake you have made.

I checked and the shape of the X value in that test case is 2 x 3, so your W1 should be of shape 4 x 2. So why did it turn out to be 4 x 5? If your previous functions like initialize_parameters and forward_propagation etc passed the unit tests, then the bug must be in nn_model. Either you are passing the wrong values to the initialize routine. Try adding some print statements to show the shapes of your W1 and W2 values in the main loop before you call forward propagation.

Something is clearly wrong. Now your job is to figure out what it is.

Actually here’s a theory: maybe you hard-coded the return values from the layer_sizes function, instead of using the “shapes” of the input variables. If you just always return 5 for n_x, that still passes the test case in the notebook, but it’s a fundamental mistake and will cause havoc later when you actually use the layer_sizes function.

Note that your reply is about the Week 3 assignment. Sorry, I didn’t read you original post about the Week 2 assignment carefully enough: you are hard-coding the dimensions in your model function instead of using the dimensions of the input variables. You assume that there are 4 features and 3 samples. Also why do you reshape Y_test to be a column vector? That is not necessary.

Sorry mentor I think we are not understand eachother (or maybe I just don’t understand).
I’m talkin about the exercise 8 of the week 3 (nn_model).
What it asks is to create the parameters. Of course I created them with the function I created before:

I really don’t understand why it’s not working (even though of course the mistake is mine). That function needs 3 input, I give ‘(n_x, n_h, n_y)’. Where is the problem?

The error is:

You need to figure out why your W1 value ends up being 4 x 5 instead of 4 x 2. I have suggested several places to look for the bug: did you “hardcode” 5 as the first return value in layer_sizes? If not, then where did that value of 5 come from? It is nowhere to be found in the inputs for that test case. I already mentioned this in my first reply.

I suggest you put this code in nn_model after the call to initialize_parameters:

W1 = parameters['W1']
print(f"W1.shape = {W1.shape}")

What do you see?

Oh yes I hardcoded 5 in layer_sizes… What a mistake… Thank you a lot for the help!!!
I still have to look at what was wrong in the week 2, I will let you know also about it. Thank you!

It looks like you hardcoded several things there as well. Please see my earlier reply about that.