Predict grader cell issue

Hello @arvyzukai

NameError Traceback (most recent call last)
Cell In[171], line 5
3 question2 = “When can I see you again?”
4 # 1 means it is duplicated, 0 otherwise
----> 5 predict(question1 , question2, 0.7, model, verbose = True)

Cell In[170], line 26, in predict(question1, question2, threshold, model, verbose)
23 v2 = v1v2[0]
24 # Take the dot product to compute cos similarity of each pair of entries, v1, v2
25 # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
—> 26 d = tf.math.reduce_sum(v1[j]*v2[j])
27 # Is d greater than the threshold?
28 res = d>threshold

NameError: name ‘j’ is not defined

The below test failed as I edited out the j range from the code, I got the below result on running the test cell

Wrong prediction for questions Q1: Do they enjoy eating the dessert?, Q2: Do they like hiking in the desert?
Expected:False,
Got:True.

Wrong prediction for questions Q1: Why don’t we still do great music like in the 70’s and 80’s?, Q2: Should I raise my young child on 80’s music?
Expected:False,
Got:True.

3 tests passed
2 tests failed

this part of the code was already given by the grader,
Take the dot product to compute cos similarity of each pair of entries, v1, v2
25 # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
—> 26 d = tf.math.reduce_sum(v1[j]*v2[j])
27 # Is d greater than the threshold?
28 res = d>threshold

so is my extract v1 and v2 from the model output code incorrect? I have recalled v1 and v2 using v1v2[0] for both, so I suppose recalling v2 also as v1 i.e. v1v2[0] is incorrect, can I get a hint!!! please

Regards
DP

Hi @Deepti_Prasad

As the error suggests, there’s no “j” in your code.

I believe you saw this post, so you would not need the “index j” (which you don’t need when doing vector calculations), just multiply v2 and v1 elementwise and sum them. In other words do the same thing:

but without “j”.

Cheers

P.S. Note, that you could achieve the same thing with tf.linalg.matmul but for some reason, the course creators wanted us to use tf.math.reduce_sum.

1 Like

Arvy that j was already given by the grader and not written by me, so do I edit that out?

Also when I remove j I get the second issue in where two test fail, I have shared that detail too in the post comment as expected is false but got true

What do you mean? Where exactly it was given?

Last grader cell had part of codes already given. We had to only write codes for

v1v2

v1
v2

Hmm… That’s strange… Could you refresh your Notebook to confirm that? At least for me, the code to complete should be:

    # Call the predict method of your model and save the output into v1v2
    v1v2 = None
    # Extract v1 and v2 from the model output
    v1 = None
    v2 = None
    # Take the dot product to compute cos similarity of each pair of entries, v1, v2
    # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
    d = None
    # Is d greater than the threshold?
    res = None

    ### END CODE HERE ###
1 Like

M sorry my bad, memory loss, it was not given when I was not able to solve the previous grader cell I had filled those codes referring to the upgraded lab from week 3 :joy::crazy_face:

1 Like

I am failing two tests

Wrong prediction for questions Q1: Do they enjoy eating the dessert?, Q2: Do they like hiking in the desert?
Expected:False,
Got:True.

Wrong prediction for questions Q1: Why don’t we still do great music like in the 70’s and 80’s?, Q2: Should I raise my young child on 80’s music?
Expected:False,
Got:True.

3 tests passed
2 tests failed

It’s hard to tell Deepti, why that is the case only from that. But this suggests, that your implementation is predicting duplicates when it should not.

The reasons could be many.

What do you get from the “Expected Outputs” prior to the test case?

1 Like

my d value don’t match for the first test cell

Q1 = When will I see you? Q2 = When can I see you again? d = 2.0000002 res = True

Out[66]:

True

Expected Output

If input is:

question1 = "When will I see you?"
question2 = "When can I see you again?"

Output is (d may vary a bit):

1/1 [==============================] - 0s 13ms/step
Q1  =  When will I see you? 
Q2  =  When can I see you again?
d   =  0.8422112
res =  True

second test cell, both d and res don’t match

Q1 = Do they enjoy eating the dessert? Q2 = Do they like hiking in the desert? d = 1.9999999 res = True

Out[67]:

True

Expected output

If input is:

question1 = "Do they enjoy eating the dessert?"
question2 = "Do they like hiking in the desert?"

Output (d may vary a bit):

1/1 [==============================] - 0s 12ms/step
Q1  =  Do they enjoy eating the dessert? 
Q2  =  Do they like hiking in the desert?
d   =  0.12625802
res =  False

False

OK, that narrows a bit - first, you have to get the d right.

What do you get for v1 and v2?

1 Like

(codes removed)

when I used like this I had got an index error, so I used

v1 = v1v2[0]
v2 = v1v2[-1]

Remember what the model’s last layer does:
image
it concatenates the outputs.

So when you call model.predict(*) you get the concatenated output (1, 256) which you now need to split back into v1 and v2. (the v1v2[0] and v1v2[-1] would not work, since it’s the same thing in this case).

You probably have successfully implemented that in Exercise 4 (the classify function). What you need again here is to get:

  • v1.shape - (1, 128)
  • v2.shape - (1, 128)

Here is the entire thing


For (“When will I see you?” and “When can I see you again?”), against which you can check your values. First:

    # Call the predict method of your model and save the output into v1v2
    v1v2 = ?

Output:

v1v2.shape
(1, 256)

v1v2[0, :3]
array([0.03971604, 0.06642354, 0.00687801], dtype=float32)

v1v2[0, 128:131]
array([ 0.16333432, -0.01577964,  0.00418817], dtype=float32)

Second, now you need to split them:

    # Extract v1 and v2 from the model output
    v1 = ?
    v2 = ?

Output:

### v1
v1.shape
(1, 128)

v1[0, :3]
array([0.03971604, 0.06642354, 0.00687801], dtype=float32)

### v2
v2.shape
(1, 128)

v2[0, :3]
array([ 0.16333432, -0.01577964,  0.00418817], dtype=float32)

Then you need to get sum of the element wise products:

    # Take the dot product to compute cos similarity of each pair of entries, v1, v2
    # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
    d = ?

Output:

print(d)
<tf.Tensor: shape=(), dtype=float32, numpy=0.8422112>

P.S. even they ask to use tf.math.reduce_sum you could get away with tf.linalg.matmul but you would have to be cognizant of the resulting shape. So, I guess, you better use the reduce_sum.


Lastly, you need to compare against the threshold:

    # Is d greater than the threshold?
    res = ?

Output:

res
<tf.Tensor: shape=(), dtype=bool, numpy=True>

# since 0.8422111 > 0.7

Cheers

1 Like

Hello @arvyzukai

First of all thank you for all the support you have provided.

I cleared the last assignment of this NLP course 3 but I don’t think It was easy to clear without your hints and guidance as the assignment lacks proper instructions, forget about the hint part.

In fact whatever instructions were given, it confused me more like it stated to use dot product and the very next sentence it states to use tf.math.reduce_sum.

It didn’t given any instruction on how to extract v1 and v2 from v1v2 for comparison purposes which was the biggest block to clear the task.

Honestly I really feel a course update need to be done carefully.

I extracted v1 and v2 with instruction help you mentioned with v1.shape and v2.shape which mentions to directly mention the vector instead of using any calculation or integer usage.

Felt like a simple codes was left to make it complicated with improper instructions and lack of proper instructions.

Hope you convey this to the L.T of NLP specialisation.

Thank you once again for all the support.

Regards
DP

2 Likes

Hi @Deepti_Prasad

I’m glad you finally won this battle. :muscle:

I understand what you say and I will try to offer some adjustments.

Cheers

1 Like