W2_A1_EX-8_Loss Function Vectorization

The exercise asks to vectorize the loss function but my answer loss = abs(y - yhat) doesn’t seem to be correct. Any hints as to why that is?

Hi @Alexandros_Konstanto ,

Please check the formula for calculating the L1 loss, you have missed the summation part of the formula. To get the summation of all the examples, use np.sum()

1 Like

And here is the output I get when I try your version of the code:

L1 = [0.1 0.2 0.1 0.6 0.1]
Error: The function should return a float.
Error: Wrong output
 0  Tests passed
 2  Tests failed
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-3f77e39d7fb5> in <module>
      3 print("L1 = " + str(L1(yhat, y)))
      4 
----> 5 L1_test(L1)

~/work/release/W2A1/public_tests.py in L1_test(target)
    216     ]
    217 
--> 218     test(test_cases, target)
    219 
    220 def L2_test(target):

~/work/release/W2A1/test_utils.py in test(test_cases, target)
     24         print('\033[92m', success," Tests passed")
     25         print('\033[91m', len(test_cases) - success, " Tests failed")
---> 26         raise AssertionError("Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))

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

So you can see that your function returned L1 as a vector and then it told you that it was expecting a float, meaning a scalar value. The point that Kin made is the key one and will solve your issue.

Just for future reference, you can frequently help yourself by first reading the error message. If you don’t understand what it is telling you, then the first thing to do is invest some effort trying to understand the message. Of course the mentors and other fellow students are here to help in the case that method doesn’t yield a solution. :nerd_face:

Thanks. I did read the error message and was puzzled by it because if you read the last line of your output it recommends not using a global variable. Isn’t ‘np’ a global variable?

Hi @Alexandros_Konstanto ,

Here is the doc:

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=, initial=, where= )[source]#

As we didn’t specify the ‘axis’ parameter, so it returns a scalar.

1 Like

That makes sense. Thanks

It’s not “my output”, but that is just one suggestion of the type of thing that could be wrong. There are some global variables that are fine to reference, as in the case of np.

It is the first two lines of the error output that contain the most valuable information in this case:

So part of what I was trying to do here is give some guidance about how to read the error messages.

Whether you get a 2D array, a 1D array or a scalar from np.sum is all a question of what arguments you specify. Here’s a thread that gives explicit examples of all three cases.

1 Like