Week 4, Assignment 2, two-Layer model

I spend number of days in finding out of mistakes . I am getting the correct costs (even for 2499 iterations) and even use the correct W1,W2,b1,b2 but I am getting the error :
“Wrong shape for variable 0”.Please sir , tell me what does this error mean and how I can remove it.

Following text appear as result of execution of my code.

Cost after iteration 1: 0.6926114346158594
Cost after first iteration: [0.69304974]
Cost after iteration 1: 0.6915746967050506
Cost after iteration 1: 0.6915746967050506
Error: Wrong shape for variable 0.
Cost after iteration 1: 0.6915746967050506
Cost after iteration 2: 0.6524135179683452
3 Tests passed
1 Tests failed

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.

1 Like

Here are my results from that test case:

Cost after iteration 1: 0.6926114346158595
Cost after first iteration: 0.693049735659989
Cost after iteration 1: 0.6915746967050506
Cost after iteration 1: 0.6915746967050506
Cost after iteration 1: 0.6915746967050506
Cost after iteration 2: 0.6524135179683452
 All tests passed.

Notice that everything is the same except the second line. I’m worried that is where the problem is: note that in your case the value prints with brackets around it. That might also explain why you get that error about the shape being wrong.

I added the following statements right before the “return” statement in my two_layer_model code:

    print(f"type(costs) {type(costs)}")
    print(f"type(costs[0]) {type(costs[0])}")
    print(f"costs[0].shape {costs[0].shape}")

Here’s what I get when I run that test cell with those prints in place:

Cost after iteration 1: 0.6926114346158595
type(costs) <class 'list'>
type(costs[0]) <class 'numpy.ndarray'>
costs[0].shape ()
Cost after first iteration: 0.693049735659989
Cost after iteration 1: 0.6915746967050506
type(costs) <class 'list'>
type(costs[0]) <class 'numpy.ndarray'>
costs[0].shape ()
Cost after iteration 1: 0.6915746967050506
type(costs) <class 'list'>
type(costs[0]) <class 'numpy.ndarray'>
costs[0].shape ()
Cost after iteration 1: 0.6915746967050506
type(costs) <class 'list'>
type(costs[0]) <class 'numpy.ndarray'>
costs[0].shape ()
Cost after iteration 2: 0.6524135179683452
type(costs) <class 'list'>
type(costs[0]) <class 'numpy.ndarray'>
costs[0].shape ()
 All tests passed.

So my costs[0] value is a 0D array. What do you see if you try the above?

Thank you Sir . You find out mistake correctly.

sir, in my case it’s showing 0 tests passed, 4 test failed, what should I do now sir??

It looks like your costs values have the correct type and values, so that is apparently not the problem. If you look at the output carefully, you can see that it’s the public test that fails. You can examine the code in the file public_tests.py to see how it works. It’s getting a datatype mismatch, but doesn’t tell us which variable. The other return value is parameters, which is supposed to be a dictionary containing numpy arrays. Try adding some more print statements to show the type and values of parameters.

I did that in my code and here’s what I get:

Cost after iteration 1: 0.6926114346158595
type(parameters) <class 'dict'>
type(parameters['W1']) <class 'numpy.ndarray'>
parameters['W1'].shape (7, 12288)
Cost after first iteration: 0.693049735659989
Cost after iteration 1: 0.6915746967050506
type(parameters) <class 'dict'>
type(parameters['W1']) <class 'numpy.ndarray'>
parameters['W1'].shape (4, 10)
Cost after iteration 1: 0.6915746967050506
type(parameters) <class 'dict'>
type(parameters['W1']) <class 'numpy.ndarray'>
parameters['W1'].shape (4, 10)
Cost after iteration 1: 0.6915746967050506
type(parameters) <class 'dict'>
type(parameters['W1']) <class 'numpy.ndarray'>
parameters['W1'].shape (4, 10)
Cost after iteration 2: 0.6524135179683452
type(parameters) <class 'dict'>
type(parameters['W1']) <class 'numpy.ndarray'>
parameters['W1'].shape (4, 10)
 All tests passed.

Hello, I’ve trained the two-layer model and get all the expected costs right but on the previous
exercise I get the following error


-----------------…-----------------…-------------------------

Here’s the result from fitting the network

Hi, Ctl.

Did you hard-wire any of the values within the function? Doing this might ruin the expectation of getting the right output!

Also, do check whether you are implementing the right activation in place.

Besides, you are implementing back prop for Y and (1-Y) both. Is that in order?

Hi Rashmi, thank you for your prompt response.

No values are are not hard-coded since they are computed by the functions defined on the previous homework. As for the order of backprop I’ve called linear_activation_backward first with dA2 activating "sigmoid" and then with dA1 on "relu". Is that what you mean?

Are you using initialize_parameters_deep for two_layer_model? Using this will give the wrong output. Use initialize_parameters instead!

I’ve used the non-generalized initialize_parameters from the previous chapter

Please DM me your notebook, let me have a look over it.

The way you state that worries me a little: yes, that’s the correct function to use, but note that you do not have to manually “copy” that function cell over to this notebook. All the “Step by Step” functions are provided for you using an “import” command. Hand-copying the previous functions will cause problems, but not so much on the two_layer_model case …

I’m sure Rashmi will be able to see what is going on from your notebook.

Hello Paul sir, thank you for kind response. Sir, I did go through his notebook, but couldn’t find anything wrong in the implementations. Thank you.

Hi, I just called the function as if I had defined it on a previous cell. I didn’t copy/paste or do any explicit importing.

Hi, Carlos.

The error is actually in the below lines of code:

Compute cost\n",

"        #(≈ 1 line of code)\n",
"        cost = compute_cost(A2, Y)\n",
"        costs.append(cost)\n"

The appending of the cost to the costs array is done by the template itself, which is only done after every 100 iterations. But it seems, you have done it on every single iteration, and that is why the costs array ends up being in the wrong size.

I see, thank you. I should more carefully parse what’s already given by the template and how it interacts with my own code. Thanks again!