-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_seq2seq.py
336 lines (255 loc) · 12.7 KB
/
simple_seq2seq.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
''' This is a simple sequence to sequence implementation in pytorch.
We are implementing the machine translation task for German -> English.
'''
import os
import math
import random
import spacy
import torch
import torch.nn as nn
import torch.optim as optim
from torchtext.datasets import TranslationDataset, Multi30k
from torchtext.data import Field, BucketIterator
# set the random seed to have deterministic results
SEED = 1
random.seed(SEED)
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
# spacy used for tokenization
spacy_en = spacy.load('en')
spacy_de = spacy.load('de')
def tokenize_de(text):
# tokenizes the german text into a list of strings(tokens) and reverse it
return [tok.text for tok in spacy_de.tokenizer(text)][::-1] # list[::-1] used to reverse the list
def tokenize_en(text):
# tokenizes the english text into a list of strings(tokens)
return [tok.text for tok in spacy_en.tokenizer(text)]
# torchtext's Field handle how the data should be processed. For more refer: https://github.com/pytorch/text
# use the tokenize_de, tokenize_en for tokenization of german and english sentences.
# German is the src, English is the trg
# append the <sos> (start of sentence), <eos> (end of sentence) tokens to all sentences.
SRC = Field(tokenize=tokenize_de, init_token='<sos>', eos_token='<eos>', lower=True)
TRG = Field(tokenize=tokenize_en, init_token='<sos>', eos_token='<eos>', lower=True)
# we will be using Multi30k dataset. This is a dataset with ~30K parallel English, German, French sentences.
# exts specifies which languages to use as source and target. source goes first
# fields define which data processing to apply for source and target
train_data, valid_data, test_data = Multi30k.splits(exts=('.de', '.en'), fields=(SRC, TRG))
print('Loaded data...')
# build the vocab
# consider words which are having atleast min_freq.
# words having less than min_freq will be replaced by <unk> token
SRC.build_vocab(train_data, min_freq=2)
TRG.build_vocab(train_data, min_freq=2)
print('Vocab builded...')
# define batch size
BATCH_SIZE = 32
# create data iterators for the data
# padding all the sentences to same length, replacing words by its index,
# bucketing (minimizes the amount of padding by grouping similar length sentences)
train_iterator, valid_iterator, test_iterator = BucketIterator.splits((train_data, valid_data, test_data), batch_size=BATCH_SIZE)
class Encoder(nn.Module):
''' Sequence to sequence networks consists of Encoder and Decoder modules.
This class contains the implementation of Encoder module.
Args:
input_dim: A integer indicating the size of input dimension.
emb_dim: A integer indicating the size of embeddings.
hidden_dim: A integer indicating the hidden dimension of RNN layers.
n_layers: A integer indicating the number of layers.
dropout: A float indicating dropout.
'''
def __init__(self, input_dim, emb_dim, hidden_dim, n_layers, dropout):
super().__init__()
self.input_dim = input_dim
self.emb_dim = emb_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.dropout = dropout
self.embedding = nn.Embedding(input_dim, emb_dim)
self.rnn = nn.LSTM(emb_dim, hidden_dim, n_layers, dropout=dropout) # default is time major
self.dropout = nn.Dropout(dropout)
def forward(self, src):
# src is of shape [sentence_length, batch_size], it is time major
# embedded is of shape [sentence_length, batch_size, embedding_size]
embedded = self.embedding(src)
embedded = self.dropout(embedded)
# inputs to the rnn is input, (h, c); if hidden, cell states are not passed means default initializes to zero.
# input is of shape [sequence_length, batch_size, input_size]
# hidden is of shape [num_layers * num_directions, batch_size, hidden_size]
# cell is of shape [num_layers * num_directions, batch_size, hidden_size]
outputs, (hidden, cell) = self.rnn(embedded)
# outputs are always from the top hidden layer, if bidirectional outputs are concatenated.
# outputs shape [sequence_length, batch_size, hidden_dim * num_directions]
return hidden, cell
class Decoder(nn.Module):
''' This class contains the implementation of Decoder Module.
Args:
embedding_dim: A integer indicating the embedding size.
output_dim: A integer indicating the size of output dimension.
hidden_dim: A integer indicating the hidden size of rnn.
n_layers: A integer indicating the number of layers in rnn.
dropout: A float indicating the dropout.
'''
def __init__(self, embedding_dim, output_dim, hidden_dim, n_layers, dropout):
super().__init__()
self.embedding_dim = embedding_dim
self.output_dim = output_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.dropout = dropout
self.embedding = nn.Embedding(output_dim, embedding_dim)
self.rnn = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout=dropout)
self.linear = nn.Linear(hidden_dim, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, input, hidden, cell):
# input is of shape [batch_size]
# hidden is of shape [n_layer * num_directions, batch_size, hidden_size]
# cell is of shape [n_layer * num_directions, batch_size, hidden_size]
input = input.unsqueeze(0)
# input shape is [1, batch_size]. reshape is needed rnn expects a rank 3 tensors as input.
# so reshaping to [1, batch_size] means a batch of batch_size each containing 1 index.
embedded = self.embedding(input)
embedded = self.dropout(embedded)
# embedded is of shape [1, batch_size, embedding_dim]
output, (hidden, cell) = self.rnn(embedded, (hidden, cell))
# generally output shape is [sequence_len, batch_size, hidden_dim * num_directions]
# generally hidden shape is [num_layers * num_directions, batch_size, hidden_dim]
# generally cell shape is [num_layers * num_directions, batch_size, hidden_dim]
# sequence_len and num_directions will always be 1 in the decoder.
# output shape is [1, batch_size, hidden_dim]
# hidden shape is [num_layers, batch_size, hidden_dim]
# cell shape is [num_layers, batch_size, hidden_dim]
predicted = self.linear(output.squeeze(0)) # linear expects as rank 2 tensor as input
# predicted shape is [batch_size, output_dim]
return predicted, hidden, cell
class Seq2Seq(nn.Module):
''' This class contains the implementation of complete sequence to sequence network.
It uses to encoder to produce the context vectors.
It uses the decoder to produce the predicted target sentence.
Args:
encoder: A Encoder class instance.
decoder: A Decoder class instance.
'''
def __init__(self, encoder, decoder):
super().__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, src, trg, teacher_forcing_ratio=0.5):
# src is of shape [sequence_len, batch_size]
# trg is of shape [sequence_len, batch_size]
# if teacher_forcing_ratio is 0.5 we use ground-truth inputs 50% of time and 50% time we use decoder outputs.
batch_size = trg.shape[1]
max_len = trg.shape[0]
trg_vocab_size = self.decoder.output_dim
# to store the outputs of the decoder
outputs = torch.zeros(max_len, batch_size, trg_vocab_size)
# context vector, last hidden and cell state of encoder to initialize the decoder
hidden, cell = self.encoder(src)
# first input to the decoder is the <sos> tokens
input = trg[0, :]
for t in range(1, max_len):
output, hidden, cell = self.decoder(input, hidden, cell)
outputs[t] = output
use_teacher_force = random.random() < teacher_forcing_ratio
top1 = output.max(1)[1]
input = (trg[t] if use_teacher_force else top1)
# outputs is of shape [sequence_len, batch_size, output_dim]
return outputs
INPUT_DIM = len(SRC.vocab)
OUTPUT_DIM = len(TRG.vocab)
ENC_EMB_DIM = 256 # encoder embedding size
DEC_EMB_DIM = 256 # decoder embedding size (can be different from encoder embedding size)
HID_DIM = 512 # hidden dimension (must be same for encoder & decoder)
N_LAYERS = 2 # number of rnn layers (must be same for encoder & decoder)
ENC_DROPOUT = 0.5 # encoder dropout
DEC_DROPOUT = 0.5 # decoder dropout (can be different from encoder droput)
# encoder
enc = Encoder(INPUT_DIM, ENC_EMB_DIM, HID_DIM, N_LAYERS, ENC_DROPOUT)
# decoder
dec = Decoder(DEC_EMB_DIM, OUTPUT_DIM, HID_DIM, N_LAYERS, DEC_DROPOUT)
# model
model = Seq2Seq(enc, dec)
optimizer = optim.Adam(model.parameters())
pad_idx = TRG.vocab.stoi['<pad>']
# loss function calculates the average loss per token
# passing the <pad> token to ignore_idx argument, we will ignore loss whenever the target token is <pad>
criterion = nn.CrossEntropyLoss(ignore_index=pad_idx)
def train(model, iterator, optimizer, criterion, clip):
''' Training loop for the model to train.
Args:
model: A Seq2Seq model instance.
iterator: A DataIterator to read the data.
optimizer: Optimizer for the model.
criterion: loss criterion.
clip: gradient clip value.
Returns:
epoch_loss: Average loss of the epoch.
'''
# some layers have different behavior during train/and evaluation (like BatchNorm, Dropout) so setting it matters.
model.train()
# loss
epoch_loss = 0
for i, batch in enumerate(iterator):
src = batch.src
trg = batch.trg
optimizer.zero_grad()
# trg is of shape [sequence_len, batch_size]
# output is of shape [sequence_len, batch_size, output_dim]
output = model(src, trg)
# loss function works only 2d logits, 1d targets
# so flatten the trg, output tensors. Ignore the <sos> token
# trg shape shape should be [(sequence_len - 1) * batch_size]
# output shape should be [(sequence_len - 1) * batch_size, output_dim]
loss = criterion(output[1:].view(-1, output.shape[2]), trg[1:].view(-1))
# backward pass
loss.backward()
# clip the gradients
torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
# update the parameters
optimizer.step()
epoch_loss += loss.item()
# return the average loss
return epoch_loss / len(iterator)
def evaluate(model, iterator, criterion):
''' Evaluation loop for the model to evaluate.
Args:
model: A Seq2Seq model instance.
iterator: A DataIterator to read the data.
criterion: loss criterion.
Returns:
epoch_loss: Average loss of the epoch.
'''
# some layers have different behavior during train/and evaluation (like BatchNorm, Dropout) so setting it matters.
model.eval()
# loss
epoch_loss = 0
# we don't need to update the model parameters. only forward pass.
with torch.no_grad():
for i, batch in enumerate(iterator):
src = batch.src
trg = batch.trg
output = model(src, trg, 0) # turn off the teacher forcing
# loss function works only 2d logits, 1d targets
# so flatten the trg, output tensors. Ignore the <sos> token
# trg shape shape should be [(sequence_len - 1) * batch_size]
# output shape should be [(sequence_len - 1) * batch_size, output_dim]
loss = criterion(output[1:].view(-1, output.shape[2]), trg[1:].view(-1))
epoch_loss += loss.item()
return epoch_loss / len(iterator)
N_EPOCHS = 10 # number of epochs
CLIP = 10 # gradient clip value
SAVE_DIR = 'models' # directory name to save the models.
MODEL_SAVE_PATH = os.path.join(SAVE_DIR, 'seq2seq_model.pt')
best_validation_loss = float('inf')
if not os.path.isdir(f'{SAVE_DIR}'):
os.makedirs(f'{SAVE_DIR}')
for epoch in range(N_EPOCHS):
train_loss = train(model, train_iterator, optimizer, criterion, CLIP)
valid_loss = evaluate(model, valid_iterator, criterion)
if valid_loss < best_validation_loss:
best_validation_loss = valid_loss
torch.save(model.state_dict(), MODEL_SAVE_PATH)
print(f'| Epoch: {epoch+1:03} | Train Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f} | Val. Loss: {valid_loss:.3f} | Val. PPL: {math.exp(valid_loss):7.3f} |')
# load the parameters(state_dict) that gave the best validation loss and run the model to test.
model.load_state_dict(torch.load(MODEL_SAVE_PATH))
test_loss = evaluate(model, test_iterator, criterion)
print(f'| Test Loss: {test_loss:.3f} | Test PPL: {math.exp(test_loss):7.3f} |')