-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathpytorch-simple-neural-net.py
73 lines (56 loc) · 2.13 KB
/
pytorch-simple-neural-net.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
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
# MNIST dataset:
train_dataset = datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())
# Data loader:
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)
# Fully connected neural network with one hidden layer:
class NeuralNet(nn.Module):
def __init__(self):
super(NeuralNet, self).__init__()
self.l1 = nn.Linear(784, 500)
self.relu = nn.ReLU()
self.l2 = nn.Linear(500, 10)
def forward(self, x):
out = self.l1(x)
out = self.relu(out)
out = self.l2(out)
# no activation and no softmax at the end
return out
model = NeuralNet()
# Loss and optimizer:
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# Train the model:
n_total_steps = len(train_loader)
num_epochs = 3
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# resized: [100, 784]
images = images.reshape(-1, 28*28)
# Forward pass:
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize:
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')
# Test the model:
with torch.no_grad():
n_correct = 0
n_samples = 0
for images, labels in test_loader:
images = images.reshape(-1, 28*28)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
n_samples += labels.size(0)
n_correct += (predicted == labels).sum().item()
acc = 100.0 * n_correct / n_samples
print(f'Accuracy of the network on the 10000 test images: {acc} %')