Course 1, Programming Assignment 1, 4.1, Exercise 3

I implemented the forward prop equation in a straightforward way:

{moderator edit - solution code removed}

The dimensions for my matrices and answer look right. But I am failing the tests. Apparently my calculation works for the first case but does not generalize to the other test cases. But I can’t figure out why. Also confusing is the error about using global variables since I do see any in my function. I thought that maybe I need to account for A[1] = X. But how can I do this with only one line of code? Below is the code and output. Any help would be greatly appreciated.

{moderator edit - solution code removed}

t_A, t_W, t_b = linear_forward_test_case()
t_Z, t_linear_cache = linear_forward(t_A, t_W, t_b)
print("Z = " + str(t_Z))

linear_forward_test(linear_forward)
W = [[ 1.74481176 -0.7612069 0.3190391 ]]
A = [[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 ]]
b = [[-0.24937038]]
W.shape = (1, 3)
A.shape = (3, 2)
b.shape = (1, 1)
Z.shape = (1, 2)
Z = [[ 3.26295337 -1.23429987]]
W = [[ 1.74481176 -0.7612069 0.3190391 ]]
A = [[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 ]]
b = [[-0.24937038]]
W.shape = (1, 3)
A.shape = (3, 2)
b.shape = (1, 1)
Z.shape = (1, 2)
W = [[ 1.74481176 -0.7612069 0.3190391 ]]
A = [[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 ]]
b = [[-0.24937038]]
W.shape = (1, 3)
A.shape = (3, 2)
b.shape = (1, 1)
Z.shape = (1, 2)
Error: Wrong shape for variable 0.
Error: Wrong shape for variable 1.
W = [[ 1.74481176 -0.7612069 0.3190391 ]]
A = [[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 ]]
b = [[-0.24937038]]
W.shape = (1, 3)
A.shape = (3, 2)
b.shape = (1, 1)
Z.shape = (1, 2)
Error: Wrong output for variable 0.
Error: Wrong output for variable 1.
1 Tests passed
2 Tests failed

AssertionError Traceback (most recent call last)
in
3 print("Z = " + str(t_Z))
4
----> 5 linear_forward_test(linear_forward)

~/work/release/W4A1/public_tests.py in linear_forward_test(target)
107 ]
108
→ 109 multiple_test(test_cases, target)
110
111 def linear_activation_forward_test(target):

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


  1. 0 ↩︎

The code you wrote to compute Z is correct. But it looks like you decided for some unknown reason to modify the line of the given template code that constructs the cache. As a result, you fail the test, because the test cares about the contents of the cache. Here’s what the last two lines of the template for the function look like in my notebook:

    cache = (A, W, b)
    
    return Z, cache

Note that doesn’t count as “sharing solution code” since it’s just part of the template that they give to everyone. Compare that to your code. Do you see the difference? Why did you change that? It is not necessarily illegal to change code outside the boundaries of the “START CODE HERE” to “END CODE HERE” blocks, but extreme care is required if you “go there”. :nerd_face:

Also note that in general it’s against the rules to just post your source code and say, in effect, please fix it for me. It’s ok to show us the exception traces you may get or the output of running the code, but please don’t publish your actual code. Although it does always make diagnosing the problem a lot easier :laughing:

2 Likes

One other point worth making here is that when you fail one of the tests like that, you don’t immediately know what they mean by “variable 0” and “variable 1”. To figure that out, you can look at the actual test code to see what it is comparing against. Click “File → Open” from the notebook and then open the file public_tests.py and find the relevant function.

If you had done that in this case, you’d have discovered that the cache contents is part of what it is checking and you would have seen that your Z value is correct, which would have pointed you in the right direction to find the bug.

expected_Z = np.array([[ 3.26295337, -1.23429987]])

2 Likes

Thank you!!! I see the problem. I must have accidently change that line. I had thought to save the file and start again; but your quick reply save me some time especially since it might have taken a while for me to see the error :nerd_face:!

Thank you also for the explanation on using Discourse properly. This is the first problem I encounter a problem in this course that I couldn’t solve fairly quickly.

Enjoy your holidays!
Alex

This is a great to know too!

paulinpaloalto wrote:

One other point worth making here is that when you fail one of the tests like that, you don’t immediately know what they mean by “variable 0” and “variable 1”. To figure that out, you can look at the actual test code to see what it is comparing against. Click “File → Open” from the notebook and then open the file public_tests.py and find the relevant function.

1 Like