-
Notifications
You must be signed in to change notification settings - Fork 0
/
gg_train_v4.py
202 lines (179 loc) · 7.67 KB
/
gg_train_v4.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
import os
import pandas as pd
from konlpy.tag import Mecab
from kobert import get_tokenizer
from kobert import get_pytorch_kobert_model
from sklearn.model_selection import train_test_split
from transformers import AdamW
from transformers import Adafactor
from transformers.optimization import get_cosine_schedule_with_warmup
import torch
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import gluonnlp as nlp
import numpy as np
import numpy
from tqdm import tqdm
from time import time
from time import ctime
import subprocess
subprocess.call(['sh','./logo.sh'])
tt = ctime(time())
max_len = 64
batch_size = 64
warmup_ratio = 0.1
num_epochs = 50
max_grad_norm = 1
log_interval = 200
learning_rate = 5e-5
bertmodel,vocab = get_pytorch_kobert_model()
tokenizer = get_tokenizer()
m = Mecab(dicpath='/toy/mecab-ko-dic-2.1.1-20180720')
tok = nlp.data.BERTSPTokenizer(m,vocab,lower=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#BERT Dataset class
class BERTDataset(Dataset):
def __init__(self, dataset, sent_idx, label_idx, bert_tokenizer, max_len,
pad, pair):
transform = nlp.data.BERTSentenceTransform(
bert_tokenizer, max_seq_length=max_len, pad=pad, pair=pair)
self.sentences = [transform([i[sent_idx]]) for i in dataset]
self.labels = [np.int32(i[label_idx]) for i in dataset]
def __getitem__(self, i):
return (self.sentences[i] + (self.labels[i], ))
def __len__(self):
return (len(self.labels))
class BERTClassifier(nn.Module):
def __init__(self,
bert,
hidden_size = 768,
num_classes=44,
dr_rate=None,
params=None):
super(BERTClassifier, self).__init__()
self.bert = bert
self.dr_rate = dr_rate
self.classifier = nn.Linear(hidden_size , num_classes)
if dr_rate:
self.dropout = nn.Dropout(p=dr_rate)
def gen_attention_mask(self, token_ids, valid_length):
attention_mask = torch.zeros_like(token_ids)
for i, v in enumerate(valid_length):
attention_mask[i][:v] = 1
return attention_mask.float()
def forward(self, token_ids, valid_length, segment_ids):
attention_mask = self.gen_attention_mask(token_ids, valid_length)
_, pooler = self.bert(input_ids = token_ids, token_type_ids = segment_ids.long(), attention_mask = attention_mask.float().to(token_ids.device))
if self.dr_rate:
out = self.dropout(pooler)
return self.classifier(out)
def main():
# Setting parameters
path = '/toy/LG_data/'
file_list = os.listdir(path)
d_test , d_result , d_target , d_target_num = [], [], [], []
for file in file_list:
if file[-1]=='x':
xl = pd.ExcelFile(path+file)
t_test=[]
t_result=[]
for i in range(1,len(xl.sheet_names)):
t_test = xl.parse(xl.sheet_names[i])['개발자 TESTcase']
t_result = xl.parse(xl.sheet_names[i])['개발자Result']
for j in range(len(t_test)):
if type(t_test[j])==str and type(t_result[j])==str:
d_test.append(t_test[j])
d_result.append(t_result[j])
d_target.append(xl.sheet_names[i])
tar = list(set(d_target))
tar.sort()
for i in d_target:
for j in range(len(tar)):
if i == tar[j]:
d_target_num.append(j)
for i in range(len(d_target_num)):
if d_target[i] != tar[d_target_num[i]]:
print(i)
d_concat=[]
for i in range(len(d_test)):
d_concat.append(d_test[i]+'\n'+d_result[i])
data_list=[]
for q,label in zip(d_test,d_target_num):
data=[]
data.append(q)
data.append(str(label))
data_list.append(data)
for q,label in zip(d_result,d_target_num):
data=[]
data.append(q)
data.append(str(label))
data_list.append(data)
for q,label in zip(d_concat,d_target_num):
data=[]
data.append(q)
data.append(str(label))
data_list.append(data)
dataset_train,dataset_test = train_test_split(data_list, test_size = 0.2, random_state = 0)
data_train = BERTDataset(dataset_train, 0, 1, tok, max_len, True, False)
data_test = BERTDataset(dataset_test, 0, 1, tok, max_len, True, False)
train_dataloader = torch.utils.data.DataLoader(data_train, batch_size=batch_size, num_workers=5)
test_dataloader = torch.utils.data.DataLoader(data_test, batch_size=batch_size, num_workers=5)
#device = torch.device('cpu')
model = BERTClassifier(bertmodel, dr_rate=0.5).to(device)
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
#optimizer = Adafactor(optimizer_grouped_parameters, lr=learning_rate)
optimizer = AdamW(optimizer_grouped_parameters, lr=learning_rate)
loss_fn = nn.CrossEntropyLoss()
t_total = len(train_dataloader) * num_epochs
warmup_step = int(t_total * warmup_ratio)
scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=warmup_step, num_training_steps=t_total)
def calc_accuracy(X,Y):
max_vals, max_indices = torch.max(X, 1)
train_acc = (max_indices == Y).sum().data.cpu().numpy()/max_indices.size()[0]
return train_acc
train_dataloader
for e in range(num_epochs+1):
train_acc = 0.0
test_acc = 0.0
model.train()
for batch_id, (token_ids, valid_length, segment_ids, label) in enumerate(tqdm(train_dataloader)):
optimizer.zero_grad()
token_ids = token_ids.long().to(device)
segment_ids = segment_ids.long().to(device)
valid_length= valid_length
label = label.long().to(device)
out = model(token_ids, valid_length, segment_ids)
loss = loss_fn(out, label)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
optimizer.step()
scheduler.step() # Update learning rate schedule
train_acc += calc_accuracy(out, label)
if batch_id % log_interval == 0:
print("epoch {} batch id {} loss {} train acc {}".format(e+1, batch_id+1, loss.data.cpu().numpy(), train_acc / (batch_id+1)))
print("epoch {} train acc {}".format(e+1, train_acc / (batch_id+1)))
model.eval()
for batch_id, (token_ids, valid_length, segment_ids, label) in enumerate(tqdm(test_dataloader)):
token_ids = token_ids.long().to(device)
segment_ids = segment_ids.long().to(device)
valid_length= valid_length
label = label.long().to(device)
out = model(token_ids, valid_length, segment_ids)
test_acc += calc_accuracy(out, label)
print("epoch {} test acc {}".format(e+1, test_acc / (batch_id+1)))
m_path = '/toy/LG_model/'
#torch.save(model,m_path,'LG_model' + tt + '.pt')
if e%10==0:
torch.save(model,m_path+'kobert_model'+str(e)+'.pt')
#torch.save(model.state_dict(), m_path + 'model_state_dict' + tt + '.pt')
torch.save(model.state_dict(), m_path + 'kobert_state'+str(e)+'.pt')
torch.save(model,m_path+'kobert_model'+str(e)+'.pt')
torch.save(model.state_dict(),m_path+'kobert_state'+str(e)+'.pt')
if __name__ == '__main__':
main()