-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrain.py
More file actions
51 lines (33 loc) · 1.04 KB
/
train.py
File metadata and controls
51 lines (33 loc) · 1.04 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
import torch
from model import CNN, ImgDataset
from torch.autograd import Variable
from torch import nn, optim
use_cuda = torch.cuda.is_available()
# hyperparameters
batch_size = 64
lr = 0.001
epochs = 5
data = ImgDataset(batch_size=batch_size)
dataloader = data.dataloader
net = CNN()
net.train()
if use_cuda:
print('CUDA device found, now active')
net.cuda()
crit = nn.CrossEntropyLoss(size_average=False)
optimizer = optim.SGD(net.parameters(), lr, weight_decay=0.0001)
# training loop
for i in range(epochs):
for j, (data, label) in enumerate(dataloader):
label = Variable(label, requires_grad=False)
data = Variable(data, requires_grad=False)
if use_cuda:
label = label.cuda()
data = data.cuda()
optimizer.zero_grad()
pred = net(data)
loss = crit(pred, label)
loss.backward()
optimizer.step()
print('epoch [{}/{}] batch [{}] loss {:.5f}'.format(i, epochs, j, loss.data[0]))
torch.save(net, 'model.pt')