-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrnn.py
More file actions
524 lines (439 loc) · 20.8 KB
/
rnn.py
File metadata and controls
524 lines (439 loc) · 20.8 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# this is for filtering the warnings
import warnings
warnings.filterwarnings('ignore')
path_prefix='./'
# utils.py
# 這個 block 用來先定義一些等等常用到的函式
import torch
import re
import numpy as np
import pandas as pd
import torch.optim as optim
import torch.nn.functional as F
from gensim.parsing.preprocessing import remove_stopwords, stem_text,strip_non_alphanum,strip_punctuation,strip_multiple_whitespaces
def load_training_data(path='training_label.txt'):
# 把 training 時需要的 data 讀進來
# 如果是 'training_label.txt',需要讀取 label,如果是 'training_nolabel.txt',不需要讀取 label
if 'training_label' in path:
with open(path, 'r', encoding="utf-8") as f:
lines = f.readlines()
lines = [line.strip('\n') for line in lines]
lines = [line.split(' ') for line in lines]
lines = [' '.join(line).split() for line in lines]
x = [line[2:] for line in lines]
y = [line[0] for line in lines]
print(x[0:10])
print(y[0:10])
return x, y
else:
with open(path, 'r', encoding="utf-8") as f:
lines = f.readlines()
x = [line.strip('\n').split(' ') for line in lines]
return x
def load_testing_data(path='testing_data'):
# 把 testing 時需要的 data 讀進來
with open(path, 'r', encoding="utf-8") as f:
lines = f.readlines()
lines = ["".join(line.strip('\n').split(",")[1:]).strip() for line in lines[1:]]
lines = [line.split(' ') for line in lines]
X = [' '.join(line).split() for line in lines]
print(X[0:10])
return X
def evaluation(outputs, labels):
# outputs => probability (float)
# labels => labels
outputs[outputs>=0.5] = 1 # 大於等於 0.5 為有惡意
outputs[outputs<0.5] = 0 # 小於 0.5 為無惡意
correct = torch.sum(torch.eq(outputs, labels)).item()
return correct
# w2v.py
# 這個 block 是用來訓練 word to vector 的 word embedding
# 注意!這個 block 在訓練 word to vector 時是用 cpu,可能要花到 10 分鐘以上
import os
import numpy as np
import pandas as pd
import argparse
from gensim.models import word2vec,fasttext
from gensim.parsing.preprocessing import remove_stopwords
def train_word2vec(x):
# 訓練 word to vector 的 word embedding
#model = word2vec.Word2Vec(x, size=500, window=5, min_count=5, workers=12, iter=10, sg=1)
model = fasttext.FastText(min_count=5,size=500)
model.build_vocab(x)
model.train(x, total_examples=200000, epochs=10)
return model
if __name__ == "__main__":
print("loading training data ...")
train_x, y = load_training_data('training_label.txt')
train_x_no_label = load_training_data('training_nolabel.txt')
print("loading testing data ...")
test_x = load_testing_data('testing_data.txt')
#model = train_word2vec(train_x + train_x_no_label + test_x)
#re_test_x.append(remove_stopwords(for i in test_x))
#model = train_word2vec( train_x+ test_x)
print("saving model ...")
# model.save(os.path.join(path_prefix, 'model/w2v_all.model'))
#model.save(os.path.join(path_prefix, 'FT_all.model'))
# preprocess.py
# 這個 block 用來做 data 的預處理
from torch import nn
from gensim.models import Word2Vec,FastText
class Preprocess():
def __init__(self, sentences, sen_len, w2v_path="./w2v.model",w2v=True):
self.w2v_path = w2v_path
self.sentences = sentences
self.sen_len = sen_len
self.idx2word = []
self.word2idx = {}
self.embedding_matrix = []
self.words_over_len = 0
self.w2v=w2v
def get_w2v_model(self):
# 把之前訓練好的 word to vec 模型讀進來
if self.w2v:
self.embedding = Word2Vec.load(self.w2v_path)
else:
self.embedding = FastText.load(os.path.join(path_prefix, 'FT_all.model'))
self.embedding_dim = self.embedding.vector_size
def add_embedding(self, word):
# 把 word 加進 embedding,並賦予他一個隨機生成的 representation vector
# word 只會是 "<PAD>" 或 "<UNK>"
vector = torch.empty(1, self.embedding_dim)
torch.nn.init.uniform_(vector)
self.word2idx[word] = len(self.word2idx)
self.idx2word.append(word)
self.embedding_matrix = torch.cat([self.embedding_matrix, vector], 0)
def make_embedding(self, load=True):
print("Get embedding ...")
# 取得訓練好的 Word2vec word embedding
if load:
print("loading word to vec model ...")
self.get_w2v_model()
else:
raise NotImplementedError
# 製作一個 word2idx 的 dictionary
# 製作一個 idx2word 的 list
# 製作一個 word2vector 的 list
for i, word in enumerate(self.embedding.wv.vocab):
print('get words #{}'.format(i+1), end='\r')
#e.g. self.word2index['he'] = 1
#e.g. self.index2word[1] = 'he'
#e.g. self.vectors[1] = 'he' vector
self.word2idx[word] = len(self.word2idx)
self.idx2word.append(word)
self.embedding_matrix.append(self.embedding[word])
print('')
self.embedding_matrix = torch.tensor(self.embedding_matrix)
# 將 "<PAD>" 跟 "<UNK>" 加進 embedding 裡面
self.add_embedding("<PAD>")
self.add_embedding("<UNK>")
print("total words: {}".format(len(self.embedding_matrix)))
return self.embedding_matrix
def pad_sequence(self, sentence):
# 將每個句子變成一樣的長度
if len(sentence) > self.sen_len:
sentence = sentence[:self.sen_len]
self.words_over_len+=1
else:
pad_len = self.sen_len - len(sentence)
for _ in range(pad_len):
sentence.append(self.word2idx["<PAD>"])
assert len(sentence) == self.sen_len
return sentence
def sentence_word2idx(self):
# 把句子裡面的字轉成相對應的 index
sentence_list = []
unknown_words = 0
known_words = 0
for i, sen in enumerate(self.sentences):
print('sentence count #{}'.format(i+1), end='\r')
sentence_idx = []
for word in sen:
if (word in self.word2idx.keys()):
sentence_idx.append(self.word2idx[word])
known_words+=1
else:
sentence_idx.append(self.word2idx["<UNK>"])
unknown_words+=1
# 將每個句子變成一樣的長度
sentence_idx = self.pad_sequence(sentence_idx)
sentence_list.append(sentence_idx)
print("i=",i)
print("known words=", known_words)
print("unknown words=", unknown_words)
print("sentence over len=",self.words_over_len)
return torch.LongTensor(sentence_list)
def labels_to_tensor(self, y):
# 把 labels 轉成 tensor
y = [int(label) for label in y]
return torch.LongTensor(y)
# data.py
# 實作了 dataset 所需要的 '__init__', '__getitem__', '__len__'
# 好讓 dataloader 能使用
import torch
from torch.utils import data
class TwitterDataset(data.Dataset):
"""
Expected data shape like:(data_num, data_len)
Data can be a list of numpy array or a list of lists
input data shape : (data_num, seq_len, feature_dim)
__len__ will return the number of data
"""
def __init__(self, X, y):
self.data = X
self.label = y
def __getitem__(self, idx):
if self.label is None: return self.data[idx]
return self.data[idx], self.label[idx]
def __len__(self):
return len(self.data)
# model.py
# 這個 block 是要拿來訓練的模型
import torch
from torch import nn
class Attention(nn.Module):
def __init__(self, feature_dim, step_dim, bias=True, **kwargs):
super(Attention, self).__init__(**kwargs)
self.supports_masking = True
self.bias = bias
self.feature_dim = feature_dim
self.step_dim = step_dim
self.features_dim = 0
weight = torch.zeros(feature_dim, 1)
nn.init.kaiming_uniform_(weight)
self.weight = nn.Parameter(weight)
if bias:
self.b = nn.Parameter(torch.zeros(step_dim))
def forward(self, x, mask=None):
feature_dim = self.feature_dim
step_dim = self.step_dim
eij = torch.mm(
x.contiguous().view(-1, feature_dim),
self.weight
).view(-1, step_dim)
if self.bias:
eij = eij + self.b
eij = torch.tanh(eij)
a = torch.exp(eij)
if mask is not None:
a = a * mask
a = a / (torch.sum(a, 1, keepdim=True) + 1e-10)
weighted_input = x * torch.unsqueeze(a, -1)
return torch.sum(weighted_input, 1)
class LSTM_Net(nn.Module):
def __init__(self, embedding, embedding_dim, hidden_dim, num_layers, dropout=0.5, fix_embedding=True):
super(LSTM_Net, self).__init__()
# 製作 embedding layer
self.embedding = torch.nn.Embedding(embedding.size(0),embedding.size(1))
self.embedding.weight = torch.nn.Parameter(embedding)
# 是否將 embedding fix 住,如果 fix_embedding 為 False,在訓練過程中,embedding 也會跟著被訓練
self.embedding.weight.requires_grad = False if fix_embedding else True
self.embedding_dim = embedding.size(1)
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.dropout = dropout
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=num_layers,bidirectional=True, batch_first=True)
self.attention_layer = Attention(hidden_dim*2, 35)#sen_len=35
self.classifier = nn.Sequential( nn.Dropout(dropout),
nn.Linear(hidden_dim*2, 128),
nn.Linear(128, 1),
nn.Sigmoid()
)
self.end = nn.Sequential(
nn.MaxPool1d(3, stride=3),
nn.Linear(42, 1),
nn.Sigmoid() )
def forward(self, inputs):
#print("enter forward!!!")
#print("inputs dim=", inputs.size(0))
h0=torch.randn(self.num_layers,inputs.size(0),self.hidden_dim)
c0=torch.randn(self.num_layers,inputs.size(0),self.hidden_dim)
inputs = self.embedding(inputs)
x, _ = self.lstm(inputs, None)
# x 的 dimension (batch, seq_len, hidden_size)
# 取用 LSTM 最後一層的 hidden state
x = self.attention_layer(x)
#x = x[:, -1, :]
x = self.classifier(x)
#x = x.unsqueeze(1)
#x = self.end(x)
return x
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, logits=False, reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, inputs, targets):
if self.logits:
BCE_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduce=False)
else:
BCE_loss = F.binary_cross_entropy(inputs, targets, reduce=False)
pt = torch.exp(-BCE_loss)
F_loss = self.alpha * (1-pt)**self.gamma * BCE_loss
if self.reduce:
return torch.mean(F_loss)
else:
return F_loss
# train.py
# 這個 block 是用來訓練模型的
import torch
from torch import nn
import torch.optim as optim
import torch.nn.functional as F
def training(batch_size, n_epoch, lr, model_dir, train, valid, model, device):
total = sum(p.numel() for p in model.parameters())
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('\nstart training, parameter total:{}, trainable:{}\n'.format(total, trainable))
model.train() # 將 model 的模式設為 train,這樣 optimizer 就可以更新 model 的參數
#criterion = nn.BCELoss() # 定義損失函數,這裡我們使用 binary cross entropy loss
criterion = FocalLoss()
t_batch = len(train)
v_batch = len(valid)
#optimizer = optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-04, amsgrad=False) # 將模型的參數給 optimizer,並給予適當的 learning rate
optimizer = optim.AdamW(model.parameters(), lr=lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.01, amsgrad=False)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.25, patience=3, verbose=True, cooldown=0, eps=1e-08)
total_loss, total_acc, best_acc = 0, 0, 0
for epoch in range(n_epoch):
total_loss, total_acc = 0, 0
# 這段做 training
for i, (inputs, labels) in enumerate(train):
#print("inputs =", inputs)
#print("inputs shape =", inputs.shape)
inputs = inputs.to(device, dtype=torch.long) # device 為 "cuda",將 inputs 轉成 torch.cuda.LongTensor
labels = labels.to(device, dtype=torch.float) # device為 "cuda",將 labels 轉成 torch.cuda.FloatTensor,因為等等要餵進 criterion,所以型態要是 float
optimizer.zero_grad() # 由於 loss.backward() 的 gradient 會累加,所以每次餵完一個 batch 後需要歸零
outputs = model(inputs) # 將 input 餵給模型
outputs = outputs.squeeze() # 去掉最外面的 dimension,好讓 outputs 可以餵進 criterion()
loss = criterion(outputs, labels) # 計算此時模型的 training loss
loss.backward() # 算 loss 的 gradient
optimizer.step() # 更新訓練模型的參數
correct = evaluation(outputs, labels) # 計算此時模型的 training accuracy
total_acc += (correct / batch_size)
total_loss += loss.item()
print('[ Epoch{}: {}/{} ] loss:{:.3f} acc:{:.3f} '.format(
epoch+1, i+1, t_batch, loss.item(), correct*100/batch_size), end='\r')
print('\nTrain | Loss:{:.5f} Acc: {:.3f}'.format(total_loss/t_batch, total_acc/t_batch*100))
# 這段做 validation
model.eval() # 將 model 的模式設為 eval,這樣 model 的參數就會固定住
with torch.no_grad():
total_loss, total_acc = 0, 0
for i, (inputs, labels) in enumerate(valid):
inputs = inputs.to(device, dtype=torch.long) # device 為 "cuda",將 inputs 轉成 torch.cuda.LongTensor
labels = labels.to(device, dtype=torch.float) # device 為 "cuda",將 labels 轉成 torch.cuda.FloatTensor,因為等等要餵進 criterion,所以型態要是 float
outputs = model(inputs) # 將 input 餵給模型
outputs = outputs.squeeze() # 去掉最外面的 dimension,好讓 outputs 可以餵進 criterion()
loss = criterion(outputs, labels) # 計算此時模型的 validation loss
correct = evaluation(outputs, labels) # 計算此時模型的 validation accuracy
total_acc += (correct / batch_size)
total_loss += loss.item()
print("Valid | Loss:{:.5f} Acc: {:.3f} ".format(total_loss/v_batch, total_acc/v_batch*100))
if total_acc > best_acc:
# 如果 validation 的結果優於之前所有的結果,就把當下的模型存下來以備之後做預測時使用
best_acc = total_acc
#torch.save(model, "{}/val_acc_{:.3f}.model".format(model_dir,total_acc/v_batch*100))
torch.save(model, "{}/ckpt.model".format(model_dir))
print('saving model with acc {:.3f}'.format(total_acc/v_batch*100))
print('-----------------------------------------------')
model.train() # 將 model 的模式設為 train,這樣 optimizer 就可以更新 model 的參數(因為剛剛轉成 eval 模式)
scheduler.step(total_loss)
# test.py
# 這個 block 用來對 testing_data.txt 做預測
import torch
from torch import nn
import torch.optim as optim
import torch.nn.functional as F
def testing(batch_size, test_loader, model, device):
model.eval()
ret_output = []
with torch.no_grad():
for i, inputs in enumerate(test_loader):
inputs = inputs.to(device, dtype=torch.long)
outputs = model(inputs)
outputs = outputs.squeeze()
outputs[outputs>=0.5] = 1 # 大於等於 0.5 為負面
outputs[outputs<0.5] = 0 # 小於 0.5 為正面
ret_output += outputs.int().tolist()
return ret_output
# main.py
import os
import torch
import argparse
import numpy as np
from torch import nn
from gensim.models import word2vec
from sklearn.model_selection import train_test_split
# 通過 torch.cuda.is_available() 的回傳值進行判斷是否有使用 GPU 的環境,如果有的話 device 就設為 "cuda",沒有的話就設為 "cpu"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 處理好各個 data 的路徑
train_with_label = os.path.join(path_prefix, 'training_label.txt')
train_no_label = os.path.join(path_prefix, 'training_nolabel.txt')
testing_data = os.path.join(path_prefix, 'testing_data.txt')
w2v_path = os.path.join(path_prefix, 'w2v_all.model') # 處理 word to vec model 的路徑
# 定義句子長度、要不要固定 embedding、batch 大小、要訓練幾個 epoch、learning rate 的值、model 的資料夾路徑
sen_len = 35
fix_embedding = True # fix embedding during training
batch_size = 8
epoch = 25
lr = 0.001#original 0.001
# model_dir = os.path.join(path_prefix, 'model/') # model directory for checkpoint model
model_dir = path_prefix # model directory for checkpoint model
print("loading data ...") # 把 'training_label.txt' 跟 'training_nolabel.txt' 讀進來
train_x, y = load_training_data(train_with_label)
train_x_no_label = load_training_data(train_no_label)
# 對 input 跟 labels 做預處理
preprocess_w2v = Preprocess(train_x, sen_len, w2v_path=w2v_path,w2v=True)
embedding_w2v = preprocess_w2v.make_embedding(load=True)
preprocess_FT = Preprocess(train_x, sen_len, w2v_path=w2v_path,w2v=False)
embedding_FT = preprocess_FT.make_embedding(load=True)
concat_embedding = np.column_stack((embedding_w2v, embedding_FT))
concat_embedding=torch.from_numpy(concat_embedding)
#print(type(concat_embedding))
train_x = preprocess_w2v.sentence_word2idx()
y = preprocess_w2v.labels_to_tensor(y)
#BI-hid_dim150-81.27 100-81.086 200-81.509 250-81.240
#SI-hid_dim150------ 100------- 200-81.285(81.275 2layer) 250-------
# 製作一個 model 的對象
model = LSTM_Net(concat_embedding, embedding_dim=1000, hidden_dim=200, num_layers=2, dropout=0., fix_embedding=fix_embedding)
model = model.to(device) # device為 "cuda",model 使用 GPU 來訓練(餵進去的 inputs 也需要是 cuda tensor)
print(model)
# 把 data 分為 training data 跟 validation data(將一部份 training data 拿去當作 validation data)
X_train, X_val, y_train, y_val = train_x[:180000], train_x[180000:], y[:180000], y[180000:]
# 把 data 做成 dataset 供 dataloader 取用
train_dataset = TwitterDataset(X=X_train, y=y_train)
val_dataset = TwitterDataset(X=X_val, y=y_val)
# 把 data 轉成 batch of tensors
train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True,
num_workers = 8)
val_loader = torch.utils.data.DataLoader(dataset = val_dataset,
batch_size = batch_size,
shuffle = False,
num_workers = 8)
# 開始訓練
training(batch_size, epoch, lr, model_dir, train_loader, val_loader, model, device)
# 開始測試模型並做預測
print("loading testing data ...")
test_x = load_testing_data(testing_data)
preprocess = Preprocess(test_x, sen_len, w2v_path=w2v_path)
embedding = preprocess.make_embedding(load=True)
test_x = preprocess.sentence_word2idx()
test_dataset = TwitterDataset(X=test_x, y=None)
test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
batch_size = batch_size,
shuffle = False,
num_workers = 8)
print('\nload model ...')
model = torch.load(os.path.join(model_dir, 'ckpt.model'))
outputs = testing(batch_size, test_loader, model, device)
# 寫到 csv 檔案供上傳 Kaggle
tmp = pd.DataFrame({"id":[str(i) for i in range(len(test_x))],"label":outputs})
print("save csv ...")
tmp.to_csv(os.path.join(path_prefix, 'predict.csv'), index=False)
print("Finish Predicting")