Problem with exercise of week4 - Deep Neural Network - Application

Hello,

I am having a problem with the assignment for week 4 of the course “Deep Neural Network - Application”

The problem is the following: When testing function “two_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost=False):” via the test commands:"

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)”

I get the error:

" ValueError 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)
ValueError: too many values to unpack (expected 2)”

According to what I can see online while trying to solve the problem, I noticed that on previous version of this exercise, the test call to the test function has “print_cost=True” while in the exercise version that I am doing, the same call to the same function has “print_cost=False”. As such, the graphic for the cost gets empty and I get the error stated above. I can have all the assignments of this course passed but this one is failing due to this error and I am not capable of understanding why this error or what am I doing wrong.

Any help would be gladly appreciated.

Regards,
Luís

1 Like

That mean the function two_layer_model() returns more than 2 values in a tuple.

What is expected are two, which will be unpacked into parameters and costs. Check the construction of the return value inside two_layer_model()

Hello @LFC ,

Please add your questions in the course QA.

You can find courses in the specialization. And you must tag the “week” for your question.

Hello dtonhofer,

Thanks for you help. The function only returns parameters:
return parameters

and if I add to return also the costs, I still get an error:

"---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
in
3 print("Cost after first iteration: " + str(costs[0]))
4
----> 5 two_layer_model_test(two_layer_model)

~/work/release/W4A2/public_tests.py in two_layer_model_test(target)
75 ]
76
—> 77 multiple_test(test_cases, target)
78
79

~/work/release/W4A2/test_utils.py in multiple_test(test_cases, target)
140 print(‘\033[92m’, success," Tests passed")
141 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
→ 142 raise AssertionError(“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))
143

AssertionError: Not all tests were passed for two_layer_model. Check your equations and avoid using global variables inside the function."

if I return parameters, (empty space after the coma), I get:

ValueError 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)

ValueError: not enough values to unpack (expected 2, got 1)

So, if I only “return parameters” I get an error stating too many values to unpack (expected 2)
If I "return parameters, " I get an error stating not enough values to unpack (expected 2, got 1)

I have all the other assignments done and ok…it is just this one that is failing and I believe it is due to the fact that the test function calls two_layer_model() with print_costs = False and I can not change this cell.

And when I try to find help on this exercise on the internet, the examples of code that passed this test have the test function calling two_layer_model() with print_costs = True..

But thank you very much for your help.

Regards,
Luís

No, that’s not it. In particular, that problem would be well-known.

So, if I only “return parameters” I get an error stating too many values to unpack (expected 2)

This means parameters is a tuple that is too long - more than 2 parameters. Try to print parameters before returning it.

If I "return parameters, " I get an error stating not enough values to unpack (expected 2, got 1)

This is normal. If you write return parameters, a tuple of length 1 holding parameters will be created and returned. A tuple of length 1 is too short for the expected 2 elements.

For example, this:

def foo():
    parameters = [1,2,3]
    return parameters,

result = foo()

print(f"Type   : {type(result)}")
print(f"Length : {len(result)}")
print(f"Result : {result}")

gives

Type   : <class 'tuple'>
Length : 1
Result : ([1, 2, 3],)

It sounds like you copied an old solution from the internet. That does not work if the solution is too old, because they changed the definition of the function in 2021. Also note that it’s against the rules to copy someone else’s solution.

The current definition of the function returns two return values, as shown by the test code, but the old version only returned the parameters variable. I suggest you get a clean copy of the notebook to get the correct template code to work with.

1 Like

Thanks!