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 : 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
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 v v
compute the cosine similarity dot product of v v
compute res by comparing d to the threshold
# GRADED FUNCTION: predict
def predictquestion question threshold, model, verboseFalse:
Function for predicting if two questions are duplicates.
Args:
questionstr: First question.
questionstr: Second question.
threshold float: Desired threshold.
model tensorflowkeras.Model: The Siamese model.
datagenerator function: Data generator function. Defaults to datagenerator.
verbose bool optional: If the results should be printed out. Defaults to False.
Returns:
bool: True if the questions are duplicates, False otherwise.
generator tfdata.Dataset.fromtensorslicesquestionquestionNonebatchbatchsize
### START CODE HERE ###
# Call the predict method of your model and save the output into vv
vv None
# Extract v and v from the model output
v None
v None
# Take the dot product to compute cos similarity of each pair of entries, v v
# Since v and v are both vectors, use the function tfmath.reducesum instead of tflinalg.matmul
d None
# Is d greater than the threshold?
res None
### END CODE HERE ###
ifverbose:
printQ question
Q question
printd dnumpy
printres res.numpy
return res.numpy
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
