-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_fcn.py
309 lines (253 loc) · 11.3 KB
/
train_fcn.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
import torch
from torch import nn as nn
import os
from torch.utils.data import DataLoader
from torch.nn import functional as F
from torch import cuda
from sklearn import metrics
import numpy as np
from transformers import AdamW
from transformers import get_scheduler
from entities import VariantSixDataset
from model import PatchClassifier
import pandas as pd
from tqdm import tqdm
import utils
dataset_name = 'ase_dataset_sept_19_2021.csv'
# dataset_name = 'huawei_sub_dataset.csv'
directory = os.path.dirname(os.path.abspath(__file__))
model_folder_path = os.path.join(directory, 'model')
NUMBER_OF_EPOCHS = 100
TRAIN_BATCH_SIZE = 512
VALIDATION_BATCH_SIZE = 512
TEST_BATCH_SIZE = 512
TRAIN_PARAMS = {'batch_size': TRAIN_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
VALIDATION_PARAMS = {'batch_size': VALIDATION_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
TEST_PARAMS = {'batch_size': TEST_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
LEARNING_RATE = 1e-4
use_cuda = cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
torch.backends.cudnn.benchmark = True
false_cases = []
CODE_LENGTH = 512
HIDDEN_DIM = 768
NUMBER_OF_LABELS = 2
model_path_prefix = model_folder_path + '/patch_classifier_variant_6_best_java_11112021_model_'
best_java_model_path = model_folder_path + '/patch_classifier_variant_6_best_java_11112021_model.sav'
best_python_model_path = model_folder_path + '/patch_classifier_variant_6_best_python_11112021_model.sav'
def get_data():
print("Reading dataset...")
df = pd.read_csv(dataset_name)
df = df[['commit_id', 'repo', 'partition', 'PL', 'label']]
items = df.to_numpy().tolist()
label_train = []
label_val = []
label_test_java = []
label_test_python = []
url_train = []
url_val = []
url_test_java = []
url_test_python = []
for item in tqdm(items):
commit_id = item[0]
repo = item[1]
url = repo + '/commit/' + commit_id
partition = item[2]
pl = item[3]
label = item[4]
if partition == 'train':
if url not in url_train:
url_train.append(url)
label_train.append(label)
elif partition == 'val':
if url not in url_val:
url_val.append(url)
label_val.append(label)
elif partition == 'test':
if pl == 'java':
if url not in url_test_java:
url_test_java.append(url)
label_test_java.append(label)
else:
if url not in url_test_python:
url_test_python.append(url)
label_test_python.append(label)
else:
Exception("Invalid partition: {}".format(partition))
print("Finish reading dataset")
return url_train, url_val, url_test_java, url_test_python, label_train, label_val, label_test_java, label_test_python
def predict_test_data(model, testing_generator, device):
y_pred = []
y_test = []
probs = []
test_ids = []
with torch.no_grad():
model.eval()
for ids, url, before_batch, after_batch, label_batch in testing_generator:
before_batch, after_batch, label_batch = before_batch.to(device), after_batch.to(device), label_batch.to(device)
outs = model(before_batch, after_batch)
outs = F.softmax(outs, dim=1)
y_pred.extend(torch.argmax(outs, dim=1).tolist())
y_test.extend(label_batch.tolist())
probs.extend(outs[:, 1].tolist())
test_ids.extend(label_batch.tolist())
precision = metrics.precision_score(y_pred=y_pred, y_true=y_test)
recall = metrics.recall_score(y_pred=y_pred, y_true=y_test)
f1 = metrics.f1_score(y_pred=y_pred, y_true=y_test)
try:
auc = metrics.roc_auc_score(y_true=y_test, y_score=probs)
auc_pr = metrics.average_precision_score(y_score=probs, y_true=y_test)
except Exception:
auc = 0
auc_pr = 0
print("Finish testing")
return precision, recall, f1, auc
def train(model, training_generator, val_java_generator, val_python_generator, test_java_generator, test_python_generator):
loss_function = nn.NLLLoss()
optimizer = AdamW(model.parameters(), lr=LEARNING_RATE)
num_training_steps = NUMBER_OF_EPOCHS * len(training_generator)
lr_scheduler = get_scheduler(
"linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps
)
train_losses, valid_losses = [], []
print("Training...")
best_auc_val_java = 0
best_auc_val_python = 0
for epoch in range(NUMBER_OF_EPOCHS):
model.train()
print("Calculating commit training loss...")
current_batch = 0
for ids, url, before_batch, after_batch, label_batch in training_generator:
before_batch, after_batch, label_batch = before_batch.to(device), after_batch.to(device), label_batch.to(device)
outs = model(before_batch, after_batch)
outs = F.log_softmax(outs, dim=1)
loss = loss_function(outs, label_batch)
train_losses.append(loss.item())
model.zero_grad()
loss.backward()
optimizer.step()
lr_scheduler.step()
current_batch += 1
if current_batch % 50 == 0:
print("Train commit iter {}, total loss {}, average loss {}".format(current_batch, np.sum(train_losses),
np.average(train_losses)))
print("epoch {}, training commit loss {}".format(epoch, np.sum(train_losses)))
with torch.no_grad():
model.eval()
print("Result on Java validation dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=val_java_generator,
device=device)
if auc > best_auc_val_java:
best_auc_val_java = auc
if torch.cuda.device_count() > 1:
torch.save(model.module.state_dict(),
best_java_model_path)
else:
torch.save(model.state_dict(), best_java_model_path)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
print("Result on Python validation dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=val_python_generator,
device=device)
if auc > best_auc_val_python:
best_auc_val_python = auc
if torch.cuda.device_count() > 1:
torch.save(model.module.state_dict(),
best_python_model_path)
else:
torch.save(model.state_dict(), best_python_model_path)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
if torch.cuda.device_count() > 1:
torch.save(model.module.state_dict(),
model_path_prefix + '_patch_classifier_epoc_' + str(epoch) + '.sav')
else:
torch.save(model.state_dict(), model_path_prefix + '_patch_classifier.sav')
print("Result on Java testing dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=test_java_generator,
device=device)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
print("Result on Python testing dataset...")
precision, recall, f1, auc = predict_test_data(model=model,
testing_generator=test_python_generator,
device=device)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
def do_train():
print("Dataset name: {}".format(dataset_name))
url_data, label_data = utils.get_data(dataset_name)
train_ids, val_java_ids, val_python_ids, test_java_ids, test_python_ids = [], [], [], [], []
index = 0
id_to_url = {}
id_to_label = {}
print("Preparing data indices...")
for i, url in enumerate(url_data['train']):
train_ids.append(index)
label = label_data['train'][i]
id_to_url[index] = url
id_to_label[index] = label
index += 1
for i, url in enumerate(url_data['val_java']):
val_java_ids.append(index)
label = label_data['val_java'][i]
id_to_url[index] = url
id_to_label[index] = label
index += 1
for i, url in enumerate(url_data['val_python']):
val_python_ids.append(index)
label = label_data['val_python'][i]
id_to_url[index] = url
id_to_label[index] = label
index += 1
for i, url in enumerate(url_data['test_java']):
test_java_ids.append(index)
label = label_data['test_java'][i]
id_to_url[index] = url
id_to_label[index] = label
index += 1
for i, url in enumerate(url_data['test_python']):
test_python_ids.append(index)
label = label_data['test_python'][i]
id_to_url[index] = url
id_to_label[index] = label
index += 1
print("Preparing dataset...")
training_set = VariantSixDataset(train_ids, id_to_label, id_to_url)
val_java_set = VariantSixDataset(val_java_ids, id_to_label, id_to_url)
val_python_set = VariantSixDataset(val_python_ids, id_to_label, id_to_url)
test_java_set = VariantSixDataset(test_java_ids, id_to_label, id_to_url)
test_python_set = VariantSixDataset(test_python_ids, id_to_label, id_to_url)
training_generator = DataLoader(training_set, **TRAIN_PARAMS)
val_java_generator = DataLoader(val_java_set, **VALIDATION_PARAMS)
val_python_generator = DataLoader(val_python_set, **VALIDATION_PARAMS)
test_java_generator = DataLoader(test_java_set, **TEST_PARAMS)
test_python_generator = DataLoader(test_python_set, **TEST_PARAMS)
model = PatchClassifier()
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
train(model=model, training_generator=training_generator, val_java_generator=val_java_generator, val_python_generator=val_python_generator,
test_java_generator=test_java_generator, test_python_generator=test_python_generator)
if __name__ == '__main__':
do_train()