-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathself_attention.py
68 lines (46 loc) · 1.71 KB
/
self_attention.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm as tqdm
import torch
import numpy as np
## inputs key, query, abnd value vector which are the encoder inputs * k, q, v weights from prev training (MLP for key, mLP for value, MLP for query)
def self_attention(key, query, value):
# scores = dotprod of key + query
# b here stands for batch, l = length, k= num pixels, num words etc)
scores = torch.einsum("bkl,bql->bqk", key, query)
# note that the dimensions of key query and value
dimensions_k = key.dim()
# this helps to create a more stable gradient (you could probably normalize via other constants?)
scores = scores / np.sqrt(dimensions_k)
# turn scores into probabilities from 0,1
attention = torch.softmax(scores)
# dotprod of value vector, and attention vector
attended_values = torch.einsum("bdl,bad->bal", value, attention)
return attended_values
"""Testing"""
batch_size = 1
d_model = 3
length = 4
def example_tensor(shape):
rng = torch.arange(start=0, end=np.product(shape), dtype=torch.float32)
return torch.reshape(rng, shape)
value = example_tensor([batch_size, d_model, length])
print(f"value: \n{value}\n")
key = torch.zeros([batch_size, d_model, length])
key[0,0,0] = 100.
key[0,1,1] = 100.
key[0,2,2] = 100.
print(f"key: \n{key}\n")
query = torch.zeros([batch_size, d_model, length])
query[0,0,1] = 100.
query[0,1,0] = 100.
query[0,2,2] = 100.
print(f"query: \n{query}\n")
import numpy.testing as npt
attended_values = self_attention(query, key, value)
print(attended_values)
npt.assert_equal(attended_values.numpy(), np.array(
[[[4., 5., 6., 7.],
[0., 1., 2., 3.],
[8., 9., 10., 11.]]]))