-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorch_mnist.py
52 lines (46 loc) · 1.71 KB
/
torch_mnist.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
import torch.nn as nn
import torch
from torch.optim import adam
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import DataLoader
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.l1 = nn.Linear(784, 128)
self.activation = nn.ReLU()
self.l2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(x.size(0), -1)
x = self.l1(x)
x = self.activation(x)
x = self.l2(x)
return x
train_data = datasets.MNIST(root="./datasets", train=True, transform=transforms.ToTensor())
test_data = datasets.MNIST(root="./datasets", train=False, transform=transforms.ToTensor())
train_dataloader = DataLoader(train_data, batch_size=16, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=16, shuffle=True)
epochs = 1000
m = MLP()
optim = adam.Adam(m.parameters(), lr=3e-4)
loss = None
for i in range(epochs):
data, labels = next(iter(train_dataloader))
pred = m(data)
loss = nn.CrossEntropyLoss(reduction="mean")(pred, labels)
optim.zero_grad()
loss.backward()
optim.step()
if i % 100 == 0:
# Accuracy calculation
with torch.no_grad(): # Disable gradient calculation for evaluation
predicted = torch.argmax(pred, dim=1) # Get class with highest score
correct = (predicted == labels).sum().item() # Count correct predictions
accuracy = correct / labels.size(0) # Compute accuracy as a fraction of total samples
print(f"loss: {loss}, accuracy: {accuracy}")
torch.save({
'epoch': epochs,
'model_state_dict': m.state_dict(),
'optimizer_state_dict': optim.state_dict(),
'loss': loss
}, "./pretrained/mnist.pt")