Stuck on Exercise 9 - L2

I’m new to coding/Python and Deep Learning/ML and finding these practice exercises incredibly difficult without prior knowledge. While I understand the concepts, I do not know how to translate it into code. Is it expected that I know all these functions and syntax?

Anyway, I’m trying to calculate the L2 loss function as loss = np.dot(yhat, y).

It returns the following (I have no idea what is correct and what is not):

Error: Wrong output
1 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
4 print("L2 = " + str(L2(yhat, y)))
5
----> 6 L2_test(L2)

~/work/release/W2A1/public_tests.py in L2_test(target)
249 ]
250
→ 251 test(test_cases, target)
252

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

It’s an Intermediate-level course. It assumes you already have fundamental Python programming skills.

I ended up finding the correct answer. However, I’m confused why the np.dot function was hinted. I cannot find a solution that uses it.

The dot product of two vectors (np.dot) is the product of the corresponding elements of the two vectors, followed by taking the sum of those values. The mathematical formula for the L2 loss is:

L2 = \displaystyle \sum_{i = 1}^n (\hat{y_i} - y_i)^2

so you can compute that by doing the dot product of (\hat{y} - y) with itself.

But there are other alternatives: elementwise multiplication followed by np.sum.

As Tom says, MLS and DLS assume you already know python. You get an introduction to numpy but you need to know python already. If you already have significant programming experience in another language, you might be able to get by just looking at some tutorials on python to understand what is different about it. But if this is literally your first exposure to any kind of programming, it is highly recommended that you first take an “intro to python” course.