Question: Create a tensorflow.data.Dataset from your two questions. Again, labels are not important, so you simply write None ( this is completed for you ) ,

Create a tensorflow.data.Dataset from your two questions. Again, labels are not important, so you simply write None (this is completed for you),
use the trained model output to extract v1, v2(similar to Exercise 04),
compute the cosine similarity (dot product) of v1, v2(similarly to Exercise 04),
compute res (the decision if questions are duplicate or not) by comparing d to the threshold.
Replace all "None" instances with correct vaiables!
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 Programming Questions!