Question: Part 5 : Testing with your own questions In this final section you will test the model with your own questions. You will write a

Part 5: Testing with your own questions
In this final section you will test the model with your own questions. You will write a function predict which takes two questions as input and returns True or False depending on whether the question pair is a duplicate or not.
Write a function predict that takes in two questions, the threshold and the model, and returns whether the questions are duplicates (True) or not duplicates (False) given a similarity threshold.
Exercise 05
Instructions:
Create a tensorflow.data.Dataset from your two questions. Again, labels are not important, so you simply write None
use the trained model output to create v1, v2
compute the cosine similarity (dot product) of v1, v2
compute res by comparing d to the threshold
# GRADED FUNCTION: predict
def predict(question1, question2, threshold, model, verbose=False):
"""Function for predicting if two questions are duplicates.
Args:
question1(str): First question.
question2(str): Second question.
threshold (float): Desired threshold.
model (tensorflow.keras.Model): The Siamese model.
data_generator (function): Data generator function. Defaults to data_generator.
verbose (bool, optional): If the results should be printed out. Defaults to False.
Returns:
bool: True if the questions are duplicates, False otherwise.
"""
generator = tf.data.Dataset.from_tensor_slices((([question1],[question2]),None)).batch(batch_size=1)
### START CODE HERE ###
# 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 ###
if(verbose):
print("Q1=", question1,"
Q2=", question2)
print("d =", d.numpy())
print("res =", res.numpy())
return res.numpy()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!