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
| import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets,transforms from torch.utils.data import DataLoader import matplotlib.pyplot as plt
torch.manual_seed(1024) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5,),(0.5,)) ]) train_dataset = datasets.MNIST(root='./data',train=True,transform=transforms.ToTensor(),download=False)
test_dataset = datasets.MNIST(root='./data',train=False,transform=transforms.ToTensor(),download=False) train_loader = DataLoader(train_dataset,batch_size=64,shuffle=True) test_loader = DataLoader(test_dataset,batch_size=64,shuffle=False) class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(28 * 28, 256) self.fc2 = nn.Linear(256, 128) self.fc3 = nn.Linear(128, 64) self.fc4 = nn.Linear(64, 10)
def forward(self,x): x = torch.flatten(x,start_dim=1) x = torch.relu(self.fc1(x)) x = torch.relu(self.fc2(x)) x = torch.relu(self.fc3(x)) x = self.fc4(x) return x net = Net() print(net) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=0.001) train_losses = [] train_accuracies = [] test_accuracies = [] epochs = 10 best_accuracy = 0.0 best_model_path = 'best_mnist_path.pth'
for epoch in range(epochs): running_loss = 0.0 correct_train = 0 total_train = 0
net.train() for inputs,labels in train_loader: optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs,labels) loss.backward() optimizer.step() running_loss += loss.item()
_,predicted = torch.max(outputs,1) total_train += labels.size(0) correct_train += (predicted == labels).sum().item() train_accuracy = correct_train / total_train train_losses.append(running_loss / len(train_loader)) train_accuracies.append((train_accuracy))
print(f"Epoch {epoch + 1} / {epochs}, Loss: {running_loss / len(train_loader):.4f},Train Accuracy :{train_accuracy:.2%}") net.eval() correct = 0 total = 0 with torch.no_grad(): for inputs,labels in test_loader: outputs = net(inputs) _,predicted = torch.max(outputs,1) total += labels.size(0) correct += (predicted == labels).sum().item()
test_accuracy = correct / total test_accuracies.append(test_accuracy) print(f"Epoch {epoch+1}/{epochs},Test Accuracy:{test_accuracy:.2%}") if test_accuracy > best_accuracy: best_accuracy = test_accuracy torch.save(net.state_dict(),best_model_path) print(f"Best model saved with accuracy:{best_accuracy:.2%}") print(f"Best Accuracy on test set:{best_accuracy:.2%}") plt.figure(figsize=(12,5)) plt.subplot(1,2,1) plt.plot(train_losses,label='Training Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.title('Training Loss over Epochs') plt.legend() plt.grid(True) plt.subplot(1,2,2) plt.plot(train_accuracies,label='Train Accuracy') plt.plot(test_accuracies,label='Test Accuracy') plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.title('Train and Test Accuracy over Epochs') plt.legend() plt.grid(True) plt.tight_layout() plt.savefig('loss_and_accuracy_curves.png') plt.show()
|