Skip to content

Commit 81809d9

Browse files
committed
master: attention maths.
1 parent d2515bd commit 81809d9

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Attention Maths
2+
3+
```buildoutcfg
4+
Q: Queries (embeddings of German words)
5+
K: Keys (embeddings of English words)
6+
V: Values
7+
D: Dimensionality of word embeddings
8+
Lq: no. of. queries
9+
Lk: no. of keys
10+
11+
```
12+
13+
1. Input to attention: Q, K, V Often Vs are same as Ks.
14+
15+
2. dim[Q] = [Lq, D];
16+
3. dim[K] = [Lk, D];
17+
4. dim[V] = [Lk, D]
18+
19+
<img src="../images/15.step - 1.png"></img> <br><br>
20+
21+
4. A query Q, will assign each key K, a probability that key K is a match for Q. Similarity is measured by taking dot
22+
product of vectors. So Q and K are similar iff `Q dot K` is large.
23+
<img src="../images/16.step - 2.png"></img> <br><br>
24+
25+
5. To make attention more focused on best matching keys, use softmax `(softmax(Q.KTranspose))`. Hence, we now calculate a matrix of Q-K probabailities
26+
often called *attention weights*. The shape of this matrix is `[Lq, Lk]`.
27+
28+
6. In the final step, we take values and get weighted sum of values, weighting each value Vi by the probability that the key Ki matches the query.
29+
30+
7. Finally the attention mechanism calculates the dynamic or alignment weights representing the relative importance of the inputs in this sequence.
31+
<img src="../images/17.step - 3.png"></img> <br><br>
32+
33+
8. Multiplying alignment weights with input sequence (values), will then weight the sequence.
34+
<img src="../images/18.step - 4.png"></img> <br><br>
35+
36+
9. A single context vector can then be calculated using the sum of weighted vectors.
37+
<img src="../images/19.step - 5.png"></img> <br><br>

Chapter-wise code/Code - PyTorch/7. Attention Models/2. Neural Text Summarization/1. Transformer Models/Dot Product Attention.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ First, the word *I* is embedded, to obtain a vector representation that holds co
2929
7. Finally, multiplying these probabilities with the values, you will then get a weighted sequence, which is the attention results itself.<br><br>
3030
<img src="../images/14. step - 8.png" width="50%"></img><br>
3131

32+
## Next Up
33+
34+
Now that we know about attention, next we will learn about *attention maths*. You can find the readme file for the same here.
35+
3236

125 KB
Loading
250 KB
Loading
254 KB
Loading
256 KB
Loading
239 KB
Loading

0 commit comments

Comments
 (0)