-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
84 lines (65 loc) · 2.59 KB
/
main.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
83
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from network import CNN, DropMaxCNN
from DropMax import DropMax
if __name__ == "__main__":
n_classes = 10
device = torch.device("cuda")
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
trains = torch.utils.data.DataLoader(
datasets.MNIST('./data', train=True, download=True, transform=transform),
batch_size=32, shuffle=True, num_workers=4
)
tests = torch.utils.data.DataLoader(
datasets.MNIST('./data', train=False, transform=transform),
batch_size=32, shuffle=False, num_workers=4
)
print("Train : ", len(trains.dataset))
print("Test : ", len(tests.dataset))
dropmax_cnn = DropMaxCNN(n_classes=n_classes).to(device)
dropmax_loss = DropMax(device)
adam = optim.Adam(dropmax_cnn.parameters(), lr=0.0005, betas=(0.5, 0.999), weight_decay=1e-4)
cnn = CNN(n_classes=n_classes).to(device)
ce_loss = nn.CrossEntropyLoss()
adam2 = optim.Adam(cnn.parameters(), lr=0.0005, betas=(0.5, 0.999), weight_decay=1e-4)
for epoch in range(30):
dropmax_cnn.train()
print("Epoch : ", epoch)
for i, (img, target) in enumerate(trains):
img = img.to(device)
one_hot = torch.zeros(img.shape[0], n_classes)
one_hot.scatter_(1, target.unsqueeze(dim=1), 1)
one_hot = one_hot.to(device)
o, p, r, q = dropmax_cnn(img)
loss = dropmax_loss(o, p, r, q, one_hot)
adam.zero_grad()
loss.backward()
adam.step()
o2 = cnn(img)
loss2 = ce_loss(o2, target.to(device))
adam2.zero_grad()
loss2.backward()
adam2.step()
if i % 30000 == 0:
print("DM loss : ", loss.item())
print("CE loss : ", loss2.item())
break
correct, correct2 = 0, 0
for i, (img, target) in enumerate(tests):
img = img.to(device)
one_hot = torch.zeros(img.shape[0], n_classes)
one_hot.scatter_(1, target.unsqueeze(dim=1), 1)
one_hot = one_hot.to(device)
o, p, r, q = dropmax_cnn(img)
correct += dropmax_loss.get_acc(p, o, target)
o2 = cnn(img)
_, idx = o2.max(dim=1)
correct2 += (idx.cpu() == target).sum().item()
print("DM acc : ", correct / len(tests.dataset))
print("CE acc : ", correct2 / len(tests.dataset))
print("--------")