-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_learning.py
411 lines (345 loc) · 14 KB
/
transfer_learning.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import numpy
import spacy
import numpy as np
import random
import math
import time
from tqdm import tqdm
import re
import sys
random.seed(1)
np.random.seed(1)
torch.manual_seed(1)
torch.cuda.manual_seed(1)
BATCH_SIZE = 16
MAX_EPOCHS = 10
EMBEDDING_SIZE = 128
HIDDEN_DIMENSION = 128
LAYERS = 3
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cuda')
class Dataset(torch.utils.data.Dataset):
def __init__(self, en_file_location, fr_file_location, sequence_length):
self.en_file_location = en_file_location
self.fr_file_location = fr_file_location
self.sequence_length = sequence_length
self.initialize_data()
self.modify()
self.combined_data = list()
self.combineData()
def initialize_data(self):
with open(self.en_file_location, "r") as inFile:
enData = inFile.readlines()
with open(self.fr_file_location, "r") as inFile:
frData = inFile.readlines()
en_tokenized_data = self.tokenizer(enData)
self.ENdataset = self.cleaner(en_tokenized_data)
(
self.ENword2Index,
self.ENindex2Word,
self.ENvocab_size,
self.ENvocab,
self.ENwordFrequency
) = self.vocabBuilder(self.ENdataset)
self.ENwords = list()
for sentence in self.ENdataset:
for word in sentence:
self.ENwords.append(word)
self.ENwords_indexes = [self.ENword2Index[word] for word in self.ENwords]
fr_tokenized_data = self.tokenizer(frData)
self.FRdataset = self.cleaner(fr_tokenized_data)
(
self.FRword2Index,
self.FRindex2Word,
self.FRvocab_size,
self.FRvocab,
self.FRwordFrequency
) = self.vocabBuilder(self.FRdataset)
self.FRwords = list()
for sentence in self.FRdataset:
for word in sentence:
self.FRwords.append(word)
self.FRwords_indexes = [self.FRword2Index[word] for word in self.FRwords]
def tokenizer(self,corpus):
"""
tokenizes the corpus
Arguments:
corpus (list)
Returns:
tokenized corpus (list)
"""
hashtag_regex = "#[a-zA-Z0-9]+"
url_regex = "((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)"
mention_regex = "@\w+"
processed_corpus = list()
for tweet in corpus:
normalized_tweet = tweet.lower()
hashtag_removed_tweet = re.sub(hashtag_regex, "<HASHTAG>", normalized_tweet)
website_removed_tweet = re.sub(url_regex, "<URL>", hashtag_removed_tweet)
mention_removed_tweet = re.sub(
mention_regex, "<MENTION>", website_removed_tweet
)
punctuation_repeat_removed = re.sub(
r"(\W)(?=\1)", "", mention_removed_tweet
)
tokenized_tweet = punctuation_repeat_removed.split()
cleaned_tokenized_tweet = list()
for token in tokenized_tweet:
if token not in ["<HASHTAG>", "<URL>", "<MENTION>", "<OOV>"]:
split_tokens = "".join(
(char if char.isalpha() or char.isnumeric() else f" {char} ")
for char in token
).split()
for cleaned_token in split_tokens:
cleaned_tokenized_tweet.append(cleaned_token)
else:
cleaned_tokenized_tweet.append(token)
cleaned_tokenized_tweet = ['<SOS>'] + cleaned_tokenized_tweet + ['<EOS>']
processed_corpus.append(cleaned_tokenized_tweet)
return processed_corpus
def cleaner(self,corpus):
"""
replacing !,?,. with . and removing other punctuations
Arguments:
tokenized corpuse (list)
Returns:
cleaned corpus (list)
"""
import string
cleaned_corpus = list()
for sentence in corpus:
new_sentence = list()
for token in sentence:
if token in ["!", ".", "?"]:
new_sentence.append(".")
elif token in string.punctuation:
continue
else:
new_sentence.append(token)
cleaned_corpus.append(new_sentence)
return cleaned_corpus
def vocabBuilder(self,corpus):
"""
Builds the vocabulary of the input dataset.
Arguments:
The cleaned tokenized the dataset
Returns:
Word to Index dict, Index to Word list, Number of Unique Words, Set of Vocab
"""
word2Index = dict()
index2Word = list()
vocab = set()
wordFrequency = dict()
n_unique_words = 0
for sentence in corpus:
for word in sentence:
vocab.add(word)
if word not in word2Index:
word2Index[word] = n_unique_words
index2Word.append(word)
n_unique_words += 1
wordFrequency[word] = 1
else:
wordFrequency[word] += 1
return word2Index, index2Word, n_unique_words, vocab, wordFrequency
def modify(self):
for i in range(len(self.ENdataset)):
for j in range(len(self.ENdataset[i])):
if self.ENwordFrequency[self.ENdataset[i][j]] < 2:
self.ENdataset[i][j] = '<OOV>'
elif any(character.isdigit() for character in self.ENdataset[i][j]):
self.ENdataset[i][j] = '<OOV>'
print(self.ENvocab_size)
self.ENdataset = self.cleaner(self.ENdataset)
(
self.ENword2Index,
self.ENindex2Word,
self.ENvocab_size,
self.ENvocab,
self.ENwordFrequency
) = self.vocabBuilder(self.ENdataset)
self.ENwords = list()
for sentence in self.ENdataset:
for word in sentence:
self.ENwords.append(word)
self.ENwords_indexes = [self.ENword2Index[word] for word in self.ENwords]
for i in range(len(self.FRdataset)):
for j in range(len(self.FRdataset[i])):
if self.FRwordFrequency[self.FRdataset[i][j]] < 2:
self.FRdataset[i][j] = '<OOV>'
elif any(character.isdigit() for character in self.FRdataset[i][j]):
self.FRdataset[i][j] = '<OOV>'
self.FRdataset = self.cleaner(self.FRdataset)
(
self.FRword2Index,
self.FRindex2Word,
self.FRvocab_size,
self.FRvocab,
self.FRwordFrequency
) = self.vocabBuilder(self.FRdataset)
self.FRwords = list()
for sentence in self.FRdataset:
for word in sentence:
self.FRwords.append(word)
self.FRwords_indexes = [self.FRword2Index[word] for word in self.FRwords]
print(self.FRvocab_size)
def combineData(self):
for idx in range(len(self.ENdataset)):
self.combined_data.append((self.ENdataset[idx], self.FRdataset[idx]))
def __len__(self):
return len(self.FRdataset)
def __getitem__(self, index):
return (
np.array(self.ENwords_indexes[index : index + self.sequence_length]),
np.array(self.FRwords_indexes[index : index + self.sequence_length])
)
def collate(data):
X = [x[0] for x in data]
Y = [y[1] for y in data]
x_len = max([len(x) for x in X])
y_len = max([len(y) for y in Y])
padded_x = np.zeros((BATCH_SIZE, x_len))
padded_y = np.zeros((BATCH_SIZE, y_len))
for idx, (x, y) in enumerate(zip(X,Y)):
padded_x[idx] = numpy.pad(x, (0,x_len - len(x)))
padded_y[idx] = numpy.pad(y, (0,y_len - len(y)))
return (
torch.tensor(padded_x, dtype=torch.long).t().to(device),
torch.tensor(padded_y, dtype=torch.long).t().to(device)
)
class Encoder(nn.Module):
def __init__(self, input_dim, embedding_dim, hidden_dim, n_layers, dropout ,output_dim):
super().__init__()
self.input_dim = input_dim
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.dropout = nn.Dropout(dropout)
self.embedding_layer = nn.Embedding(self.input_dim, self.embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout = dropout)
self.fc = nn.Linear(hidden_dim, output_dim)
def forward(self, source):
embedding = self.dropout(self.embedding_layer(source))
output, (state_h, state_c) = self.lstm(embedding)
return state_h, state_c
class Decoder(nn.Module):
def __init__(self, output_dim, embedding_dim, hidden_dim, n_layers, dropout):
super().__init__()
self.output_dim = output_dim
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.embedding_layer = nn.Embedding(output_dim, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, n_layers, dropout = dropout)
self.fc = nn.Linear(hidden_dim, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, source, state_h, state_c):
source = source.unsqueeze(0)
embedding = self.dropout(self.embedding_layer(source))
output, (state_h, state_c) = self.lstm(embedding, (state_h, state_c))
pred = self.fc(output.squeeze(0))
return pred, state_h, state_c
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder):
super().__init__()
self.encoder = encoder
self.decoder = decoder
def forward(self, source, ground_truth, force_teaching_ratio=0.5):
# ground_truth.shape[0] = lenght of the sentence
# ground_truth.shape[1] = batch_size
state_h, state_c = self.encoder(source)
outputs = torch.zeros(ground_truth.shape[0], ground_truth.shape[1], self.decoder.output_dim).to(device)
decoder_input = ground_truth[0,:]
for idx in range(1,ground_truth.shape[0]):
output, state_h, state_c = self.decoder(decoder_input, state_h, state_c)
outputs[idx] = output
force = random.random() < force_teaching_ratio
predicted = output.argmax(1)
decoder_input = ground_truth[idx] if force else predicted
return outputs
def train(model, optimizer, criterion, dataloader):
for epoch in range(MAX_EPOCHS):
model.train().to(device)
epoch_loss = 0.0
for x,y in tqdm(dataloader):
optimizer.zero_grad()
pred = model(x, y)
pred = pred[1:].reshape(-1,pred.shape[-1])
y = y[1:].reshape(-1)
loss = criterion(pred, y)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
print({ 'epoch': epoch, 'loss':epoch_loss/len(dataloader) })
class RNN(nn.Module):
def __init__(
self,
dataset,
lstm_size=128,
n_layers=3,
embedding_dim=128,
):
super(RNN, self).__init__()
self.vocab_size = dataset.vocab_size
self.input_dim = lstm_size
self.embedding_dim = embedding_dim
self.lstm_hidden_dim = lstm_size
self.n_layers = n_layers
self.embedding_layer = nn.Embedding(
num_embeddings=self.vocab_size, embedding_dim=self.embedding_dim
)
self.lstm = nn.LSTM(
input_size=self.input_dim,
hidden_size=self.lstm_hidden_dim,
num_layers=self.n_layers,
dropout=0.2,
)
self.fc = nn.Linear(self.lstm_hidden_dim, self.vocab_size)
self.output_dim = data.FRvocabSize
def forward(self, x, prev_state=None):
if prev_state == None:
prev_state = self.init_state(1)
embed = self.embedding_layer(x)
output, state = self.lstm(embed, prev_state)
logits = self.fc(output)
return logits, state
def init_state(self, sequence_length):
return (
torch.zeros(self.n_layers, sequence_length, self.lstm_hidden_dim).to(device),
torch.zeros(self.n_layers, sequence_length, self.lstm_hidden_dim).to(device),
)
def translate(text):
model.eval()
with torch.no_grad():
tokens = text.split(' ')
for idx in range(len(tokens)):
if tokens[idx] not in data.ENvocab:
tokens[idx] = '<OOV>'
tokens = ['<SOS>'] + tokens + ['<EOS>']
src_indexes = [data.ENword2Index[token] for token in tokens]
src_tensor = torch.LongTensor(src_indexes).unsqueeze(0).to(device)
src_tensor = src_tensor.reshape(-1,1)
output = model(src_tensor, src_tensor)
output_dim = output.shape[-1]
output = output.view(-1, output_dim)
indices = torch.argmax(output,dim=1).tolist()
return [data.FRindex2Word[x] for x in indices]
if __name__ == '__main__':
data = Dataset("./data/ted-talks-corpus/train.en", "./data/ted-talks-corpus/train.fr", 3)
data.combined_data = sorted(data.combined_data, key=lambda x:len(x[0]))
dataloader = DataLoader(data, shuffle=False, collate_fn=collate, batch_size=BATCH_SIZE, drop_last=True)
enc = Encoder(12824,EMBEDDING_SIZE, HIDDEN_DIMENSION, LAYERS, 0.5, 12824)
dec = Decoder(15821,EMBEDDING_SIZE, HIDDEN_DIMENSION, LAYERS, 0.5)
enc.load_state_dict(torch.load('./models/encoder_weights.pth'))
dec.load_state_dict(torch.load('./models/decoder_weights.pth'))
model = Seq2Seq(enc, dec)
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()
# train(model, optimizer, criterion, dataloader)
model = torch.load(sys.argv[1])
input_sentence = input("Input Sentence : ")
print(" ".join(translate(input_sentence)[1:-1]))