# I'm getting error on this code snippet, why?

### Exercise 8 - L1

Implement the numpy vectorized version of the L1 loss. You may find the function abs(x) (absolute value of x) useful.

Reminder:

• The loss is used to evaluate the performance of your model. The bigger your loss is, the more different your predictions (π¦Μ π¦^) are from the true values (π¦π¦). In deep learning, you use optimization algorithms like Gradient Descent to train your model and to minimize the cost.
• L1 loss is defined as:

πΏ1(π¦Μ ,π¦)=βπ=0πβ1|π¦(π)βπ¦Μ (π)|(6)(6)πΏ1(π¦^,π¦)=βπ=0πβ1|π¦(π)βπ¦^(π)|

In [48]:

β

def L1(yhat, y):

ββ"

Arguments:

yhat β vector of size m (predicted labels)

y β vector of size m (true labels)

Returns:

loss β the value of the L1 loss function defined above

ββ"

#(β 1 line of code)

# loss =

return loss

In [49]:

yhat = np.array([.9, 0.2, 0.1, .4, .9])

y = np.array([1, 0, 0, 1, 1])

print("L1 = " + str(L1(yhat, y)))

β

L1_test(L1)

Hi there,
Correct code :

{moderator edit - solution code removed}

The L1 norm is calculated as the sum of the absolute vector values .

Welcome to the community!

Please note that sharing graded code publicly is against our community guidelines. This ensures that all learners have the opportunity to effectively engage with and learn the material themselves.

If you encounter any issues or errors with your assignment, feel free to share the specific error messages or parts of the code where youβre having trouble. The mentors here are happy to help you understand and resolve those issues.

Thank you for understanding, and happy learning!

1 Like