Answered step by step
Verified Expert Solution
Link Copied!

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

4.2 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, Q1_test, Q2_test and y_test, 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 y_test - 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 04
Instructions
Loop through the incoming data in batch_size 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 v1, v2 using the model output,
for each element of the batch - compute the cosine similarity of each pair of entries, v1[j],v2[j]- determine if d > threshold - increment accuracy if that result matches the expected results (y_test[j]) 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 tf.math.confusion_matrix function.
# GRADED FUNCTION: classify
def classify(test_Q1, test_Q2, y_test, threshold, model, batch_size=64, verbose=True):
"""Function to test the accuracy of the model.
Args:
test_Q1(numpy.ndarray): Array of Q1 questions. Each element of the array would be a string.
test_Q2(numpy.ndarray): Array of Q2 questions. Each element of the array would be a string.
y_test (numpy.ndarray): Array of actual target.
threshold (float): Desired threshold
model (tensorflow.Keras.Model): The Siamese model.
batch_size (int, optional): Size of the batches. Defaults to 64.
Returns:
float: Accuracy of the model
numpy.array: confusion matrix
"""
y_pred =[]
test_gen = tf.data.Dataset.from_tensor_slices(((test_Q1, test_Q2),None)).batch(batch_size=batch_size)
### START CODE HERE ###
pred = None
_, n_feat = None
v1= None
v2= None
# Compute the cosine similarity. Using `tf.math.reduce_sum`.
# Don't forget to use the appropriate axis argument.
d = None
# Check if d>threshold to make predictions
y_pred = tf.cast(None, tf.float64)
# take the average of correct predictions to get the accuracy
accuracy = None
# compute the confusion matrix using `tf.math.confusion_matrix`
cm = None
### END CODE HERE ###
return accuracy, cm

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago