Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part 4 : Evaluation 4 . 1 Evaluating your siamese network In this section you will learn how to evaluate a Siamese network. You will
Part : Evaluation
Evaluating your siamese network
In this section you will learn how to evaluate a Siamese network. You will start by loading a pretrained model, and then you will use it to predict. For the prediction you will need to take the output of your model and compute the cosine loss between each pair of questions.
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 so This time you don't need the labels, so you can just replace them by None,
compute v v using the model output,
for each element of the batch compute the cosine similarity of each pair of entries, vjvj determine if d threshold increment accuracy if that result matches the expected results ytestj Instead 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 classifytestQ testQ ytest, threshold, model, batchsize verboseTrue:
Function to test the accuracy of the model.
Args:
testQnumpyndarray: Array of Q questions. Each element of the array would be a string.
testQnumpyndarray: Array of Q questions. 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 int optional: Size of the batches. Defaults to
Returns:
float: Accuracy of the model
numpy.array: confusion matrix
ypred
testgen tfdata.Dataset.fromtensorslicestestQ testQNonebatchbatchsizebatchsize
### START CODE HERE ###
pred None
nfeat None
v None
v None
# 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 tfcastNone tffloat
# 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