-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
60 lines (51 loc) · 1.71 KB
/
Copy pathtrain.py
File metadata and controls
60 lines (51 loc) · 1.71 KB
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
import torch
import torch.nn as nn
from torch import optim
from model import LeNet
from data import data_train_loader
from matplotlib import pyplot as plt
import torchvision.transforms as transforms
def train():
model = LeNet()
model.train()
lr = 0.01
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9, weight_decay=5e-4)
train_acc = []
loss_data = []
train_loss = 0
correct = 0
total = 0
epoch = 16
sample = []
for time in range(epoch):
for batch_idx, (inputs, targets) in enumerate(data_train_loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss = loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
print(batch_idx, len(data_train_loader),
'Loss: %3.f | Acc: %.3f%%(%d/%d)' % (
train_loss, 100. * correct / total, correct, total))
train_acc.append(correct / total)
loss_data.append(train_loss)
for i in range(len(train_acc)):
sample.append(i)
plt.figure()
plt.plot(sample, train_acc, 'blue', label='Training accuracy')
plt.xlabel('Epoch')
plt.title('Training Process')
plt.show()
plt.figure()
plt.plot(sample, loss_data, 'red', label='Training Loss')
plt.xlabel('Epoch')
plt.title('Training Process')
plt.show()
torch.save(model.state_dict(), 'model.pth')
if __name__ == '__main__':
train()