Week 2, Neural network Exercise 8 - model question

All of my functions passed the tests, but I am getting an error message in Exercise 8 when I am trying to put together the model.

   # (≈ 1 line of code)   
# initialize parameters with zeros 
# w, b = ...

#(≈ 1 line of code)
# Gradient descent 
# parameters, grads, costs = ...

# Retrieve parameters w and b from dictionary "parameters"
# w = ...
# b = ...

# Predict test/train set examples (≈ 2 lines of code)
# Y_prediction_test = ...
# Y_prediction_train = ...

I believe I correctly implemented the above instructions but I am getting the following error when I run the model_test(model)


ValueError Traceback (most recent call last)
----> 1 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
109 y_test = np.array([1, 0, 1])
→ 111 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=1e-4)
113 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)
43 b = params[“b”]
—> 45 Y_prediction_test = predict(w, b, X_test)
46 Y_prediction_train = predict(w, b, X_train)

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)
20 # Compute vector “A” predicting the probabilities of a cat being present in the picture

ValueError: cannot reshape array of size 2 into shape (4,1)

I spent more than an hour trying to fix this error. I am at a loss.

Hi @nagyka

Please temporarily post your solution for exercise 8, and I will see if I can see any mistakes.

Hi @jonaslalin,

Here it is - temporarily:
solution code removed

Your error is a typo. Try changing parameters into params.
If it works, please also remove your solution code :slight_smile:

sigh…fixing the typo fixed the code…I will remove the solution.
Thank you so much!

1 Like

I noticed that the typo came from the instructions:

#(≈ 1 line of code)
# Gradient descent 
# parameters, grads, costs = ...

Could you have them fix it to

params, grads, costs = …

The assignment “trained” us to use the instructions for writing our code and that’s what I used. Which is why I did not noticed the typo. It cost me more than of hour of lost time.

1 Like

@nagyka, the instructions are correct :slight_smile:
Retrieve parameters and # Retrieve parameters w and b from dictionary "parameters", not params. I was just kind to give you a hint where you only needed to change one line of code, instead of two. In this case, params or parameters does not matter. Consistency matters.

1 Like

I’m having the same error as @nagyka but I don’t have a typo for ‘params’. Can I temporarily post my solution as well for you to have a look?

Sure @jkyle, I will give it a try :slight_smile:

OK @jonaslalin

Here it is - temporarily:
solution code removed

@jkyle, from what I see, you actually have exactly the same typo :slight_smile:
In your optimize call, try changing parameters to params. Please remove the code if it works.

I got your point about “consistency.” And I truly appreciate the hint. I should have spotted the inconsistency. My bad.

In the Optimize function in exercise 6 we used “params” so I was focused on that, not catching that Ex 8 was using “parameters”

From Ex 6:
params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)

print ("w = " + str(params[“w”]))
print ("b = " + str(params[“b”]))
print ("dw = " + str(grads[“dw”]))
print ("db = " + str(grads[“db”]))
print("Costs = " + str(costs))


Thanks again!!! Have a good Sunday!

@jonaslalin That worked, thanks! I would agree with @nagyka though, the hint in the assignment says to use “#parameters, grads, costs = …”, that cost me about an hour trying to debug the mistake. Shouldn’t the prompt say “# params, grads, costs = …”? I doubt we’re the only two falling in that trap.

1 Like

No worries @nagyka. I will make a suggestion to the lab devs to revise params and parameters wording everywhere. Thx and good luck with the rest of the course :slight_smile:

Yes, @jkyle, but the hint also says to use parameters to update w and b :slight_smile:

    # Retrieve parameters w and b from dictionary "parameters"
    # w = ...
    # b = ...

Anyway, I will forward your suggestion. Good luck with the rest of the course.

@jkyle I am sure we are not the only ones. I have spent more than an hour unable to see the inconsistency.

1 Like

@nagyka and @jkyle, it is great that you spot inconsistencies like this. It will help us improve the course content. Please report anything else that might mislead you and waste your time :slight_smile: I have reported this issue upstream now. Thx again.

1 Like

@nagyka and @jkyle, parameters has now been changed to params in the lab and the new version should be online shortly. Thx again for your feedback :slight_smile:

1 Like