Week 4/Assignment: Deep Neural Network – Application

Hi there,
in this assignment I didn’t pass all tests, it is the test call cell of the 2-layer-modell (two_layer_model).
One of the test calls returns: “Error: Wrong shape for variable 0”. By printing out the inputs I see that the test call before uses exactly the same inputs WITHOUT any error. This is weird …

Can you find the mistake? Please see my notebook attached and further description of the problem …

Thank you so much!
Kind regards,
Götz Baumgarten

Moderator Edit: Solution code removed.

Hello,

I am deleting your notebook as sharing this is not allowed. Please share your full error (not code) with proper assignment and exercise number.

4 - Two-layer Neural Network

Exercise 1 - two_layer_model

Error:
Cost after iteration 1: 0.6926114346158595
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.

How you are initializing the parameters? You have to use the initialize function.

Yes , I used that. I found the problem: I did not use the function compute_cost. If I call that, it works. But it is weird because if I replace the call of compute_cost by those 3 lines of this function (see below) it still does not work. Do you know why?

if 0: # exactly the lines of function compute_cost (does not work - why?!)
m = Y.shape[1] # number of examples
cost = -1/m*(np.dot(Y, np.log(A2).T) + np.dot(1-Y, np.log(1-A2).T))
cost = float(np.squeeze(cost)) # makes sure cost is the dimension we expect.
# E.g., turns [[17]] into 17
else: # this works …
cost = compute_cost(A2, Y)

Thank you very much! Best, Götz

float() and squeeze() should not be necessary.