-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunUtil.py
86 lines (71 loc) · 2.51 KB
/
runUtil.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
# coding = utf-8
# -*- coding:utf-8 -*-
import config
import torch
from torch import nn
config.setup_seed()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
labelDict = {
0: 'positive',
1: 'negative',
2: 'neutral'
}
def train(model, optimizer, train_loader, val_loader):
criterion = nn.CrossEntropyLoss()
best_accuracy = 0
for e in range(config.epoch):
for i, data in enumerate(train_loader):
model.train()
labels = data['tag'].to(device)
optimizer.zero_grad()
out = model(data)
# print(labels.device)
# print(out.device)
loss = criterion(out, labels)
loss.backward()
optimizer.step()
out = out.argmax(dim=1)
accuracy = (out == labels).sum().item() / len(labels)
print('epoch:', e + 1, 'step:', i + 1, 'loss:', loss.item(), 'train accuracy:', accuracy)
if (i + 1) % 10 == 0:
print('validation accuracy:', test(model, val_loader))
accuracy = test(model, val_loader)
print('epoch:', e + 1, 'validation accuracy:', accuracy)
if accuracy >= best_accuracy:
best_accuracy = accuracy
torch.save(model, config.cache_model_path)
print('saved the model as', config.cache_model_path)
def test(model, data_loader):
model.eval()
correct = 0
total = 0
for i, data in enumerate(data_loader):
with torch.no_grad():
labels = data['tag'].to(device)
out = model(data)
out = out.argmax(dim=1)
correct += (out == labels).sum().item()
total += len(labels)
return correct / total
def predict(model, data_loader):
model.eval()
result = dict()
for i, data in enumerate(data_loader):
with torch.no_grad():
out = model(data)
out = out.argmax(dim=1)
ids = data['guid']
for j in range(len(ids)):
guid = str(ids[j].item())
tag = labelDict[out[j].item()]
result[guid] = tag
# print(guid, tag)
with open(config.prediction_path, 'w', encoding='utf-8') as wfs:
wfs.write('guid,tag\n')
with open(config.test_without_label_path, 'r', encoding='utf-8') as rfs:
rfs.readline()
for line in rfs:
guid = line[0: line.find(',')]
wfs.write(guid + ',' + result[guid] + '\n')
if __name__ == '__main__':
print(device)