W4_A2_Two layer model_too many values to unpack

Hello,

Hi, I am trying to run the default code snippet of the course where it is like this:

parameters, costs = two_layer_model(train_x, train_y, layers_dims = (n_x, n_h, n_y), num_iterations = 2, print_cost=False)

print("Cost after first iteration: " + str(costs[0]))

two_layer_model_test(two_layer_model)

but I get the following error. Any thought?
00

I refreshed the whole notebook and it not worked.

Please use the “pencil” icon in the thread title, and move your message to the forum area for the course and week are attending.

This looks like the second assignment in DLS Course 1 Week 4, so I will move it for you.

The error message is telling you that your two_layer_model function is supposed to return two separate return values, but it only returns one. So how could that happen? Please check the return statement in the two_layer_model function. That was part of the template code and you should not have needed to modify it.

One other thing to note is that if you just copied a solution from github, you might have gotten an old version of this notebook. The functions used to work differently, so it doesn’t work to just copy the functions from an old solution. Beside the fact that it’s against the course Honor Code, so I hope that is not what is going on here. :nerd_face:

Thanks for your reply.

I try to write it again and that what happen


IndexError Traceback (most recent call last)
in
1 parameters, costs = two_layer_model(train_x, train_y, layers_dims = (n_x, n_h, n_y), num_iterations = 2, print_cost=False)
2
----> 3 print("Cost after first iteration: " + str(costs[0]))
4
5 two_layer_model_test(two_layer_model)

IndexError: too many indices for array

That is a different problem of course. The first thing you need to do when you are debugging is understand what the error message is telling you. In this case it means that the variable costs in that line is supposed to be a list or array with one dimension, so that it can be indexed to select the first output value. But somehow it is not and you need to figure out why. So what type is it? Try this:

print(type(costs))

Note that the test cell is not modifiable, but you can add another cell below that by doing “Insert → Cell Below”.

If it says that it is a array, then try this:

print(costs.shape)

If it says that it is a list, then try this:

print(len(costs))

Why does the indexing fail? It is your job to figure that out. The point is that debugging is part of the job of being a programmer. It is a necessary skill that you need to strengthen. Notice that I’m not just giving you the answer: I’m trying to show you how to figure out the answer for yourself.

I added this cell after that test cell in my notebook:

print(f"type(parameters['W1']) = {type(parameters['W1'])}")
print(f"type(costs) = {type(costs)}")
print(f"len(costs) = {len(costs)}")
print(f"costs[-1] = {costs[-1]}")

When I run that, here’s what I get:

type(parameters['W1']) = <class 'numpy.ndarray'>
type(costs) = <class 'list'>
len(costs) = 1
costs[-1] = 0.693049735659989

I’m guessing from the error message that you will see that your costs value is a numpy array. Now you need to figure out why that happened. Note that all the code to handle the costs variable was just given to you in the template code and you didn’t need to change any of it.

Thank you so much