-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformer.py
More file actions
256 lines (213 loc) · 10.3 KB
/
transformer.py
File metadata and controls
256 lines (213 loc) · 10.3 KB
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
# transformer.py
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import random
from torch import optim
import matplotlib.pyplot as plt
from typing import List
from utils import *
# Wraps an example: stores the raw input string (input), the indexed form of the string (input_indexed),
# a tensorized version of that (input_tensor), the raw outputs (output; a numpy array) and a tensorized version
# of it (output_tensor).
# Per the task definition, the outputs are 0, 1, or 2 based on whether the character occurs 0, 1, or 2 or more
# times previously in the input sequence (not counting the current occurrence).
class LetterCountingExample(object):
def __init__(self, input: str, output: np.array, vocab_index: Indexer):
self.input = input
self.input_indexed = np.array([vocab_index.index_of(ci) for ci in input])
self.input_tensor = torch.LongTensor(self.input_indexed)
self.output = output
self.output_tensor = torch.LongTensor(self.output)
# Should contain your overall Transformer implementation. You will want to use Transformer layer to implement
# a single layer of the Transformer; this Module will take the raw words as input and do all of the steps necessary
# to return distributions over the labels (0, 1, or 2).
class Transformer(nn.Module):
def __init__(self, vocab_size, num_positions, d_model, d_internal, num_classes, num_layers):
"""
:param vocab_size: vocabulary size of the embedding layer
:param num_positions: max sequence length that will be fed to the model; should be 20
:param d_model: see TransformerLayer
:param d_internal: see TransformerLayer
:param num_classes: number of classes predicted at the output layer; should be 3
:param num_layers: number of TransformerLayers to use; can be whatever you want
"""
super().__init__()
self.vocab_size = vocab_size
self.num_positions = num_positions
self.d_model = d_model
self.d_internal = d_internal
self.num_classes = num_classes
self.num_layers = num_layers
self.transformerLayer = TransformerLayer(self.d_model,self.d_internal)
self.emb = nn.Embedding(self.vocab_size,self.d_model)
self.posEnc = PositionalEncoding(self.d_model,self.num_positions,False)
self.linear = nn.Linear(self.d_model,self.num_classes)
self.Softmax = nn.LogSoftmax(dim=1)
# raise Exception("Implement me")
def forward(self, indices):
"""
:param indices: list of input indices
:return: A tuple of the softmax log probabilities (should be a 20x3 matrix) and a list of the attention
maps you use in your layers (can be variable length, but each should be a 20x20 matrix)
"""
# raise Exception("Implement me")
x = self.emb(indices)
x += self.posEnc(x)
attnList = list()
for i in range(self.num_layers):
x,attn = self.transformerLayer(x)
attnList.append(attn)
# firstLayerOutput,firstLayerAttn = self.transformerLayer(x)
# secondLayerOutput,secondLayerAttn = self.transformerLayer(firstLayerOutput)
output = self.Softmax (self.linear(x))
return (output,attnList)
# Your implementation of the Transformer layer goes here. It should take vectors and return the same number of vectors
# of the same length, applying self-attention, the feedforward layer, etc.
class TransformerLayer(nn.Module):
def __init__(self, d_model, d_internal):
"""
:param d_model: The dimension of the inputs and outputs of the layer (note that the inputs and outputs
have to be the same size for the residual connection to work)
:param d_internal: The "internal" dimension used in the self-attention computation. Your keys and queries
should both be of this length.
"""
super().__init__()
self.d_model = d_model
self.d_internal = d_internal
self.weightQ = nn.Linear(d_model,d_internal)
self.weightK = nn.Linear(d_model,d_internal)
self.weightV = nn.Linear(d_model,d_internal)
self.weightZ = nn.Linear(d_model,d_internal)
self.softmax = nn.Softmax(dim=1)
# raise Exception("Implement me")
def forward(self, input_vecs):
"""
:param input_vecs: an input tensor of shape [seq len, d_model]
:return: a tuple of two elements:
- a tensor of shape [seq len, d_model] representing the log probabilities of each position in the input
- a tensor of shape [seq len, seq len], representing the attention map for this layer
"""
# raise Exception("Implement me")
residual = input_vecs
#Shape seq len X d_model
q = F.relu(self.weightQ(input_vecs))
k = F.relu(self.weightK(input_vecs))
v = F.relu(self.weightV(input_vecs))
#shape
qk = torch.div(torch.matmul(q,torch.t(k)),torch.sqrt(torch.tensor(self.d_model)))
# mask = torch.ones(input_vecs.shape[0],input_vecs.shape[0]).tril()
# mask[mask == 0] = 1e-15
# attn = torch.mul(qk,mask)
attnScores = self.softmax(qk)
# residual = attnScores
h = torch.matmul(attnScores,v)
h = F.relu(h)
# z=F.relu(self.weightZ(h))
z = self.weightZ(h)
# print("Z",z.shape)
# print("residual",residual.shape)
z=torch.add(z,residual)
return z,attnScores
# Implementation of positional encoding that you can use in your network
class PositionalEncoding(nn.Module):
def __init__(self, d_model: int, num_positions: int=20, batched=False):
"""
:param d_model: dimensionality of the embedding layer to your model; since the position encodings are being
added to character encodings, these need to match (and will match the dimension of the subsequent Transformer
layer inputs/outputs)
:param num_positions: the number of positions that need to be encoded; the maximum sequence length this
module will see
:param batched: True if you are using batching, False otherwise
"""
super().__init__()
# Dict size
self.emb = nn.Embedding(num_positions, d_model)
self.batched = batched
def forward(self, x):
"""
:param x: If using batching, should be [batch size, seq len, embedding dim]. Otherwise, [seq len, embedding dim]
:return: a tensor of the same size with positional embeddings added in
"""
# Second-to-last dimension will always be sequence length
input_size = x.shape[-2]
indices_to_embed = torch.tensor(np.asarray(range(0, input_size))).type(torch.LongTensor)
if self.batched:
# Use unsqueeze to form a [1, seq len, embedding dim] tensor -- broadcasting will ensure that this
# gets added correctly across the batch
emb_unsq = self.emb(indices_to_embed).unsqueeze(0)
return x + emb_unsq
else:
return x + self.emb(indices_to_embed)
# This is a skeleton for train_classifier: you can implement this however you want
def train_classifier(args, train, dev):
# raise Exception("Not fully implemented yet")
# The following code DOES NOT WORK but can be a starting point for your implementation
# Some suggested snippets to use:
model = Transformer(27, 20, 30, 30, 3, 2)
model.zero_grad()
model.train()
optimizer = optim.Adam(model.parameters(), lr=1e-3)
torch.autograd.set_detect_anomaly(True)
num_epochs = 5
for t in range(0, num_epochs):
loss_this_epoch = 0.0
random.seed(t)
# You can use batching if you'd like
ex_idxs = [i for i in range(0, len(train))]
random.shuffle(ex_idxs)
loss_fcn = nn.NLLLoss()
for ex_idx in ex_idxs:
ex = train[ex_idx]
(log_probs, attn_maps) = model.forward(ex.input_tensor)
# loss = loss_fcn(...) # TODO: Run forward and compute loss
loss = loss_fcn(log_probs,ex.output_tensor)
model.zero_grad()
loss.backward()
optimizer.step()
loss_this_epoch += loss.item()
print("Epoch No: ",t," Loss: ",loss_this_epoch)
model.eval()
return model
####################################
# DO NOT MODIFY IN YOUR SUBMISSION #
####################################
def decode(model: Transformer, dev_examples: List[LetterCountingExample], do_print=False, do_plot_attn=False):
"""
Decodes the given dataset, does plotting and printing of examples, and prints the final accuracy.
:param model: your Transformer that returns log probabilities at each position in the input
:param dev_examples: the list of LetterCountingExample
:param do_print: True if you want to print the input/gold/predictions for the examples, false otherwise
:param do_plot_attn: True if you want to write out plots for each example, false otherwise
:return:
"""
num_correct = 0
num_total = 0
if len(dev_examples) > 100:
print("Decoding on a large number of examples (%i); not printing or plotting" % len(dev_examples))
do_print = False
do_plot_attn = False
for i in range(0, len(dev_examples)):
ex = dev_examples[i]
(log_probs, attn_maps) = model.forward(ex.input_tensor)
predictions = np.argmax(log_probs.detach().numpy(), axis=1)
if do_print:
print("INPUT %i: %s" % (i, ex.input))
print("GOLD %i: %s" % (i, repr(ex.output.astype(dtype=int))))
print("PRED %i: %s" % (i, repr(predictions)))
if do_plot_attn:
for j in range(0, len(attn_maps)):
attn_map = attn_maps[j]
fig, ax = plt.subplots()
im = ax.imshow(attn_map.detach().numpy(), cmap='hot', interpolation='nearest')
ax.set_xticks(np.arange(len(ex.input)), labels=ex.input)
ax.set_yticks(np.arange(len(ex.input)), labels=ex.input)
ax.xaxis.tick_top()
# plt.show()
plt.savefig("plots/%i_attns%i.png" % (i, j))
acc = sum([predictions[i] == ex.output[i] for i in range(0, len(predictions))])
num_correct += acc
num_total += len(predictions)
print("Accuracy: %i / %i = %f" % (num_correct, num_total, float(num_correct) / num_total))