W2_A2_Ex-8_Merging functions into a model

I’m having trouble meeting the criteria for this exercise. The first thing I did was initialize my parameters w and b with shapes X_train.shape and X_train.shape[1] respectively. I then defined m as X_train.shape[1] and created a for loop to iterate though the number of iterations in which I defined A, Cost, dw, db, and then updated my parameters w and b. Lastly, I redefined the variable A with respect to the X_test dataset and ran them though the for loop shown in the screen shot below. This was taken from code written by the course in the ‘predict’ function.

I’m don’t think this is the right implementation that the grader is looking for because it threw an error shown below. I don’t know how to include the variables named ‘Y_prediction_test’ and ‘Y_prediction_train’ into my code. Additionally, I’m getting predictions that are all ‘ones’ which doesn’t sounds accurate.

Any hep will be greatly appreciated. Thanks!

Does your code return the correct variables?

Did you run all of the cells every time you opened the notebook?

Yes, I restart the runtime every time. Yes, as far as I know, it returns everything as it should but I don’t know how to include the variables y_prediction_train and y_prediction _test.

HI @Jeb_Dean,

Looking at the solution skeleton,


I have a feeling that you were not quite doing things as instructed.

You said in the title that the exercise was to “merge all functions into a model”. Did you actually use one of your previously implemented function for the task of initializing w and b? If you did, you would not have done this step “respectively”.

The solution skeleton expects you to finish this in one line because you are expected to reuse previously implemented functions, but apparently you didn’t.

I know I am not addressing your error message directly, but for the sake of the assignment, I recommend you to follow the skeleton by reusing previously implemented functions first, which will basically remove much of your existing code by replacing them with those previous functions. After that, if you still encounter any error, please open a new thread with the latest full error traceback.


Hey Raymond,

I didn’t use any of the previously created functions because it was easier for me to comprehend. To initializing w and b I used w = X_train.shape and b = X_train.shape[1]

I don’t understand what some of the instructions were asking. It seems like the only function that is useful would be optimize() to return params, grads, and costs.

Lastly I don’t know what I would set ‘Y_prediction_test’ and ‘Y_prediction_train’ too. What are they supposed to represent? We don’t make predictions on training data do we? Y_prediction_tests seems like it should be a (1,m) dimensional vector of our predictions but I don’t understand Y_Prediction_train.

Sorry for so many questions

Hello @Jeb_Dean,

Names of those functions previously implemented by you as part of the work in that assignment are pretty suggestive on what they are doing. I am sure you can find the one that is related to initializing parameters. Feel free to go to the top of the assignment where you will find a table of content with the names of the functions.

It might be easier for you to do it the way you prefer, but reusing functions created previously by you is also a challenge that the assignment is asking you to take on. After passing the assignment by following the instructions, you are free to reimplement the function in any way you prefer, but the pro is that, you will have a passed assignment as your reference that you can make use of to cross check if any change that you will make (to the way you prefer) is able to produce the same and expected result. I recommend you to reuse previously created functions.

I would refer you back to the assignment for one of its function previuosly created by you on how we need to initialize w and b. The best would be for you to reuse that function, or you initialize them the same way that function does.

As you said, we make predictions on training data. You can see immediately how we are going to use Y_prediction_train:

Can you tell how we are going to use it? We need it to compute the training accuracy and if you click “Ctrl + F” and search for “training accuracy”, you will find two occurences that potentially are discussing what it can represent.

Finally, it is a very very common practice in general programming that we break down a long program into segments, and implement each segment as a function. This increases the readability of code and reusability of code and more. I once again suggest you to reuse existing functions created by you for completing the assignment. After completing the assignment, make any change you would like to experiment with.


OK I think i’m on a better track now, thank you.

I didn’t write the code w.reshape(X.shape[0], 1) and it’s spitting the following;

The dimensions of Y_test is (1,3)
so m would become 3 ,
y_prediction is of shape (1,3),
and w is being reshaped to (1,1), and
the initialized shape of w is (4,1)

So the error makes sense. The only thing I really have the power to change is w. I may have initialized w and b wrong. I feel like w should be of shape (4,7) (The shape of the training data) but that wouldn’t solve the error.
What are your thoughts?

Hello @Jeb_Dean,

It’s great that you were keeping track of the shapes. When we speak of the expected shape for w, the one thing that we can never miss out is the shape of x. You might go over the lectures again but I am going to just share here, that for this example (not in general), the shape of w should be

(number_of_features_in_X, 1)

This actually has also been stated in the DocString of exercise 4 (please check).

Therefore, I suggest you to print the shapes along the steps in the model function. On initialization of w, its shape should be (something, 1) where that something is the number of features in x which you can also print out its shape to get the actual number.


The size of X_train is (4,7) so the size I have for w is correct: (4,1). Even so, I still get the error from my last post shown below:

On another note, for this assignment it’s clear that w.shape=(number_of_featuers_in_X, 1) and b.shape=(a scalar). I’m confused overall about the dimensions of w and b. I thought w’s dimensions is (number_of_features_in_X, number_of_training_examples). And then b’s dimensions is (number_of_featurrs_in_X, 1). Why is it different in this case? Maybe that’s only for deep learning??


Why does w need to be reshaped shown in the code error above that comes from the function predict().


I will just let you check out the lectures again for the answers of these question, but both of the shapes suggested there were wrong.

You said your w was indeed in the shape of (4, 1), and you said X.shape[0] was 4. Then the following line


was just reshaping w from the shape of (4, 1) to (4, 1). How would it show that error message?

Note that all I can do is just to discuss the error message with you.

Good luck, @Jeb_Dean!


I guess it was to make sure w was in the right shape?

Hey rmwkwok,

Sorry I haven’t been responding, I took a little break because I was getting overwhelmed. I’m now getting the wrong values of dw. dw and db are calculated by functions that have already passed tests so I don’t know what could be wrong. Any ideas?

Hi @Jeb_Dean,

The error message itself only spoke that your work couldn’t deliver the expected result. Normally we need to review the code ourselves, but if we would like to see some hints, we can search the forum with, for example, the keyword “0.14449502” (one of the output number from your work) and check out some relevant discussions that reported the same number. There are quite a number of that.


PS1: Check if the searched discussion is about the same exercise.

PS2: Note that passing public tests only means that those tests couldn’t catch any error :wink: At the end of the day, we are the guards.