Hello team, I am having issue on this week 2 programming assignment exercise 8 where I need to merge all the functions I created. Even though I managed to pass the assignment, but my codes on this task didn’t run and I want it to run so that I can be able to form a complete logistic regression model. Is there anyway I can get a help on getting the right codes that give me the output? Is it possible/allowed to share my codes here so that anyone can help me to debug any errors in the codes?
Looking forward to hearing from you. Thank you
The course rules are that we are not supposed to share solution code here on the forums or in any other public place. But usually we can offer some suggestions for debugging based on the outputs you are getting. So one way to start would be to “copy/paste” the full output you get from the failure that you are seeing.
If we can’t help based on that, then there are ways to share code privately. But doing everything in public according to the rules also has the advantage that other students may be able to learn from our discussion as well.
Let’s give it a try and see how it goes.
yeah sure. It makes sense. so here is the output I am getting
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)
123 y_test = np.array([0, 1, 0])
124
→ 125 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127 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)
42 b = params[“b”]
43
—> 44 Y_prediction_test = predict(w, b, X)
45 Y_prediction_train = predict(w, b, X)
46
in predict(w, b, X)
16 m = X.shape[1]
17 Y_prediction = np.zeros((1, m))
—> 18 w = w.reshape(X.shape[0], 1)
19
20 # Compute vector “A” predicting the probabilities of a cat being present in the picture
ValueError: cannot reshape array of size 4 into shape (2,1)
<Update: sorry I wasn’t looking at the full trace, just the error message. Please see my next reply.>
That means your w array is the wrong size. So where is that determined? It is the argument you pass to the “initialization” function, right? Are you sure that you did not reference global variables like dim when you made that call? You need to base the shape on the number of rows of the X matrix, right?
Oh, sorry, I think the problem is that you are passing the wrong value to predict for the third argument: there is no variable called X in the model function, right? So you are picking up a global variable that has nothing to do with everything else.
I see. Now here is the error I am getting after passing X_test and X_train in the predict function
AssertionError 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)
131 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"
132 assert d[‘w’].shape == (X.shape[0], 1), f"Wrong shape for d[‘w’]. {d[‘w’].shape} != {(X.shape[0], 1)}"
→ 133 assert np.allclose(d[‘w’], expected_output[‘w’]), f"Wrong values for d[‘w’]. {d[‘w’]} != {expected_output[‘w’]}"
134
135 assert np.allclose(d[‘b’], expected_output[‘b’]), f"Wrong values for d[‘b’]. {d[‘b’]} != {expected_output[‘b’]}"
AssertionError: Wrong values for d[‘w’]. [[-0.00412895]
[ 0.07901825]
[-0.09511868]
[-0.18598965]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]
Ok, one step at a time. That probably means you are “hard-coding” some or all of the optional parameters that you pass from model to optimize. There should be no “equal signs” in the parameter list when you call optimize. And you need to pass all the parameters, including learning rate and number of iterations, so that you don’t end up using the default values.
okay. great. I also think I failed to initialize w and b to zeros… for me I called the initialize_with_zeros function. is it right?
Yes, you need to call initialize_with_zeros from model in the early part of it.
ooraa then I am wondering why my codes are giving me erros
In the early optimize function we were having parameters including X and Y… so while calling it in the model, which variables should we use … For me I used X_train and Y_train instead of X and Y
You are supposed to use local variables defined within the scope of the model function, right? Those would include the declared parameters that are in the definition of model.
yeah yeah… let me take break… I will work on it tomorrow again to see if it can work well. Thank you very much