Week 4, Exercise 5 - L_model_forward class and shape

Although my code outputs the correct AL = [[0.03921668 0.70498921 0.19734387 0.04728177]]
I’m having errors related to the class and shapes of the output, any tips?
Thank you so much, I’m getting desperate here…

AL = [[0.03921668 0.70498921 0.19734387 0.04728177]]
Error: The function should return a numpy array. in variable 0. Got type: <class ‘tuple’> but expected type <class ‘numpy.ndarray’>
Error: The function should return a numpy array. in variable 0. Got type: <class ‘list’> but expected type <class ‘tuple’>
Error: The function should return a numpy array. in variable 0. Got type: <class ‘numpy.ndarray’> but expected type <class ‘numpy.float64’>
Error: The function should return a numpy array. in variable 1. Got type: <class ‘numpy.ndarray’> but expected type <class ‘numpy.float64’>
Error: The function should return a numpy array. in variable 2. Got type: <class ‘numpy.ndarray’> but expected type <class ‘numpy.float64’>
Error: The function should return a numpy array. in variable 0. Got type: <class ‘tuple’> but expected type <class ‘numpy.ndarray’>
Error: The function should return a numpy array. in variable 1. Got type: <class ‘tuple’> but expected type <class ‘numpy.ndarray’>
Error: The function should return a numpy array. in variable 1. Got type: <class ‘list’> but expected type <class ‘list’>
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 2.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 2.
Error: Wrong shape for variable 1.
Error: Wrong shape for variable 2.
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
Error: Wrong output for variable 2.
Error: Wrong output for variable 1.
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
Error: Wrong output for variable 2.
Error: Wrong output for variable 1.
Error: Wrong output for variable 2.
0 Tests passed
3 Tests failed

AssertionError Traceback (most recent call last)
in
4 print("AL = " + str(t_AL))
5
----> 6 L_model_forward_test(L_model_forward)

~/work/release/W4A1/public_tests.py in L_model_forward_test(target)
238 ]
239
→ 240 multiple_test(test_cases, target)
241 ‘’’ {
242 “name”:“datatype_check”,

~/work/release/W4A1/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 L_model_forward. Check your equations and avoid using global variables inside the function.

Have you looked at the test case? You can see it by clicking “File → Open” and then opening the file public_tests.py and finding the appropriate routine. What you will see is that it checks more than just the AL value: it also checks the cache value, which is a critical part of the L_model_forward output. It looks like that’s where your problems are.

I added some print statements at the end of my L_model_forward code to print various things about the output values. Here’s what I get from running the test cell for L_model_forward:

l = 3
A3 = [[0.03921668 0.70498921 0.19734387 0.04728177]]
A3.shape = (1, 4)
type(caches) = <class 'list'>
len(caches) = 3
type(caches[-1]) = <class 'tuple'>
len(caches[-1]) = 2
AL = [[0.03921668 0.70498921 0.19734387 0.04728177]]

So you can see that in the “2 hidden” test case, we have 3 total layers and so the caches value is a list with 3 entries (one for each layer). By looking at the last one, you can see that each of those three elements is a “tuple” with 2 elements.

The cache value output for each layer looks like this:

((A, W, b), Z)

So that is 2-tuple. The first element of it is a 3-tuple that is the “linear cache” with the entries A, W and b, which are all numpy arrays. The second element is a 1-tuple Z that is the “activation cache” value.

Have a look at your code based on the above descriptions and try putting in some print statements in the L_model_forward code to see what is happening with the cache values.

Thank you so much for your quick feedback Paulin!
It’s solved now, I was creating the list caches and appending wrongly.

Best,

That’s good news! Thanks for confirming that you solved it. Onward! :nerd_face: