W2 Assignment: model() has a dimension problem

Hi! I am doing the Week 2 assignment for the first course. And I met with a problem when doing the model() part. Here is a screenshot of the error:

Yesterday I posted the same question on Coursera Discussion. I got a suggestion that I can just restart the kernel. I tried but it still failed.
I passed all tests before getting to this part. And the report of this error is confusing for me. Can anyone tell me how to do the debug? Thank you!

The mistake is that the w value that you are passing down to propagate is wrong there: it is 2 x 3, but it should be 4 x 1 so that w^T ends up being 1 x 4. So how did that happen? If all your other routines pass the test cases, then the bug must be in model. Print the size of w after the call to the initialize routine. If that is right, then your code is going off the rails in some other place. What is the shape in optimize?

Thank you! I have figured it out. I put the wrong X in the mode.

Hello!Have the same issue. What do you mean by “I put the wrong X in the model”?

Hi! In my codes, I wrote X rather than X_train and it caused the problem. You can check your code and make sure that you use correct input everywhere.

Yeah I missed one and it did not work till I noticed
w, b =initialize_with_zeros(dim)
here you should use X_train too.

I have the same problem having no idea what dimensions should I pass to initialize_with_zeros(dim) function. I tried with X_train.shape[0] but got IndexError: tuple index out of range. When trying with hardcoded values it failed too.
Any suggestions? @paulinpaloalto

Using the first element of X_train.shape should be the correct answer. You must have made some sort of syntactic error in how you specified that.

I don’t understand what do you mean by : “how you specified that”. I am passing X_train.shape[0]
to i_w_z() function. The error I am getting is (the last part):

in predict(w, b, X)
14 ‘’’
—> 16 m = X.shape[1]
17 Y_prediction = np.zeros((1, m))
18 w = w.reshape(X.shape[0], 1)

IndexError: tuple index out of range

But prediction function, and all the others passed tests and gave correct outputs.
Regards, Andrzej.

Ok, so that must mean that the X value there is not what it should be. Try adding these lines to your predict function:

print(f"type(X) = {type(X)}")
print(f"X.shape = {X.shape}")

It should be a numpy array and the shape should be a 2 tuple, but what do you actually see?

In other words, the bug is not in predict: the bug is that you are passing incorrect arguments when you call that from model. At least that’s the theory … Let’s see what the evidence shows.

Gathering evidences I got

type(X) = <class ‘numpy.ndarray’>
X.shape = (2, 3)

but after run predict_test(predict) it changes to

type(X) = <class ‘numpy.ndarray’>
X.shape = (3, 3)

I don’t want to feel fired with thinking by your help so I need a bit more time to rethink this problem. Perhaps there is something wrong with clause I put to zero the w variable because I see later in the code (before computing hypothesis A), that w is reshaped with .reshape(X.shape[0],1). Truly saying I don’t know in 100% why. Before it the shape of w is ((2,1), after that it is (3,1). Is that correct?

Yes, those are the correct shapes of w to match the X shapes that you show.

But the question is what you see when you call predict from model, right? The problem is not in predict: it is that there is something wrong with the parameters you are passing from model to predict.

It is a general principle of debugging that a perfectly correct function can still throw errors if you pass it bad parameters. So you start from the point of the error and then you have to work backwards to figure out where the problem really is. It may be higher up the call stack …

Note that predict does not change any of its input values, right? So if there is something wrong with X, that is not the fault of the code in predict.

And it wasn’t. :smiley:
Is was as silly typo as the misunderstanding with bias variable. I put Y in place of X as a parameter to predict()
Thank You very much!

It’s great to hear that you found the answer. Onward! :nerd_face:

1 Like

Although now that I think epsilon more about it, I don’t understand why passing Y would cause the particular error about “index out of range”. Y is also a 2D array, right? Just of dimensions 1 x m, instead of n_x x m.

But if you solved it, maybe that’s good enough and we should just move on …

1 Like

You’re right. Understanding such errors, the curiosity, is the thing that moves us up and forward.
But today I had a hard day and I am tired enough, maybe later we’ll talk about it, ok?

Sure, no worries. It’s fine to just let it go. Sorry, but when I get in “debug mode” the whole point is you need to exactly understand what the error says and then how to map that back to what we’ve done wrong. That’s the skill we are developing here.

Talk to you tomorrow!


1 Like

Hi Paul,
Today I have had a bit of time at work :wink: and made some calculations in a python console, step by step going from w initialized to zeros counting loss, A and cost by hand for simple array of X.
Until now I did not feel how it is possible to find the right values starting from zero :D. But when I got in one of outputs an array of [0.5 0.5 0.5] all is happened clear. Sigmoid rulez! Zeros have its value :smiley: And the minus sign of the derivative shows the direction of the gradient.
My faults from yesterday ware caused by lack of time and by reading without the understanding. All the answers are either in comments to the code or near to it. One have only to read carefully.
Cheers, Andrzej.

Hi, Andrzej.

Thanks for your description of your thought process! Yes, it is a valuable lesson that the instructions in the notebooks are generally very thorough and clear. It’s always worth reading them carefully. Sometimes it almost feels like they go too far and it ends up seeming like “taking dictation” as opposed to real programming. :laughing:

There’s plenty more interesting material to come. Onward! :nerd_face:


1 Like