-
Notifications
You must be signed in to change notification settings - Fork 18
/
train.py
85 lines (75 loc) · 2.67 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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from model import CNN
from data import KddData
# 神经网络参数
batch_size = 128
learning_rate = 1e-2
num_epoches = 20
USE_GPU = torch.cuda.is_available()
dataset = KddData(batch_size)
model = CNN(1, 23)
if USE_GPU:
model = model.cuda()
def train():
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(num_epoches):
print('epoch {}'.format(epoch + 1))
print('*' * 10)
running_loss = 0.0
running_acc = 0.0
for i, data in enumerate(dataset.train_dataloader, 1):
img, label = data
if USE_GPU:
img = img.cuda()
label = label.cuda()
img = Variable(img)
label = Variable(label)
# 向前传播
out = model(img)
loss = criterion(out, label)
running_loss += loss.item() * label.size(0)
_, pred = torch.max(out, 1)
num_correct = (pred == label).sum()
accuracy = (pred == label).float().mean()
running_acc += num_correct.item()
# 向后传播
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Finish {} epoch, Loss: {:.6f}, Acc: {:.6f}'.format(
epoch + 1, running_loss / (len(dataset.train_dataset)), running_acc / (len(
dataset.train_dataset))))
model.eval()
eval_loss = 0
eval_acc = 0
for data in dataset.test_dataloader:
img, label = data
if USE_GPU:
img = Variable(img, volatile=True).cuda()
label = Variable(label, volatile=True).cuda()
else:
img = Variable(img, volatile=True)
label = Variable(label, volatile=True)
out = model(img)
loss = criterion(out, label)
eval_loss += loss.item() * label.size(0)
_, pred = torch.max(out, 1)
num_correct = (pred == label).sum()
eval_acc += num_correct.item()
print('Test Loss: {:.6f}, Acc: {:.6f}'.format(eval_loss / (len(
dataset.test_dataset)), eval_acc / (len(dataset.test_dataset))))
print()
def predict(data, multiple=False):
_data = dataset.encode(data)
_data = torch.from_numpy(
np.pad(_data, (0, 64 - len(_data)), 'constant').astype(np.float32)
).reshape(-1, 1, 8, 8).cuda()
_out = int(torch.max(model(_data).data, 1)[1].cpu().numpy())
return dataset.decode(_out, label=True)
if __name__ == '__main__':
train()