Hash_multi_plane & side_of_plane (copied from lab

For the functions: hash_multi_plane and side_of_plane, I am using the code straight from the lab. I have been coding it my way for 2 days and it did not work. I could post the code I wrote but I am not sure if posting code is allowed. It was forbidden in the Machine Learning course that I recently completed. Anyway, I am getting an error from the test code (Copied straight from the assignment) shown below. Any help would be appreciated.

def hash_multi_plane_matrix(P, v, num_planes):*
** sides_matrix = side_of_plane_matrix(P, v) # Get the side of planes for P and v***
** hash_value = 0***
** for i in range(num_planes):***
** sign = sides_matrix[i].item() # Get the value inside the matrix cell***
** hash_i = 1 if sign >=0 else 0***
** hash_value += 2i * hash_i # sum 2^i * hash_i*
** return hash_value***

def side_of_plane(P, v):
** dotproduct = np.dot(P, v.T) # Get the dot product P * v’**
** sign_of_dot_product = np.sign(dotproduct) # The sign of the elements of the dotproduct matrix **
** sign_of_dot_product_scalar = sign_of_dot_product.item() # The value of the first item**
** return sign_of_dot_product_scalar**

UNQ_C20 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# You do not have to input any code in this cell, but it is relevant to grading, so please do not change anything
planes = planes_l[0] # get one ‘universe’ of planes to test the function
tmp_hash_table, tmp_id_table = make_hash_table(document_vecs, planes)
print(f"The hash table at key 0 has {len(tmp_hash_table[0])} document vectors")
print(f"The id table at key 0 has {len(tmp_id_table[0])}")
print(f"The first 5 document indices stored at key 0 of are {tmp_id_table[0][0:5]}")

ERROR

ValueError Traceback (most recent call last)
in
2 # You do not have to input any code in this cell, but it is relevant to grading, so please do not change anything
3 planes = planes_l[0] # get one ‘universe’ of planes to test the function
----> 4 tmp_hash_table, tmp_id_table = make_hash_table(document_vecs, planes)
5
6 print(f"The hash table at key 0 has {len(tmp_hash_table[0])} document vectors")

in make_hash_table(vecs, planes, hash_value_of_vector)
37 for i, v in enumerate(vecs):
38 # calculate the hash value for the vector
—> 39 h = hash_value_of_vector(vecs[i], planes)
40
41 # store the vector into hash_table at key h,

in hash_value_of_vector(P_l, v)
4 print("i: ", i)
5 print("P: ", P)
----> 6 sign = side_of_plane(P,v)
7 print("sign: ", sign, )
8 hash_i = 1 if sign >=0 else 0

in side_of_plane(P, v)
4 sign_of_dot_product = np.sign(dotproduct) # The sign of the elements of the dotproduct matrix
5 print("sign_of_dot_product: ", sign_of_dot_product)
----> 6 sign_of_dot_product_scalar = sign_of_dot_product.item() # The value of the first item
7 print("sign_of_dot_product_scalar: ", sign_of_dot_product_scalar)
8 return sign_of_dot_product_scalar

ValueError: can only convert an array of size 1 to a Python scalar

Hi, David.

I don’t know how active the mentor support is for NLP. I have literally not gotten to this exercise, so I can’t directly help. But it seems pretty clear where to start investigating, right? It’s apparently expecting a scalar result and you’ve evidently provided it with an input that is not a 1 x 1 array. So the first question is “what shape is it?” Then the next question is “how did it get that way?” :nerd_face:

If the test code fails, it’s not a useful move to assume that it’s a problem with the test code. You have to work backwards from what it tells you to figure out where your code is going off the rails.

Hi Paul,

Not the most active I have seen. LOL. Yes you are right, it seems to be getting an array sometimes, in place of the scalar value. It seems to work sometimes and not other times. I am going to use an exception handling approach to monitoring what’s happening. Will let you know!
Thanks!
David

It might also be worth taking a look at the inputs for the various test cases to see if you can distinguish what is different about the cases that “throw” versus those that don’t. There’s got to be a clue there … :nerd_face:

I am trying to do that. The only problem is that the data is in arrays of length 300. I am exploring downloading the data to visually inspect it.

I doubt it is the individual values. More likely the data types. E.g. 1d arrays in one case and 2d in another. Or something about the dimensions. As in you are doing elementwise multiply instead of dot product somewhere. That sort of thing ….

Just print the types and shapes of all the inputs in your code. Then run the good and bad tests and I bet you see a pattern.

I’m sorry to hear that you found the course so frustrating. It looks like they’ve had trouble recruiting and keeping active mentors for the NLP courses. There are plenty of other courses from DeepLearning.AI which are pretty well supported. Not sure why this problem exists with NLP.

Thanks for the help you’ve given though. I appreciate you.