Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Natural Language Processing with Sequence Models/Week 4 - Siamese Networks/C3_W4_lecture_notebook_accuracy.ipynb
Views: 13373
Evaluate a Siamese model: Ungraded Lecture Notebook
Inspecting the necessary elements
In this lecture notebook you will learn how to evaluate a Siamese model using the accuracy metric. Because there are many steps before evaluating a Siamese network (as you will see in this week's assignment) the necessary elements and variables are replicated here using real data from the assignment:
q1
: vector with dimension(batch_size X max_length)
containing first questions to compare in the test set.q2
: vector with dimension(batch_size X max_length)
containing second questions to compare in the test set.
Notice that for each pair of vectors within a batch , is associated to .
y_test
: 1 if and are duplicates, 0 otherwise.v1
: output vector from the model's prediction associated with the first questions.v2
: output vector from the model's prediction associated with the second questions.
You can inspect each one of these variables by running the following cells:
Notice those 1s on the right-hand side?
Hope you remember that the value of 1
was used for padding.
Calculating the accuracy
You will calculate the accuracy by iterating over the test set and checking if the model predicts right or wrong.
The first step is to set the accuracy to zero:
You will also need the batch size
and the threshold
that determines if two questions are the same or not.
Note :A higher threshold means that only very similar questions will be considered as the same question.
In the assignment you will iterate over multiple batches of data but since this is a simplified version only one batch is provided.
Note: Be careful with the indices when slicing the test data in the assignment!
The process is pretty straightforward:
Iterate over each one of the elements in the batch
Compute the cosine similarity between the predictions
For computing the cosine similarity, the two output vectors should have been normalized using L2 normalization meaning their magnitude will be 1. This has been taken care off by the Siamese network you will build in the assignment. Hence the cosine similarity here is just dot product between two vectors. You can check by implementing the usual cosine similarity formula and check if this holds or not.
Determine if this value is greater than the threshold (If it is, consider the two questions as the same and return 1 else 0)
Compare against the actual target and if the prediction matches, add 1 to the accuracy (increment the correct prediction counter)
Divide the accuracy by the number of processed elements
Congratulations on finishing this lecture notebook!
Now you should have a clearer understanding of how to evaluate your Siamese language models using the accuracy metric.
Keep it up!