-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformers_implementation.py
292 lines (215 loc) · 10 KB
/
transformers_implementation.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
# -*- coding: utf-8 -*-
"""Transformers_implementation.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kDyaEZjLQGrmVJVHwFCCLprGvhe6xzpK
"""
# Attention is all you need paper - https://arxiv.org/pdf/1706.03762.pdf
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optimizer
import math
import numpy as np
class ScaledDotProductAtt(nn.Module):
def __init__(self, dropout=0.1): #dropout as input for avoiding overfitting and improve generalization
super(ScaledDotProductAtt, self).__init__()
self.dropout = nn.Dropout(dropout)
def forward(self, query, key, value, mask=None): #optional mask
attScores = torch.matmul(query, key.transpose(-2, -1)) / np.sqrt(key.size(-1))
if mask is not None: #Mask usually in the decoder
attScores = attScores.masked_fill(mask == 0, -1e10) #Avoiding mask to be 0 to avoid inestabilities, low number instead
attention = F.softmax(attScores, dim = -1)#last axis
attention = self.dropout(attention) #dropping 30% of the scores
return torch.matmul(attention, value), attention #added attention to get what the model is attending to
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, nhead, dropout=0.1):
super(MultiHeadAttention, self).__init__()
self.d_model = d_model # model dimension
self.nhead = nhead # number of "heads"
self.d_k = d_model // nhead # key dimension
self.d_v = d_model // nhead # value dimension
# Linearity for inputs
self.linear_q = nn.Linear(d_model, d_model)
self.linear_k = nn.Linear(d_model, d_model)
self.linear_v = nn.Linear(d_model, d_model)
self.scaledDotProductAttention = ScaledDotProductAtt(dropout)
self.linearLayer = nn.Linear(d_model, d_model) # Linear layer at output
self.dropout = nn.Dropout(dropout)
def forward(self, query, key, value, mask=None, key_padding_mask=None):
batchSize = query.size(0)
query = self.linear_q(query).view(batchSize, -1, self.nhead, self.d_k).transpose(1,2)
key = self.linear_q(key).view(batchSize, -1, self.nhead, self.d_k).transpose(1,2)
value = self.linear_q(value).view(batchSize, -1, self.nhead, self.d_v).transpose(1,2)
output, attScores = self.scaledDotProductAttention(query, key, value)
outputConcat = output.transpose(1,2).contiguous().view(batchSize, -1, self.d_model)
outputConcat = self.linearLayer(outputConcat)
return self.dropout(outputConcat)
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, maxLength=100):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(dropout)
pe = torch.zeros(maxLength, d_model)
position = torch.arange(0, maxLength, dtype=torch.float).unsqueeze(1)
divisionTerm = torch.exp(torch.arange(0, d_model, 2).float() * -(torch.log(torch.tensor(10000.0)) / d_model))
pe[:, 0::2] = torch.sin(position * divisionTerm)
pe[:, 1::2] = torch.cos(position * divisionTerm)
pe = pe.unsqueeze(0).transpose(0,1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class FeedForward(nn.Module):
def __init__(self, d_model, d_mlp=1024, dropout=0.1):
super(FeedForward, self).__init__()
self.linear_1 = nn.Linear(d_model, d_mlp)
self.dropout = nn.Dropout(dropout)
self.linear_2 = nn.Linear(d_mlp, d_model)
def forward(self, x):
x = self.linear_1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.linear_2(x)
return x
class NormalizationLayer(nn.Module):
def __init__(self, d_model, epsilon=1e-5):
super(NormalizationLayer, self).__init__()
self.gamma = nn.Parameter(torch.ones(d_model))
self.beta = nn.Parameter(torch.zeros(d_model))
self.epsilon = epsilon
def forward(self, x):
mean = x.mean(dim=1, keepdim=True)
std = x.std(dim=-1, keepdim=True)
x = (x - mean) / (std + self.epsilon)
x = self.gamma * x + self.beta
return x
class Encoder(nn.Module):
def __init__(self, d_model, nhead, d_mlp, dropout=0.1):
super(Encoder, self).__init__()
self.multiHeadAttention = MultiHeadAttention(d_model, nhead, dropout)
self.feedforward = FeedForward(d_model, d_mlp, dropout)
self.normLayer1 = NormalizationLayer(d_model)
self.normLayer2 = NormalizationLayer(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
def forward(self, x, src_mask=None, src_key_padding_mask=None, is_causal=False):
x2 = self.multiHeadAttention(x, x, x, mask=src_mask, key_padding_mask=src_key_padding_mask)[0]
x2 = self.normLayer1(x2)
x = x + self.dropout1(x2)
x2 = self.feedforward(x)
x2 = self.normLayer2(x2)
x = x + self.dropout2(x2)
return x
class Decoder(nn.Module):
def __init__(self, d_model, nhead, d_mlp, dropout=0.1):
super(Decoder, self).__init__()
self.maskedMultiHeadAttention = MultiHeadAttention(d_model, nhead, dropout)
self.multiHeadAttention = MultiHeadAttention(d_model, nhead, dropout)
self.feedforward = FeedForward(d_model, d_mlp, dropout)
self.normLayer1 = NormalizationLayer(d_model)
self.normLayer2 = NormalizationLayer(d_model)
self.normLayer3 = NormalizationLayer(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
def forward(self, target, memory, tgt_mask=None, memory_mask=None, tgt_key_padding_mask=None, memory_key_padding_mask=None):
target2 = self.maskedMultiHeadAttention(target, target, target, mask=tgt_mask, key_padding_mask=tgt_key_padding_mask)[0]
target2 = self.normLayer1(target2)
target = target + self.dropout1(target2)
target2 = self.multiHeadAttention(target2, memory, memory, mask=memory_mask, key_padding_mask=memory_key_padding_mask)[0]
target2 = self.normLayer2(target2)
target = target + self.dropout2(target2)
target2 = self.feedforward(target)
target2 = self.normLayer3(target2)
target = target + self.dropout3(target2)
return target
class Transformer(nn.Module):
def __init__(self, d_model, nhead, nEncoder, nDecoder, d_mlp, maxLength, nChar, padIndex, dropout=0.1):
super(Transformer, self).__init__()
self.d_model = d_model
encoderLayer = Encoder(d_model, nhead, d_mlp, dropout)
encoderNorm = NormalizationLayer(d_model)
self.encoder = nn.TransformerEncoder(encoderLayer, nEncoder, encoderNorm)
decoderLayer = Decoder(d_model, nhead, d_mlp, dropout)
decoderNorm = NormalizationLayer(d_model)
self.decoder = nn.TransformerDecoder(decoderLayer, nDecoder, decoderNorm)
self.posEncoder = PositionalEncoding(d_model, dropout, maxLength)
self.inputEmbed = nn.Embedding(nChar, d_model, padding_idx=padIndex)
self.outputEmbed = nn.Embedding(nChar, d_model, padding_idx=padIndex)
self.linear = nn.Linear(d_model, nChar)
def forward(self, src, output, src_mask=None, outputMask=None, src_key_padding_mask=None, output_keyPaddingMask=None, memory_keyPaddingMask=None, isCausal=False):
src = self.inputEmbed(src) * np.sqrt(self.d_model)
src = self.posEncoder(src)
encoderOutputs = self.encoder(src, mask=src_mask, src_key_padding_mask=src_key_padding_mask, is_causal=isCausal)
output = self.outputEmbed(output) * np.sqrt(self.d_model)
output = self.posEncoder(output)
decoderOutputs = self.decoder(output, encoderOutputs, tgt_mask=outputMask, memory_mask=None, tgt_key_padding_mask=output_keyPaddingMask, memory_key_padding_mask=memory_keyPaddingMask)
outputs = self.linear(decoderOutputs)
return outputs
d_model = 512
nhead = 1
nEncoder = 1
nDecoder = 1
d_mlp = 1024
maxLength = 6
nChar = 26
padIndex = 0
dropout = 0.1
model = Transformer(d_model, nhead, nEncoder, nDecoder, d_mlp, maxLength, nChar, padIndex, dropout)
from torch.utils.data import Dataset, DataLoader
class ReverseDS(Dataset):
def __init__(self, length=10000, seqLength=10):
self.length = length
self.seqLenght = seqLength
self.vocab = list("abcdefghijklmnopqrstuvwxyz")
self.vocabSize = len(self.vocab)
self.charToIdx = {char: idx for idx, char in enumerate(self.vocab)}
self.idxToChar = {idx: char for idx, char in enumerate(self.vocab)}
def __len__(self):
return self.length
def __getitem__(self, index):
sequence = torch.randint(high=self.vocabSize, size=(self.seqLenght,))
return sequence, torch.flip(sequence, dims=[0])
dataset = ReverseDS(seqLength=maxLength)
dataloader = DataLoader(dataset, batch_size=5, shuffle=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Transformer(d_model, nhead, nEncoder, nDecoder, d_mlp, maxLength, nChar, padIndex, dropout).to(device)
loss_func = nn.CrossEntropyLoss()
optimize = optimizer.Adam(model.parameters(), lr=0.0001)
def tokensToText(tokens, dataset):
return ''.join(dataset.idxToChar[token.item()] for token in tokens)
inputs, targets = next(iter(dataloader))
print("input: ", tokensToText(inputs[4], dataset))
print("target: ", tokensToText(targets[4], dataset))
# Training Loop
nEpochs = 100
for epoch in range(nEpochs):
for i, (input, target) in enumerate(dataloader):
input = input.T.to(device)
target = target.T.to(device)
target_input = target[:-1, :]
target_real = target[1:, :]
output = model(input, target_real)
lossFunc = loss_func(output.view(-1, nChar), target_real.reshape(-1))
optimize.zero_grad()
lossFunc.backward()
optimize.step()
if i % 100 == 0:
print(f"Epoch: {epoch}, Iteration: {i}, Loss: {lossFunc.item()}")
break
def outputToText(output, dataset):
tokens = F.softmax(output, dim=-1)
tokens = torch.argmax(tokens, dim=-1)
text = ''.join(dataset.idxToChar[token.item()] for token in tokens)
return text
inputs, targets = next(iter(dataloader))
index = 1
print("input: ", tokensToText(inputs[index], dataset))
print("target: ", tokensToText(targets[index], dataset))
input = inputs[index].T.to(device)
target = targets[index].T.to(device)
print(target)
output = model(input, target)
print(output)
print("input: ", tokensToText(inputs[index], dataset))
print("Prediction: ", outputToText(output[index], dataset))