-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_py.py
66 lines (53 loc) · 2 KB
/
train_py.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
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from Model import *
train_datasets = torchvision.datasets.CIFAR10('./Pytorch',transform=torchvision.transforms.ToTensor())
test_datasets = torchvision.datasets.CIFAR10('./Pytorch',train=False,transform=torchvision.transforms.ToTensor())
trains_load = DataLoader(train_datasets,batch_size=64)
test_load = DataLoader(test_datasets,batch_size=64)
writer = SummaryWriter('train_py')
epoch = 4
train_step = 0
test_setp = 0
lossf = nn.CrossEntropyLoss()
model = Model()
optim = torch.optim.SGD(model.parameters(),lr=0.01)
for i in range(epoch):
print('the {} epoch:'.format(i+1))
total_accuracy = 0
total_loss = 0
for trains in trains_load:
imgs,labels = trains
result = model(imgs)
loss = lossf(result,labels)
total_loss += loss
accury = (result.argmax(1) == labels)
total_accuracy += list(accury).count(True)
optim.zero_grad()
loss.backward()
writer.add_scalar('train',loss.item(),train_step)
optim.step()
if train_step % 100 == 0:
print('step{},loss:{}'.format(train_step,total_loss))
train_step += 1
print('the accurate:{}'.format(total_accuracy/len(train_datasets)))
if i % 2 == 0:
torch.save(model.state_dict(),'model.pth{}'.format(i+1))
print('模型保存成功')
total_test_loss = 0
total_test_acc = 0
test_step = 0
with torch.no_grad():
for test in test_load:
imgs,labels = test
result = model(imgs)
loss = lossf(result,imgs)
total_test_loss += loss
accury = (result.argmax(1)==labels)
total_test_acc += list(accury).count(True)
writer.add_scalar('test_loss',loss.item(),test_step)
writer.add_scalar('test_acc',total_test_acc,test_step)
print('accurate:'.format(total_test_acc/len(test_load)))