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

2.1 Implement the L1 and L2 loss functions

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]:

GRADED FUNCTION: L1

​

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 =

YOUR CODE STARTS HERE

YOUR CODE ENDS HERE

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 .

Hi @mahiptiwari1000 .

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

@behdad.bahadivand please do not post the code solutions.

1 Like

Got it,thank you for informing me

1 Like

I’m sorry. My bad.

1 Like

No problem.