-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_modeling.py
390 lines (354 loc) · 13.8 KB
/
language_modeling.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
'''
Author: Julia Jeng, Shu Wang, Arman Anwar
Brief: AIT 726 Homework 2
Usage:
Put file 'language_modeling.py' and folder 'twitter' in the same folder.
Command to run:
python language_modeling.py
Description:
Build and train a feed forward neural network (FFNN) with 2 layers with hidden vector size 20.
Initalized weights: random weights.
Loss function: mean squared error.
Activation function: sigmoid.
Learning rate: 0.0001-0.00001.
Train/valid rate: 4:1
Emoticon tokenizer: TweetTokenizer
'''
import os
import re
import sys
import random
from random import choice
import numpy as np
from nltk.tokenize import TweetTokenizer
from nltk.util import ngrams
from nltk import word_tokenize
from itertools import chain
from collections import defaultdict
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as torchdata
# global path
logPath = './language_modeling.txt'
datPath = './tweet/'
tmpPath = './tmp/'
# Logger: redirect the stream on screen and to file.
class Logger(object):
def __init__(self, filename = "log.txt"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
# The main function.
def main():
# initialize the log file.
sys.stdout = Logger(logPath)
print("-- AIT726 Homework 2 from Julia Jeng, Shu Wang, and Arman Anwar --")
# create the vocabulary.
if (os.path.exists(tmpPath + '/dataTrain.npy') and
os.path.exists(tmpPath + '/dataTest.npy') and
os.path.exists(tmpPath + '/vocab.npy')):
dataTrain = np.load(tmpPath + '/dataTrain.npy', allow_pickle = True)
dataTest = np.load(tmpPath + '/dataTest.npy', allow_pickle = True)
vocab = np.load(tmpPath + '/vocab.npy', allow_pickle = True)
print('Successfully load data from \'' + tmpPath + '\'.')
else:
dataTrain, dataTest, vocab = CreateVocabulary()
# Extract training ngrams.
if (os.path.exists(tmpPath + '/gramTrain.npy') and
os.path.exists(tmpPath + '/labelTrain.npy')):
gramTrain = np.load(tmpPath + '/gramTrain.npy', allow_pickle = True)
labelTrain = np.load(tmpPath + '/labelTrain.npy', allow_pickle = True)
print('Successfully load n-grams from \'' + tmpPath + '\'.')
print('N-gram train data: positive %d, negative %d.' % (sum(labelTrain), len(labelTrain) - sum(labelTrain)))
else:
gramTrain, labelTrain = ExtractNGram(dataTrain, vocab)
np.save(tmpPath + '/gramTrain.npy', gramTrain)
np.save(tmpPath + '/labelTrain.npy', labelTrain)
# train the FFNN model.
model = TrainFFNN(gramTrain, labelTrain, vocab)
# Extract testing ngrams.
if (os.path.exists(tmpPath + '/gramTest.npy') and
os.path.exists(tmpPath + '/labelTest.npy')):
gramTest = np.load(tmpPath + '/gramTest.npy', allow_pickle = True)
labelTest = np.load(tmpPath + '/labelTest.npy', allow_pickle = True)
print('Successfully load n-grams from \'' + tmpPath + '\'.')
print('N-gram test data: positive %d, negative %d.' % (sum(labelTest), len(labelTest) - sum(labelTest)))
else:
gramTest, labelTest = ExtractNGram(dataTest, vocab)
np.save(tmpPath + '/gramTest.npy', gramTest)
np.save(tmpPath + '/labelTest.npy', labelTest)
TestFFNN(model, gramTest, labelTest)
return
# Read train/test sets and create vocabulary.
def CreateVocabulary():
'''
read train and test sets and create vocabulary.
:return: none
'''
# pre-process the data.
def Preprocess(data):
# remove url
pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
data = re.sub(pattern, '', data)
# remove html special characters.
pattern = r'&[(amp)(gt)(lt)]+;'
data = re.sub(pattern, '', data)
# remove independent numbers.
pattern = r' \d+ '
data = re.sub(pattern, ' ', data)
# lower case capitalized words.
pattern = r'([A-Z][a-z]+)'
def LowerFunc(matched):
return matched.group(1).lower()
data = re.sub(pattern, LowerFunc, data)
# remove hashtags.
pattern = r'[@#]([A-Za-z]+)'
data = re.sub(pattern, '', data)
return data
# get tokens.
def GetTokens(data):
# use tweet tokenizer.
tknzr = TweetTokenizer()
tokens = tknzr.tokenize(data)
tokensNew = []
# tokenize at each punctuation.
pattern = r'[A-Za-z]+\'[A-Za-z]+'
for tk in tokens:
if re.match(pattern, tk):
subtokens = word_tokenize(tk)
tokensNew = tokensNew + subtokens
else:
tokensNew.append(tk)
return tokensNew
# if there is no 'tmp' folder, create one.
if not os.path.exists(tmpPath):
os.mkdir(tmpPath)
if not os.path.exists(datPath):
print('[ERROR]: Cannot find the data path \'' + datPath + '\'.')
# read the training data.
dataTrain = []
for root, ds, fs in os.walk(datPath + '/train/positive/'):
for file in fs:
# get the file path.
fullname = os.path.join(root, file)
# get the training data.
data = open(fullname, encoding = "utf8").read()
# preprocess the data.
data = Preprocess(data)
# get the tokens for the data.
tokens = GetTokens(data)
dataTrain.append(tokens)
#print(tokens)
print('Load TrainSet: %d samples.' % len(dataTrain))
np.save(tmpPath + '/dataTrain.npy', dataTrain)
# build the vocabulary from training set.
vocab = list(set(list(chain.from_iterable(dataTrain))))
print('Vocabulary: %d tokens.' % len(vocab))
np.save(tmpPath + '/vocab.npy', vocab)
dataTest = []
for root, ds, fs in os.walk(datPath + '/test/positive/'):
for file in fs:
# get the file path.
fullname = os.path.join(root, file)
# get the training data.
data = open(fullname, encoding = "utf8").read()
# preprocess the data.
data = Preprocess(data)
# get the tokens for the data.
tokens = GetTokens(data)
dataTest.append(tokens)
#print(tokens)
print('Load TestSet: %d samples.' % len(dataTest))
np.save(tmpPath + '/dataTest.npy', dataTest)
return dataTrain, dataTest, vocab
# extract gram and label for data using vocab
def ExtractNGram(data, vocab):
'''
Extract 2-grams for dataset
:param data: data set D * (tokens)
:param vocab: vocabulary 1 * V
:return: gramData gramLabel in numpy.array
'''
# generate ngram list and label.
ngramList = []
ngramLabel = []
# get all positive 2-grams.
posList = []
for doc in data:
for gram in ngrams(doc, 2):
# gram is a 2-tuple ('A', 'B'), ('A', 'C').
posList.append(gram)
ngramList.append([gram[0], gram[1]])
ngramLabel.append([1])
# create negative 2-grams.
ngramDict = defaultdict(list)
for gram in posList:
# ngramdict is a list dictionary {'A': ['B', 'C']}.
ngramDict[gram[0]].append(gram[1])
for gram in posList:
for i in range(2):
# randomly sample the second word.
while True:
word = choice(vocab)
if (word != gram[0]) and (word not in ngramDict[gram[0]]):
break
ngramList.append([gram[0], word])
ngramLabel.append([0])
numPos = len(posList)
num = len(ngramList)
print('N-gram data: positive %d, negative %d.' % (numPos, num-numPos))
# build vocabulary dictionary.
vocabDict = {word: i for i, word in enumerate(vocab)}
# convert ngram to index
gramData = []
gramLabel = []
for ind in range(0, num):
ngram = ngramList[ind]
if (ngram[0] in vocab and ngram[1] in vocab):
gramData.append([vocabDict[word] for word in ngramList[ind]])
gramLabel.append(ngramLabel[ind])
return np.array(gramData), np.array(gramLabel)
# class of LanguageModeling
class LanguageModeling(nn.Module):
def __init__(self, V):
super(LanguageModeling, self).__init__()
self.dims = 20 # embedding dimension 20-200
self.embedding = nn.Embedding(V, self.dims)
self.L1 = nn.Linear(2 * self.dims, 20)
self.L2 = nn.Linear(20, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
proj = self.embedding(x).view((-1, 2 * self.dims)) # row vector.
a1 = self.sigmoid(self.L1(proj))
a2 = self.sigmoid(self.L2(a1))
return a2
# train the feed forward neural network.
def TrainFFNN(gramTrain, labelTrain, vocab):
'''
train the FFNN with gramTrain, labelTrain and vocab
:param gramTrain: data set N * 2
:param labelTrain: label N * 1
:param vocab: 1 * V
:return:
'''
# initialize network weights with uniform distribution.
def weight_init(m):
if isinstance(m, nn.Linear):
nn.init.uniform_(m.weight)
nn.init.uniform_(m.bias)
# vocabulary number.
V = len(vocab)
# sample number.
N = len(labelTrain)
# shuffle the data and label.
index = [i for i in range(N)]
random.shuffle(index)
gramTrain = gramTrain[index]
labelTrain = labelTrain[index]
# split the train and valid set.
xTrain, xValid, yTrain, yValid = train_test_split(gramTrain, labelTrain, test_size = 0.2)
# convert data (x,y) into tensor.
xTrain = torch.LongTensor(xTrain).cuda()
yTrain = torch.LongTensor(yTrain).cuda()
xValid = torch.LongTensor(xValid).cuda()
yValid = torch.LongTensor(yValid).cuda()
# convert to mini-batch form
batchsize = 200
train = torchdata.TensorDataset(xTrain, yTrain)
numTrain = len(train)
trainloader = torchdata.DataLoader(train, batch_size = batchsize, shuffle = False)
valid = torchdata.TensorDataset(xValid, yValid)
numValid = len(valid)
validloader = torchdata.DataLoader(valid, batch_size = batchsize, shuffle = False)
# build the model of feed forward neural network.
model = LanguageModeling(V)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.apply(weight_init)
model.to(device)
# optimizing with stochastic gradient descent.
optimizer = optim.SGD(model.parameters(), lr = 0.25)
# seting loss function as mean squared error.
criterion = nn.MSELoss()
# run on each epoch.
accList = [0]
for epoch in range(1000):
# training phase.
model.train()
lossTrain = 0
accTrain = 0
for iter, (data, label) in enumerate(trainloader):
data = data.to(device)
label = label.to(device)
optimizer.zero_grad() # set the gradients to zero.
yhat = model.forward(data) # get output
loss = criterion(label.float(), yhat)
loss.backward()
optimizer.step()
# statistic
lossTrain += loss.item()
preds = (yhat > 0.5).long()
accTrain += torch.sum(torch.eq(preds, label).long()).item()
lossTrain /= (iter + 1)
accTrain *= 100 / numTrain
# validation phase.
model.eval()
accValid = 0
with torch.no_grad():
for iter, (data, label) in enumerate(validloader):
data = data.to(device)
label = label.to(device)
yhat = model.forward(data) # get output
# statistic
preds = (yhat > 0.5).long()
accValid += torch.sum(torch.eq(preds, label).long()).item()
accValid *= 100 / numValid
accList.append(accValid)
# output information.
if 0 == (epoch + 1) % 10:
print('[Epoch %03d] loss: %.3f, train acc: %.3f%%, valid acc: %.3f%%' % (epoch+1, lossTrain, accTrain, accValid))
# save the best model.
if accList[-1] > max(accList[0:-1]):
torch.save(model.state_dict(), tmpPath + '/model.pth')
# stop judgement.
if (epoch+1) >= 10 and accList[-1] < min(accList[-10:-1]):
break
# load best model.
model.load_state_dict(torch.load(tmpPath + '/model.pth'))
return model
# test the feed forward neural network.
def TestFFNN(model, gramTest, labelTest):
'''
test the gramTest with model, and output the accuracy
:param model: FFNN model
:param gramTest: data set N * 2
:param labelTest: label N * 1
:return: accuracy - testing accuracy
'''
# prepare for model.
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
# prepare for data.
data = torch.LongTensor(gramTest).cuda()
label = torch.LongTensor(labelTest).cuda()
numTest = len(label)
# test phase.
data = data.to(device)
label = label.to(device)
yhat = model.forward(data) # get output
# statistic
preds = (yhat > 0.5).long()
numCorrect = torch.sum(torch.eq(preds, label).long()).item()
accTest = 100 * numCorrect / numTest
print('-------------------------------------------')
print('Test accuracy: %.3f%%' % (accTest))
print('-------------------------------------------')
return accTest
# The program entrance.
if __name__ == "__main__":
main()