forked from yjwu17/STBP-for-training-SpikingNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
82 lines (75 loc) · 3.17 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
from __future__ import print_function
import torchvision
import torchvision.transforms as transforms
import os
import time
from model_snn import*
# os.environ['CUDA_VISIBLE_DEVICES'] = "3"
names = 'model_snn'
data_path = './raw/' #todo: input your data path
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_dataset = torchvision.datasets.MNIST(root= data_path, train=True, download=True, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
test_set = torchvision.datasets.MNIST(root= data_path, train=False, download=True, transform=transforms.ToTensor())
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=0)
best_acc = 0 # best test accuracy
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
acc_record = list([])
loss_train_record = list([])
loss_test_record = list([])
snn = SCNN()
snn.to(device)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(snn.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
running_loss = 0
start_time = time.time()
for i, (images, labels) in enumerate(train_loader):
snn.zero_grad()
optimizer.zero_grad()
images = images.float().to(device)
outputs = snn(images)
labels_ = torch.zeros(batch_size, 10).scatter_(1, labels.view(-1, 1), 1)
loss = criterion(outputs.cpu(), labels_)
running_loss += loss.item()
loss.backward()
optimizer.step()
if (i+1)%10 == 0:
print ('Epoch [%d/%d], Step [%d/%d], Loss: %.5f'
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size,running_loss ))
running_loss = 0
print('Time elasped:', time.time()-start_time)
correct = 0
total = 0
optimizer = lr_scheduler(optimizer, epoch, learning_rate, 40)
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(test_loader):
inputs = inputs.to(device)
optimizer.zero_grad()
outputs = snn(inputs)
labels_ = torch.zeros(batch_size, 10).scatter_(1, targets.view(-1, 1), 1)
loss = criterion(outputs.cpu(), labels_)
_, predicted = outputs.cpu().max(1)
total += float(targets.size(0))
correct += float(predicted.eq(targets).sum().item())
if batch_idx %100 ==0:
acc = 100. * float(correct) / float(total)
print(batch_idx, len(test_loader),' Acc: %.5f' % acc)
print('Iters:', epoch,'\n\n\n')
print('Test Accuracy of the model on the 10000 test images: %.3f' % (100 * correct / total))
acc = 100. * float(correct) / float(total)
acc_record.append(acc)
if epoch % 5 == 0:
print(acc)
print('Saving..')
state = {
'net': snn.state_dict(),
'acc': acc,
'epoch': epoch,
'acc_record': acc_record,
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/ckpt' + names + '.t7')
torch.save(snn, './checkpoint/model_' + names + '.t7')
best_acc = acc