Loss function L1

I 've tried to implement L1 loss function many time and I 've continuously got errors, I 've finished the whole assignment successfully and still getting an error

please any suggestions for where might the error be from


1 Like

I 've changed the code slightly and passed in one test of two.
This is the error text:

L1 = 0.1
Error: Wrong output
1 Tests passed
1 Tests failed
AssertionError Traceback (most recent call last)
3 print("L1 = " + str(L1(yhat, y)))
----> 5 L1_test(L1)

~/work/release/W2A1/public_tests.py in L1_test(target)
216 ]
→ 218 test(test_cases, target)
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.

Actually I didn’t understand difference between subtracting arrays[i] and just subtracting arrays without [i]


Hi @MoHassan, the difference between arrays[i] or substracting arrays without the [i] is the vectorization concept. Numpy allows performing that operation more efficiently:

a = np.array([.4, 0.1, 0.3, .2, .8])
b = np.array([1, 0, 0, 1, 1])
a - b

array([-0.6,  0.1,  0.3, -0.8, -0.2])

You can calculate the difference element-wise of a and b as above or you could do it element by element, i.e. for each i you could calculate a[i] - b[i].

As you have the arrays for the tests I would suggest that you calculate the L1, step by step outside the L1 function and then you can put it all together in the L1 function.

yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
1 Like

Hello @albertovilla, I 'm not sure I fully understand the difference between the two subtraction methods, I 'm stuck in how the output in the second method is scalar, how is it computed?

Concerning the programming exercise, I 've tried many methods, including the method you described above and the problem persists, I 've tried it on a separate example and I saw that it worked well, so please through me suggestions to get over this problem.

Thanks so much.

Hi, in order to get an scalar you may want to check the definition of numpy.sum here. Example:

np.sum([0.5, 1.5])

Basically you have three operations, calculate the difference, the absolute of that and then the sum. As suggested in my previous post I would do it manually step by step, start with a couple of vectors a and b and perform all the operations one by one, first the difference, then the absolute value and finally the sum. Once you see it working then you can put it all together.

1 Like

Hello, I 've done implementation you said above and it worked in the assignment and I passed it :slightly_smiling_face:

After that I tried to follow the same approach but expanded on more steps and other variables as you said above, and unfortunately I got error in the second step.


@MoHassan maybe you could share your code in a private message so I can have a look at it. If step-by-step works putting it all together is quite straightforward.

Also, you may want to have a look at this reference if you have doubts about vectorized operations on numpy arrays.

I just tried implementing the code again and it worked fine.

Thanks so much.