Understanding Multi-Headed Attention: A Simple Example
Now we'll see some of the power of multi-headed attention. We'll consider a simple version of multi-headed attention which is identical to single-headed self-attention as we've presented it in this homework, except two query vectors (q1 and q2) are defined, which leads to a pair of vectors (c1 and c2), each the output of single-headed attention given its respective query vector. The final output of the multi-headed attention is their average, 1/2(c1+c2). As in question 1(c), consider a set of key vectors {k1, ..., kn} that are randomly sampled, ki ∼ N(µi, Σi), where the means µi are known to you, but the covariances Σi are unknown. Also as before, assume that the means µi are mutually orthogonal; µ⊤i µj = 0 if i ≠ j, and unit norm, ∥µi∥ = 1.
i. (1 point) Assume that the covariance matrices are Σi = αI, for vanishingly small α. Design q1 and q2 such that c is approximately equal to 1/2(va + vb).
ii. (2 points) Assume that the covariance matrices are Σa = αI + 1/2(µaµ⊤a) for vanishingly small α, and Σi = αI for all i ≠ a. Take the query vectors q1 and q2 that you designed in part i. What, qualitatively, do you expect the output c to look like across different samples of the key vectors? Please briefly explain why. You can ignore cases in which ka⊤qi < 0.
i. We can design q1 and q2 as follows: q1 = va + εµa q2 = vb + εµb where ε is a small constant. Then, using the formula for single-headed attention, we can compute c1 and c2 as follows: c1 = ∑i αi(va⊤ki)ki c2 = ∑i αi(vb⊤ki)ki Taking the average of c1 and c2, we get: c = 1/2(c1 + c2) = ∑i αi(va + vb)⊤ki ki = (va + vb)⊤∑i αiki ki = (va + vb)⊤(αI) = α(va + vb) Thus, c is approximately equal to 1/2(va + vb), as desired.
ii. With the covariance matrices as specified, using the same query vectors as in part i, we can compute c1 and c2 as follows: c1 = ∑i αi(va⊤ki)ki + α(va⊤µa)µa c2 = ∑i αi(vb⊤ki)ki + α(vb⊤µb)µb Taking the average of c1 and c2, we get: c = 1/2(c1 + c2) = ∑i αi(va + vb)⊤ki ki + 1/2α(va + vb)⊤µaµa + µbµb⊤ = (va + vb)⊤(αI + 1/2(µaµ⊤a))(αI)−1(αI + 1/2(µaµ⊤a))−1(∑i ki ki) + 1/2(µaµ⊤a) + µbµb⊤ = (va + vb)⊤(αI + 1/2(µaµ⊤a))−1(∑i ki ki) + 1/2(µaµ⊤a) + µbµb⊤ Ignoring the cases where ka⊤qi < 0, we expect the output c to be a weighted sum of the key vectors ki, with the weights determined by the Mahalanobis distance between the query vectors and the key vectors. Specifically, the weights will be higher for key vectors that are closer to the query vectors in the Mahalanobis sense, which takes into account the covariance matrix. Since the covariance matrix for key vector ka is different from the covariance matrices for all other key vectors, we expect that ka will have a disproportionately high weight in the sum. Additionally, since the mean vectors µa and µb are orthogonal, we expect that the term µaµ⊤a will dominate the sum, leading to a final output that is close to va.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/oOHD 著作权归作者所有。请勿转载和采集!