-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch_mnist.py
54 lines (44 loc) · 1.29 KB
/
pytorch_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
53
54
import torch
from torchvision import datasets, transforms
from torch import nn, optim
# Define a transform to normalize the data
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)
# Download and load the training data
trainset = datasets.MNIST(
"~/.pytorch/MNIST_data/", download=True, train=True, transform=transform
)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
# Define the model architecture
model = nn.Sequential(
nn.Flatten(),
nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 128),
nn.ReLU(),
nn.Linear(128, 10),
nn.LogSoftmax(dim=1),
)
# Define the loss function
criterion = nn.NLLLoss()
# Define the optimizer
optimizer = optim.Adam(model.parameters(), lr=0.003)
# Train the model
epochs = 3
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Clear the gradients
optimizer.zero_grad()
# Forward pass
output = model(images)
loss = criterion(output, labels)
# Backward pass
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
print(f"Training loss: {running_loss/len(trainloader)}")
# Save the model
torch.save(model.state_dict(), "handwritten.pth")