How to get weights indicating importance of word of vocabulary in MultiheadAttention?

username: sbhatt
cooked: <p>Hello,</p>
<p>Can you please help with how to get vocabulary x vocabulary weight matrix from multi headed attention?</p>
<p>I have timeseries data and I am using attention to predict the next element/token in the sequence.<br>
E.g. input sequence: T1 - T2 - T3 - T4 -  T3 - T5, where each Ti is a token. I have n such tokens in the vocabulary. Each token is represented with 56 dimensions. The attention layer takes such a sequence as an input and returns attention weights and attention outputs (as shown <a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/activation.html#MultiheadAttention" rel="nofollow noopener">here</a>).</p>
<p>How do I get the nxn i.e. vocabulary_size x vocabulary_size (or embedding x embedding) matrix as an output? Each entry in the matrix indicates the importance of the particular vocabulary element compared to other vocabulary elements?</p>
<p>Specifically, the model is as follows,</p>
<p>model summary BuildModel(<br>
(input_multihead_attn): MultiheadAttention(<br>
(out_proj): Linear(in_features=56, out_features=56, bias=True)<br>
)<br>
(fully_connected): Linear(in_features=56, out_features=28, bias=True)<br>
)</p>
<p>Multiheaded attention layer returns batch_size x sequence_length x sequence_length  (length of S) weight matrix.<br>
The sequence_length x sequence_length matrix is generated from a batch of sequences. Hence, first position in the sequence may correspond to multiple tokens (e.g. T1 for sequence 1 and T2 for sequence 2). I am wondering, how do I generate n x n matrix (n is the total number of tokens) from this attention weight matrix?</p>
<p>Thank You,</p>

username: vainaijr
cooked: <p>I think the whole concept of nn.MultiheadAttention is to solve the problem that meaning of a pattern depends on what all patterns it is surrounded by.</p>
<p>for example, in the case of text,<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/8/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6.png" data-download-href="https://discuss.pytorch.org/uploads/default/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6_2_690x285.png" alt="image" data-base62-sha1="xaVFot0HYCrzHUHG5g8EsmwOgjY" width="690" height="285" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6_2_690x285.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/8/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6_2_1035x427.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/8/e8840a40fd7e33fffc7e0467fb3a36fc5a8d4fc6.png 2x" data-dominant-color="F0F8FA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1104×456 80.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>here, the representation of the word ‘it’ depends on what all words it is surrounded by, in the left case, ‘it’ refers to ‘animal’, in the right case, ‘it’ refers to ‘street’.</p>
<p>so, the representation of ‘it’ is not one fixed embedding.</p>
<p>therefore, when we pass a q, k, v to nn.MultiheadAttention, we would get a matrix, which indicates how similar our query is to each of the values, or how much the representation of our query depends on each of the values.</p>
<p>Another thing is that this concept extends to images also, or in general to a pattern of information, for example,<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/0/304fecc6c3076e4524aa45d78370cf15ffe71ac0.png" alt="image" data-base62-sha1="6To9J9jfXHiSvgh8mPhI9dMR0Ag" width="300" height="269"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/f/df34441c4e311a0808cd24f0f32dd4733229724f.png" alt="image" data-base62-sha1="vQyseDu3YzsrUWsyaE9LmVkp9fN" width="181" height="279"></p>
<p>In the first case, we have a pattern of a green dot, in the second case, it is a pattern of a green traffic light, the pattern did not change, (assume that green color is same in both images), but what it represents changed based on its surrounding.<br>
maybe this is not the best example, you can come up with a better example, but the idea of nn.MultiheadAttention applies to any pattern of information.</p>