I am grading an exam and I created this question: --- Attention Mechanism in…
- User
I am grading an exam and I created this question:
Attention Mechanism in Transformers a-b) [10 Points]¶
You are provided with three weight matrices: WQ , WK, and WV
.
Additionally, you have an embedding matrix input_embeddings of dimensions sequence_length ×
embedding_dimension.
The matrices and a helper function are given in the next cell. In [18]:
1import numpy as np 2 3 4def softmax(x, axis=None):
5 # Compute exponentials 6 exp_x = np.exp(x) 7
8 # Compute softmax 9 softmax_values = exp_x / np.sum(exp_x, axis=axis, keepdims=True) 10
11 return softmax_values 12 13 14# Weight matrices of size 6x6 15 16wq = np.array( 17 [[0.19, 0.03, 0.82, 0.15, 0.91, 0.42], 18 [0.48, 0.28, 0.26, 0.59, 0.72, 0.74], 19 [0.2 , 0.72, 0.14, 0.12, 0.41, 0.08], 20 [0.99, 0.34, 0.91, 0.38, 0.83, 0.86], 21 [0.44, 0.05, 0.99, 0.49, 0.44, 0.9 ], 22 [0.13, 0.86, 0.59, 0.98, 0.68, 0.98]] 23) 24 25wk = np.array( 26 [[0.37, 0.99, 0.14, 0.89, 0.03, 0.64], 27 [0.87, 0.13, 0.27, 0.65, 0.74, 0.88], 28 [0.25, 0.65, 0.13, 0.94, 0.1 , 0.43], 29 [0.73, 0.29, 0.97, 0.21, 0.88, 0.25], 30 [0.69, 0.53, 0.08, 0.33, 0.49, 0.52], 31 [0.48, 0.35, 0.35, 0.18, 0.26, 0.6 ]] 32) 33 34wv = np.array( 35 [[0.25, 0.58, 0.45, 0.57, 0.13, 0.13], 36 [0.67, 0.22, 0.93, 0.73, 0.37, 0.34], 37 [0.63, 0.79, 0.64, 0.36, 0.29, 0.55], 38 [0.93, 0.14, 0.6 , 0.56, 0.65, 0.12], 39 [0.48, 0.07, 0.36, 0.08, 0.58, 0.62], 40 [0.82, 0.98, 0.57, 0.39, 0.6 , 0.09]] 41) 42 43 44 45# Input embeddings (5 tokens with dimensionality of 6 each) 46input_embeddings = np.array( 47 [[0.24, 0.84, 0.78, 0.64, 0.42, 0.78], 48 [0.98, 0.55, 0.79, 0. , 0.27, 0.54], 49 [0.77, 0.15, 0.34, 0.34, 0.99, 0.02], 50 [0.87, 0.91, 0.83, 0.67, 0.82, 0.99], 51 [0.7 , 0.48, 0.67, 0.51, 0.29, 0.69]] 52)Attention Mechanism in Transformers a) [6 Points]¶
Calculate the attention matrix A from the weight matrices and your input embeddings.
Please produce grading rubrics for this question
Assistant
Here is a suggested grading rubric for the question "Attention Mechanism in Transformers a) [6 Points]":
[6 Points] Calculation of the Attention Matrix:
- [2 Points] Student correctly multiplies the input embeddings with WQ to obtain Q (queries).
- [2 Points] Student correctly multiplies the input embeddings with WK to obtain K (keys).
- [2 Points] Student correctly multiplies the input embeddings with WV to obtain V (values).
[4 Points] Attention Matrix Computation:
- [2 Points] Student correctly computes the attention scores by performing a dot product between Q and K^T.
- [2 Points] Student applies the softmax function along the appropriate axis to obtain the attention probabilities.
[3 Points] Final Attention Matrix:
- [3 Points] Student correctly computes the final attention matrix A by performing a matrix multiplication between the attention probabilities and the value matrix V.
This rubric ensures that the student understands the mechanism of calculating the attention matrix, applying the softmax function, and computing the final attention output.