-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
149 lines (125 loc) · 4.68 KB
/
utils.py
File metadata and controls
149 lines (125 loc) · 4.68 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
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
import torch
from torch.autograd import Variable
import time
import torchvision
import torchvision.transforms as transforms
import PIL
def load_data_mnist(batchsize):
# load Cifar10 data set
# define transform of data files
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
)
# load trainset and define trainloader
trainset = torchvision.datasets.MNIST(
root="./data/mnist",
train=True,
download=True,
transform=transform,
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batchsize, shuffle=True, num_workers=2
)
# load testset and define testloader
testset = torchvision.datasets.MNIST(
root="./data/mnist",
train=False,
download=True,
transform=transform,
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batchsize, shuffle=False, num_workers=2
)
classes = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
return trainloader, testloader, classes
def train(net, num_epochs, trainloader, criterion, optimizer, gpu=False, opt_steps=100):
if gpu:
net = net.cuda()
trainloader = trainloader
criterion = criterion
optimizer = optimizer
start_time = time.time()
if num_epochs == None:
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
if i <= opt_steps:
# get the inputs
inputs, labels = data
# wrap them in Variable
if gpu:
inputs, labels = Variable(inputs.cuda()), Variable(
labels.cuda()
)
else:
inputs, labels = Variable(inputs), Variable(labels)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
# net(inputs) calls forward from class Net
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 500 == 499: # print every 2000 mini-batches
print(
"[%d, %5d] loss: %.3f"
% (epoch + 1, i + 1, running_loss / 500)
)
running_loss = 0.0
else:
break
else:
for epoch in range(num_epochs): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs
inputs, labels = data
# wrap them in Variable
if gpu:
inputs, labels = Variable(inputs.cuda()), Variable(
labels.cuda()
)
else:
inputs, labels = Variable(inputs), Variable(labels)
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
# net(inputs) calls forward from class Net
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 500 == 499: # print every 2000 mini-batches
print(
"[%d, %5d] loss: %.3f"
% (epoch + 1, i + 1, running_loss / 500)
)
running_loss = 0.0
end_time = time.time()
time_needed = int(end_time - start_time)
print("Finished Training in %i seconds" % time_needed)
def accuracy(net, iterator, num_steps=None, device=torch.device("cpu")):
total = 0.
correct = 0.
if num_steps is None:
for data in iterator:
inp, labels = data
inp, labels = inp.to(device), labels.to(device)
out = net(Variable(inp))
_, predictions = torch.max(out.data, 1)
total += labels.size(0)
correct += (predictions == labels).sum()
else:
for _ in range(num_steps):
inp, labels = next(iterator.__iter__())
inp, labels = inp.to(device), labels.to(device)
out = net(Variable(inp))
_, predictions = torch.max(out.data, 1)
total += labels.size(0)
correct += (predictions == labels).sum()
print("Accuracy: %f" % (int(correct) / int(total)))
return (int(correct) / int(total))