forked from rasbt/machine-learning-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch16-part1-self-attention.py
375 lines (152 loc) · 7.25 KB
/
ch16-part1-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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# coding: utf-8
import sys
from python_environment_check import check_packages
import torch
import torch.nn.functional as F
# # Machine Learning with PyTorch and Scikit-Learn
# # -- Code Examples
# ## Package version checks
# Add folder to path in order to load from the check_packages.py script:
sys.path.insert(0, '..')
# Check recommended package versions:
d = {
'torch': '1.9.0',
}
check_packages(d)
# # Chapter 16: Transformers – Improving Natural Language Processing with Attention Mechanisms (Part 1/3)
# **Outline**
#
# - [Adding an attention mechanism to RNNs](#Adding-an-attention-mechanism-to-RNNs)
# - [Attention helps RNNs with accessing information](#Attention-helps-RNNs-with-accessing-information)
# - [The original attention mechanism for RNNs](#The-original-attention-mechanism-for-RNNs)
# - [Processing the inputs using a bidirectional RNN](#Processing-the-inputs-using-a-bidirectional-RNN)
# - [Generating outputs from context vectors](#Generating-outputs-from-context-vectors)
# - [Computing the attention weights](#Computing-the-attention-weights)
# - [Introducing the self-attention mechanism](#Introducing-the-self-attention-mechanism)
# - [Starting with a basic form of self-attention](#Starting-with-a-basic-form-of-self-attention)
# - [Parameterizing the self-attention mechanism: scaled dot-product attention](#Parameterizing-the-self-attention-mechanism-scaled-dot-product-attention)
# - [Attention is all we need: introducing the original transformer architecture](#Attention-is-all-we-need-introducing-the-original-transformer-architecture)
# - [Encoding context embeddings via multi-head attention](#Encoding-context-embeddings-via-multi-head-attention)
# - [Learning a language model: decoder and masked multi-head attention](#Learning-a-language-model-decoder-and-masked-multi-head-attention)
# - [Implementation details: positional encodings and layer normalization](#Implementation-details-positional-encodings-and-layer-normalization)
# ## Adding an attention mechanism to RNNs
# ### Attention helps RNNs with accessing information
# ### The original attention mechanism for RNNs
# ### Processing the inputs using a bidirectional RNN
# ### Generating outputs from context vectors
# ### Computing the attention weights
# ## Introducing the self-attention mechanism
# ### Starting with a basic form of self-attention
# - Assume we have an input sentence that we encoded via a dictionary, which maps the words to integers as discussed in the RNN chapter:
# input sequence / sentence:
# "Can you help me to translate this sentence"
sentence = torch.tensor(
[0, # can
7, # you
1, # help
2, # me
5, # to
6, # translate
4, # this
3] # sentence
)
sentence
# - Next, assume we have an embedding of the words, i.e., the words are represented as real vectors.
# - Since we have 8 words, there will be 8 vectors. Each vector is 16-dimensional:
torch.manual_seed(123)
embed = torch.nn.Embedding(10, 16)
embedded_sentence = embed(sentence).detach()
embedded_sentence.shape
# - The goal is to compute the context vectors $\boldsymbol{z}^{(i)}=\sum_{j=1}^{T} \alpha_{i j} \boldsymbol{x}^{(j)}$, which involve attention weights $\alpha_{i j}$.
# - In turn, the attention weights $\alpha_{i j}$ involve the $\omega_{i j}$ values
# - Let's start with the $\omega_{i j}$'s first, which are computed as dot-products:
#
# $$\omega_{i j}=\boldsymbol{x}^{(i)^{\top}} \boldsymbol{x}^{(j)}$$
#
#
omega = torch.empty(8, 8)
for i, x_i in enumerate(embedded_sentence):
for j, x_j in enumerate(embedded_sentence):
omega[i, j] = torch.dot(x_i, x_j)
# - Actually, let's compute this more efficiently by replacing the nested for-loops with a matrix multiplication:
omega_mat = embedded_sentence.matmul(embedded_sentence.T)
torch.allclose(omega_mat, omega)
# - Next, let's compute the attention weights by normalizing the "omega" values so they sum to 1
#
# $$\alpha_{i j}=\frac{\exp \left(\omega_{i j}\right)}{\sum_{j=1}^{T} \exp \left(\omega_{i j}\right)}=\operatorname{softmax}\left(\left[\omega_{i j}\right]_{j=1 \ldots T}\right)$$
#
# $$\sum_{j=1}^{T} \alpha_{i j}=1$$
attention_weights = F.softmax(omega, dim=1)
attention_weights.shape
# - We can conform that the columns sum up to one:
attention_weights.sum(dim=1)
# - Now that we have the attention weights, we can compute the context vectors $\boldsymbol{z}^{(i)}=\sum_{j=1}^{T} \alpha_{i j} \boldsymbol{x}^{(j)}$, which involve attention weights $\alpha_{i j}$
# - For instance, to compute the context-vector of the 2nd input element (the element at index 1), we can perform the following computation:
x_2 = embedded_sentence[1, :]
context_vec_2 = torch.zeros(x_2.shape)
for j in range(8):
x_j = embedded_sentence[j, :]
context_vec_2 += attention_weights[1, j] * x_j
context_vec_2
# - Or, more effiently, using linear algebra and matrix multiplication:
context_vectors = torch.matmul(
attention_weights, embedded_sentence)
torch.allclose(context_vec_2, context_vectors[1])
# ### Parameterizing the self-attention mechanism: scaled dot-product attention
torch.manual_seed(123)
d = embedded_sentence.shape[1]
U_query = torch.rand(d, d)
U_key = torch.rand(d, d)
U_value = torch.rand(d, d)
x_2 = embedded_sentence[1]
query_2 = U_query.matmul(x_2)
key_2 = U_key.matmul(x_2)
value_2 = U_value.matmul(x_2)
keys = U_key.matmul(embedded_sentence.T).T
torch.allclose(key_2, keys[1])
values = U_value.matmul(embedded_sentence.T).T
torch.allclose(value_2, values[1])
omega_23 = query_2.dot(keys[2])
omega_23
omega_2 = query_2.matmul(keys.T)
omega_2
attention_weights_2 = F.softmax(omega_2 / d**0.5, dim=0)
attention_weights_2
#context_vector_2nd = torch.zeros(values[1, :].shape)
#for j in range(8):
# context_vector_2nd += attention_weights_2[j] * values[j, :]
#context_vector_2nd
context_vector_2 = attention_weights_2.matmul(values)
context_vector_2
# ## Attention is all we need: introducing the original transformer architecture
# ### Encoding context embeddings via multi-head attention
torch.manual_seed(123)
d = embedded_sentence.shape[1]
one_U_query = torch.rand(d, d)
h = 8
multihead_U_query = torch.rand(h, d, d)
multihead_U_key = torch.rand(h, d, d)
multihead_U_value = torch.rand(h, d, d)
multihead_query_2 = multihead_U_query.matmul(x_2)
multihead_query_2.shape
multihead_key_2 = multihead_U_key.matmul(x_2)
multihead_value_2 = multihead_U_value.matmul(x_2)
multihead_key_2[2]
stacked_inputs = embedded_sentence.T.repeat(8, 1, 1)
stacked_inputs.shape
multihead_keys = torch.bmm(multihead_U_key, stacked_inputs)
multihead_keys.shape
multihead_keys = multihead_keys.permute(0, 2, 1)
multihead_keys.shape
multihead_keys[2, 1] # index: [2nd attention head, 2nd key]
multihead_values = torch.matmul(multihead_U_value, stacked_inputs)
multihead_values = multihead_values.permute(0, 2, 1)
multihead_z_2 = torch.rand(8, 16)
linear = torch.nn.Linear(8*16, 16)
context_vector_2 = linear(multihead_z_2.flatten())
context_vector_2.shape
# ### Learning a language model: decoder and masked multi-head attention
# ### Implementation details: positional encodings and layer normalization
# ---
#
# Readers may ignore the next cell.