forked from patrickloeber/pytorchTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrambled.py
81 lines (71 loc) · 2.29 KB
/
scrambled.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
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor, Normalize, Compose
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class NN(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.lrs = nn.Sequential(
nn.Linear(28*28, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(512, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.lrs(x)
return logits
def train(dataloader, m, f, o, s):
size = len(dataloader.dataset)
m.train()
for batch, (x, y) in enumerate(dataloader):
x, y = x.to(device), y.to(device)
p = m(x)
loss = f(p, y)
loss.backward()
o.step()
o.zero_grad()
if batch % 100 == 0:
loss, current = loss.item(), (batch + 1) * len(x)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
s.step()
def test(dataloader, m, f):
size = len(dataloader.dataset)
num_batches = len(dataloader)
m.eval()
correct = 0
test_loss = 0
with torch.no_grad():
for x, y in dataloader:
x, y = x.to(device), y.to(device)
p = m(x)
test_loss += f(p, y).item()
correct += (p.argmax(1) == y).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
epochs = 15
lr = 0.001
m = NN().to(device)
f = nn.CrossEntropyLoss()
o = torch.optim.Adam(m.parameters(), lr=lr)
s = torch.optim.lr_scheduler.StepLR(o, step_size=5, gamma=0.1)
transform = Compose([
ToTensor(),
Normalize((0.1307,), (0.3081,))
])
dl1 = DataLoader(datasets.MNIST('data', train=True, download=True, transform=transform), batch_size=64, shuffle=True)
dl2 = DataLoader(datasets.MNIST('data', train=False, transform=transform), batch_size=64, shuffle=True)
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train(dl1, m, f, o, s)
test(dl2, m, f)
print("Done!")