Squared Distance inC3_W2_RecSysNN_Assignment

I am struggling to write a code for squared distance. I have used Numpy function to square the difference . It is not finding square function. I find same issue with tf.

need some help and guidance. I appreciate if any one respond

1 Like

Hello @Sudhir_Nandamuru,

np.square should do the job. If you have imported numpy as np, then you need to use np.square. If you have imported numpy without giving it an alias, then numpy.square. It is very often that we give it an alias of np. Please check the first code cell of the notebook.

import numpy as np
np.square(3)
import numpy
numpy.square(3)

Cheers,
Raymond

@rmwkwok thanks… I tried to attemp the subtraction first and then square the result. np.square is not recognized…

Does the first cell of your notebook contain import numpy as np? If so, can you run it and see if np.square can be recognized?

@rmwkwok import numpy as np is first statement and I ran the library statement…I am getting the following error
ypeError Traceback (most recent call last)
in
2 a2 = np.array([1.1, 2.1, 3.1]); b2 = np.array([1.0, 2.0, 3.0])
3 a3 = np.array([0, 1, 0]); b3 = np.array([1, 0, 0])
----> 4 print(f"squared distance between a1 and b1: {sq_dist(a1, b1):0.3f}“)
5 print(f"squared distance between a2 and b2: {sq_dist(a2, b2):0.3f}”)
6 print(f"squared distance between a3 and b3: {sq_dist(a3, b3):0.3f}")

TypeError: unsupported format string passed to numpy.ndarray.format

Oh, the error implies that your sq_dist function is returning a numpy array which is wrong. Your function should return a float.

Note the following formula that you don’t just have to subtract, square, but also sum over it.

image

Did you do the “sum” step?

Raymond

@rmwkwok

I didnt do sum step… i am not sure how to put the code here… I used tensor flow function and it didnt work either

Sudhir

@rmwkwok I running into following error
UnboundLocalError: local variable ‘d’ referenced before assignment

Hello @Sudhir_Nandamuru, you have been using numpy functions, why don’t you use numpy function for the sum? Here is the numpy function for sum: np.sum.

Sudhir, what do you think this error means? :wink:

@rmwkwok It is variable initialization error that I missed it earlier. I got the lab working after finding an error with my code. thanks for your guidance…

Sudhir

Great work Sudhir @Sudhir_Nandamuru . sq_dist is a small function, but it is an important success of yours. :wink:

Cheers,
Raymond

Hi @rmwkwok. In the cofi cost function if I use the np.dot function to compute dot product between x and w, doesn’t the sum you mention in earlier posts get taken care of?

Hello @David_Stulman,

It would have been better to ask a different question in a new thread, since this thread seems to be more related to the squared distance function.

I think I have not talked about anything directly related to the cofi cost function in this thread.

np.dot involves multiplications and additions, so you may be right.

Cheers,
Raymond