Note that your w value is all zeros. So how could that happen? Read the code carefully. The last time the actual variable w was modified in your model function is when it gets the return values from the initialize_with_zeros function, right? It is a mistake to just use the dictionary references when you call predict. The better idea would be to extract the dictionary values once and then use them in both predict calls, which also has the desirable result that the w and b being returned by model will now be current instead of zeros.
That most likely means that when you call optimize from model, you are hard-coding the number of iterations to 2000. You can’t just “copy/paste” the definition of the function as the call to the function, right? If you do that, then the default values declared in the definition become hard-wired and passing in a different number of iterations at the top level of model does no good.
Please scan the rest of this thread. There are other discussions of similar bugs.
Thanks a ton Rashmi… I was able to complete my assignment. (y)
Hi Paulin,
Thanks a ton for your help. I am able to complete my programming assignment. feeling so thrilled !!!
Thanks.
Regards,
Hemshanker Raval
Thank you for your guidance
I fixed the hard coding issue and now I have completed the programming assignment. Referring to your response and some of the previous discussion about the similar bug helped me fix the problem. Once again thank you.
Hello,
I have a problem with this exercise, apparently all unit test until here are ok but i have a error:
{moderator edit - solution code removed}
ValueError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)
~/work/release/W2A2/public_tests.py in model_test(target)
113 y_test = np.array([0, 1, 0])
114
→ 115 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
116
117 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
24 # Gradient descent
25 # params, grads, costs = …
—> 26 params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
27 # Retrieve parameters w and b from dictionary “params”
28 # w = …
in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35 # grads, cost = …
36 # YOUR CODE STARTS HERE
—> 37 grads, cost = propagate(w, b, X, Y)
38
39 # YOUR CODE ENDS HERE
in propagate(w, b, X, Y)
30 # cost = …
31 # YOUR CODE STARTS HERE
—> 32 A = sigmoid(np.dot(w.T, X)+b)
33 cost = -(1/m)*np.sum(np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T))
34 #print(str(cost))
<array_function internals> in dot(*args, **kwargs)
ValueError: shapes (1,7) and (4,7) not aligned: 7 (dim 1) != 4 (dim 0)
Excuse me i managed to solve the problem in my code… the problem was
w, b = initialize_with_zeros(X_train.shape[1])
should be
w, b = initialize_with_zeros(X_train.shape[0])
now it works
Just out of curiosity I wanted to know what is the significance of having two example sets which is the Test set and the Training set (X_test and X_train). While doing the programming assignment I didn’t understood why have we used the Test set. Please someone let me know what is the need of it.
Hi, Miguel.
It’s great that you were able to figure this out on your own power! Yes, the number of elements in w needs to match the number of rows of X, not columns. Each column is one input “sample” vector.
Hi, Aryan.
Prof Ng covers this in the lectures in multiple places. The purpose of having two separate sets of data is that you train on the training set and then you use a set of data that was not used in the training in order to evaluate how good the trained model is at making predictions on data that it has never “seen” before. Of course you also need the performance to be good on the training data, otherwise you need to choose a different model. But the real goal is to have it perform well on the “test” data, because that is a better proxy for how it will do with “real world” input data.
There will be more sophistication in this area when we get to Courses 2 and 3 about how to use data, so please “hold that thought” and stay tuned to hear what Prof Ng says in the later courses.
Okay got it.
Thank you
Doesn’t help. Getting the same error, despite passing all the parameters (not hard-coding them). Really does not make sense to me at all.
This is quite a long thread. Have you read all the replies? Maybe the best thing would be to show us the error you are actually seeing.
Hi @Aryan.Singh,
@paulinpaloalto sir has explained it rightly. But, I would like to stretch this a bit more. Actually what Prof. Ng has tried to explain is that any model which is trained goes through a lengthy process in machine learning. To solve this problem, we try to run more than one ideas of different model architectures in order to attain the best one. So, we divide the data into three sets actually- train set|dev set|test set.
Suppose, we have 10,000 images from internet and mobile. So, we will divide this into three- 9500 for train set|250 for dev set| 250 for test set.
We use training data for deciding which model would be the best one out of all. Here, we use training data so as to emit parameters for each of the model. The dev set then ranks the models in terms of accuracy and decide to which model we must proceed with. Once these two steps are performed, we have the best model in-hand out of all. So, just to check whether this final model will perform on the unseen or the real world data, we go through the last step- “performance on test dataset”. This gives us the wholesome idea whether the chosen model is the best one or not.
Hi All,
I am getting the following error message while executing the ‘model’ method, i.e. exercise 8 of week 2 of course 1.
{moderator edit - solution code removed}
I checked all the methods, I think I am passing the correct values as well. Not sure where I am going wrong. Requesting you to guide me here.
There are several problems in your code:
-
You don’t pass the learning rate, number of iterations and print flag when you call optimize from model, so you will get the default values of those parameters as declared in the definition of the optimize function.
-
You store the return values for the parameters in a different variable name than the one you use to you extract w and b. That means you get leftover values from some earlier cell in the notebook instead of the actual values returned by optimize.
-
It’s not really a bug, but why write out the sigmoid calls again when you compute the predictions: you already built the predict function to perform that task, right?
The issue is resolved by using all new variables for the output parameters.
Yes, it’s always a mistake to reference global variable within the body of the function. You should only reference local variables, which includes the parameters that were actually passed in.


