-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_classifier_early_stopping.py
105 lines (92 loc) · 3.6 KB
/
mnist_classifier_early_stopping.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader, random_split
from torch.optim.lr_scheduler import StepLR
from sklearn.metrics import confusion_matrix, accuracy_score
import numpy as np
# Check if GPU is available and set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Step 1: Load and normalize the dataset with data augmentation
transform = transforms.Compose([transforms.RandomRotation(10),
transforms.RandomHorizontalFlip(),
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)
train_size = int(0.8 * len(trainset))
val_size = len(trainset) - train_size
train_dataset, val_dataset = random_split(trainset, [train_size, val_size])
trainloader = DataLoader(train_dataset, batch_size=64, shuffle=True)
valloader = DataLoader(val_dataset, batch_size=64, shuffle=False)
# Step 2: Define the neural network structure
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.linear1 = nn.Linear(784, 256)
self.dropout1 = nn.Dropout(0.5)
self.linear2 = nn.Linear(256, 128)
self.dropout2 = nn.Dropout(0.5)
self.linear3 = nn.Linear(128, 64)
self.dropout3 = nn.Dropout(0.5)
self.linear4 = nn.Linear(64, 10)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = torch.relu(self.linear1(x))
x = self.dropout1(x)
x = torch.relu(self.linear2(x))
x = self.dropout2(x)
x = torch.relu(self.linear3(x))
x = self.dropout3(x)
x = self.linear4(x)
return x
model = NeuralNetwork().to(device)
# Step 3: Define a loss function, optimizer, and scheduler
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
scheduler = StepLR(optimizer, step_size=1, gamma=0.7)
# Step 4: Train the network with early stopping
best_val_loss = float("inf")
patience = 3
trigger_times = 0
for epoch in range(10):
model.train()
running_loss = 0.0
for images, labels in trainloader:
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
scheduler.step()
model.eval()
val_loss = 0.0
all_preds = []
all_labels = []
with torch.no_grad():
for images, labels in valloader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
val_loss += loss.item()
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Calculate and print metrics
cm = confusion_matrix(all_labels, all_preds)
acc = accuracy_score(all_labels, all_preds)
print(f'Epoch {epoch+1}, Training Loss: {running_loss/len(trainloader)}, Validation Loss: {val_loss/len(valloader)}, Accuracy: {acc}')
print(f'Confusion Matrix:\n{cm}')
# Early stopping
if val_loss < best_val_loss:
best_val_loss = val_loss
trigger_times = 0
else:
trigger_times += 1
if trigger_times >= patience:
print("Early stopping!")
break
print('Finished Training')