-
Notifications
You must be signed in to change notification settings - Fork 164
/
shared_utilities.py
85 lines (63 loc) · 2.11 KB
/
shared_utilities.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
84
85
import torch
from torch.utils.data import DataLoader
from torch.utils.data.dataset import random_split
from torchvision import datasets, transforms
class PyTorchMLP(torch.nn.Module):
def __init__(self, num_features, num_classes):
super().__init__()
self.all_layers = torch.nn.Sequential(
# 1st hidden layer
torch.nn.Linear(num_features, 50),
torch.nn.ReLU(),
# 2nd hidden layer
torch.nn.Linear(50, 25),
torch.nn.ReLU(),
# output layer
torch.nn.Linear(25, num_classes),
)
def forward(self, x):
x = torch.flatten(x, start_dim=1)
logits = self.all_layers(x)
return logits
def get_dataset_loaders():
train_dataset = datasets.MNIST(
root="./mnist", train=True, transform=transforms.ToTensor(), download=True
)
test_dataset = datasets.MNIST(
root="./mnist", train=False, transform=transforms.ToTensor()
)
train_dataset, val_dataset = random_split(train_dataset, lengths=[55000, 5000], generator=torch.Generator().manual_seed(42))
train_loader = DataLoader(
dataset=train_dataset,
num_workers=0,
batch_size=64,
shuffle=True,
)
val_loader = DataLoader(
dataset=val_dataset,
num_workers=0,
batch_size=64,
shuffle=False,
)
test_loader = DataLoader(
dataset=test_dataset,
num_workers=0,
batch_size=64,
shuffle=False,
)
return train_loader, val_loader, test_loader
def compute_accuracy(model, dataloader, device=None):
if device is None:
device = torch.device("cpu")
model = model.eval()
correct = 0.0
total_examples = 0
for idx, (features, labels) in enumerate(dataloader):
features, labels = features.to(device), labels.to(device)
with torch.no_grad():
logits = model(features)
predictions = torch.argmax(logits, dim=1)
compare = labels == predictions
correct += torch.sum(compare)
total_examples += len(compare)
return correct / total_examples