Answered step by step
Verified Expert Solution
Question
1 Approved Answer
4 . 2 Classify To determine the accuracy of the model, you will use the test set that was configured earlier. While in training you
Classify
To determine the accuracy of the model, you will use the test set that was configured earlier. While in training you used only positive examples, the test data, Qtest, Qtest and ytest, is set up as pairs of questions, some of which are duplicates and some are not. This routine will run all the test question pairs through the model, compute the cosine similarity of each pair, threshold it and compare the result to ytest the correct response from the data set. The results are accumulated to produce an accuracy; the confusion matrix is also computed to have a better understanding of the errors.
Exercise
Instructions
Loop through the incoming data in batchsize chunks, you will again define a tensorflow.data.Dataset to do soThis time you don't need the labels, so you can just replace them by None,
compute vvusing the model output,
for each element of the batch compute the cosine similarity of each pair of entries, vjvjdetermine if d threshold increment accuracy if that result matches the expected results ytestjInstead of running a for loop, you will vectorize all these operations to make things more efficient,
compute the final accuracy and confusion matrix and return. For the confusion matrix you can use the tfmath.confusionmatrix function.
# GRADED FUNCTION: classify
def classifytestQtestQytest, threshold, model, batchsizeverboseTrue:
Function to test the accuracy of the model.
Args:
testQnumpyndarray: Array of Qquestions Each element of the array would be a string.
testQnumpyndarray: Array of Qquestions Each element of the array would be a string.
ytest numpyndarray: Array of actual target.
threshold float: Desired threshold
model tensorflowKeras.Model: The Siamese model.
batchsize intoptional: Size of the batches. Defaults to
Returns:
float: Accuracy of the model
numpy.array: confusion matrix
ypred
testgen tfdata.Dataset.fromtensorslicestestQtestQNonebatchbatchsizebatchsize
### START CODE HERE ###
pred None
nfeat None
vNone
vNone
# Compute the cosine similarity. Using tfmath.reducesum
# Don't forget to use the appropriate axis argument.
d None
# Check if dthreshold to make predictions
ypred tfcastNonetffloat
# take the average of correct predictions to get the accuracy
accuracy None
# compute the confusion matrix using tfmath.confusionmatrix
cm None
### END CODE HERE ###
return accuracy, cm
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started