-
Notifications
You must be signed in to change notification settings - Fork 8
/
train.py
253 lines (201 loc) · 9.17 KB
/
train.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
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import numpy as np
from sklearn import metrics
import torch.nn as nn
import argparse
import torch.distributed as dist
import torch.utils.data.distributed
from matplotlib import pyplot as plt
import NNModel
from NNModel import BERTClass
from dataSet import PhpDataset
import transformers.trainer
import torch.nn.functional as F
import psutil
device = 'cuda' if torch.cuda.is_available() else 'cpu'
blue = lambda x: '\033[94m' + x + '\033[0m'
loss_list = []
acc_list = []
loss_list_val = []
acc_list_val = []
batch_size = 32
def detect_cpu_mem():
"""检测CPU和内存占用率"""
print("进行mem和cpu检测:")
# 内存检测
mem = psutil.virtual_memory().percent
# psutil检测cpu时间隔至少3s以上
cpu = psutil.cpu_percent(3)
print("当前内存占用率:" + str(mem) + "%")
print("当前CPU占用率:" + str(cpu) + "%")
return mem, cpu
def label_smoothing(inputs, epsilon=0.1):
'''Applies label smoothing. See 5.4 and https://arxiv.org/abs/1512.00567.
inputs: 3d tensor. [N, T, V], where V is the number of vocabulary.
epsilon: Smoothing rate.
For example,
```
import tensorflow as tf
inputs = tf.convert_to_tensor([[[0, 0, 1],
[0, 1, 0],
[1, 0, 0]],
[[1, 0, 0],
[1, 0, 0],
[0, 1, 0]]], tf.float32)
outputs = label_smoothing(inputs)
with tf.Session() as sess:
print(sess.run([outputs]))
>>
[array([[[ 0.03333334, 0.03333334, 0.93333334],
[ 0.03333334, 0.93333334, 0.03333334],
[ 0.93333334, 0.03333334, 0.03333334]],
[[ 0.93333334, 0.03333334, 0.03333334],
[ 0.93333334, 0.03333334, 0.03333334],
[ 0.03333334, 0.93333334, 0.03333334]]], dtype=float32)]
```
'''
# V = inputs.size().as_list()[-1] # number of channels
return ((1 - epsilon) * inputs) + (epsilon / 2)
if __name__ == '__main__':
dataset = PhpDataset()
# train_sampler = torch.utils.data.distributed.DistributedSampler(dataset)
length = len(dataset)
train_size, validate_size = int(0.8 * length), int(0.2 * length)
train_set, validate_set = torch.utils.data.random_split(dataset, [train_size+1, validate_size])
print([train_size, validate_size])
test_set, validate_set = torch.utils.data.random_split(validate_set, [656, 655])
print('train_size:', len(train_set))
print('test_set:', len(test_set))
print('validate_set:', len(validate_set))
training_loader = DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True)
val_loader = DataLoader(dataset=validate_set, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True)
testing_loader = DataLoader(dataset=test_set, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True)
# dist.init_process_group(backend='nccl')
gpus= [0, 1, 2, 3, 4, 5, 6, 7]
# gpus = [0]
print("model initing")
# model = BERTClass()
# model = NNModel.CodeBERTClassifer()
model = NNModel.TextCNNClassifer()
model.cuda()
model = torch.nn.DataParallel(model.cuda(), device_ids=gpus, output_device=gpus[0])
# nn.TransformerDecoder
def loss_fn(outputs, targets):
return torch.nn.BCEWithLogitsLoss()(outputs, targets)
# optimizer = torch.optim.Adam(params=model.parameters(), lr=1e-05)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-05, betas=(0.9, 0.999))
# scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.5)
# optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
# scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.01, max_lr=0.1)
print("training!!!")
num_batch = len(train_set) / batch_size
# print(len(train_set))
# print(num_batch)
def train(epoch):
model.train()
for _, data in enumerate(training_loader, 0):
targets = data['targets']
targets = targets.view(-1, 1)
targets = torch.LongTensor(targets)
targets = torch.zeros(batch_size, 2).scatter_(1, targets, 1)
ids = data['ids'].to(device, dtype=torch.long).cuda(non_blocking=True)
mask = data['mask'].to(device, dtype=torch.long).cuda(non_blocking=True)
token_type_ids = data['token_type_ids'].to(device, dtype=torch.long).cuda(non_blocking=True)
targets = targets.to(device, dtype=torch.float).cuda(non_blocking=True)
targets = label_smoothing(targets)
# print(targets)
outputs = model(ids, mask, token_type_ids)
optimizer.zero_grad()
# print(outputs)
loss = loss_fn(outputs, targets)
# print(outputs, targets)
# loss.backward()
# loss_list.append(loss.cpu().detach().numpy().tolist())
# acc_list.append()
# print(loss_list)
pred_choice = outputs.max(1)[1]
targets = targets.max(1)[1]
# print(pred_choice, targets)
correct = pred_choice.eq(targets).cpu().sum()
print('[%d: %d/%d] train loss: %f accuracy: %f' % (epoch, _, num_batch, loss.item(), correct.item() / float(batch_size)))
loss_list.append(loss.item())
acc_list.append(correct.item() / float(batch_size))
# print(_)
if _ % 10 == 0:
# print(f'Epoch: {epoch}, Loss: {loss.item()}')
# detect_cpu_mem()
j, data = next(enumerate(val_loader, 0))
# print(j, data)
targets = data['targets']
targets = targets.view(-1, 1)
targets = torch.LongTensor(targets)
targets = torch.zeros(batch_size, 2).scatter_(1, targets, 1)
ids = data['ids'].to(device, dtype=torch.long).cuda(non_blocking=True)
mask = data['mask'].to(device, dtype=torch.long).cuda(non_blocking=True)
token_type_ids = data['token_type_ids'].to(device, dtype=torch.long).cuda(non_blocking=True)
target = targets.to(device, dtype=torch.float).cuda(non_blocking=True)
pred = model(ids, mask, token_type_ids)
# print(pred, target)
loss = loss_fn(pred, target)
pred_choice = pred.max(1)[1]
target = target.max(1)[1]
correct = pred_choice.eq(target).cpu().sum()
print('[%d: %d/%d] %s loss: %f accuracy: %f' % (epoch, _, num_batch, blue('val'), loss.item(), correct.item() / float(batch_size)))
# loss_list_val = []
# acc_list_val = []
loss_list_val.append(loss.item())
acc_list_val.append(correct.item() / float(batch_size))
optimizer.zero_grad()
loss.backward()
optimizer.step()
# scheduler.step()
torch.save(model.module.state_dict(), '%s/cls_model_%d.pth' % ('model', epoch))
def validation(epoch):
model.eval()
fin_targets = []
fin_outputs = []
with torch.no_grad():
for _, data in enumerate(testing_loader, 0):
targets = data['targets']
targets = targets.view(-1, 1)
targets = torch.LongTensor(targets)
targets = torch.zeros(batch_size, 2).scatter_(1, targets, 1)
ids = data['ids'].to(device, dtype=torch.long)
mask = data['mask'].to(device, dtype=torch.long)
token_type_ids = data['token_type_ids'].to(device, dtype=torch.long)
targets = targets.to(device, dtype=torch.float)
outputs = model(ids, mask, token_type_ids)
outputs = outputs.max(1)[1]
targets = targets.max(1)[1]
# print(outputs)
# print(targets)
# fin_targets.extend(targets.cpu().detach().numpy().tolist())
# fin_outputs.extend(torch.sigmoid(outputs.float()).cpu().detach().numpy().tolist())
return outputs.cpu(), targets.cpu()
for epoch in range(10):
train(epoch)
for epoch in range(1):
outputs, targets = validation(epoch)
outputs = np.array(outputs) >= 0.5
accuracy = metrics.accuracy_score(targets, outputs)
f1_score_micro = metrics.f1_score(targets, outputs, average='micro')
f1_score_macro = metrics.f1_score(targets, outputs, average='macro')
print(f"Accuracy Score = {accuracy}")
print(f"F1 Score (Micro) = {f1_score_micro}")
print(f"F1 Score (Macro) = {f1_score_macro}")
print(loss_list)
print(acc_list)
print(loss_list_val)
print(acc_list_val)
# torch.load()
for epoch in range(1):
outputs, targets = validation(epoch)
outputs = np.array(outputs) >= 0.5
accuracy = metrics.accuracy_score(targets, outputs)
f1_score_micro = metrics.f1_score(targets, outputs, average='micro')
f1_score_macro = metrics.f1_score(targets, outputs, average='macro')
print(f"Accuracy Score = {accuracy}")
print(f"F1 Score (Micro) = {f1_score_micro}")
print(f"F1 Score (Macro) = {f1_score_macro}")