Created
October 11, 2024 00:39
-
-
Save vhsu/456f9eef7fa496d4fed744d408d80bc2 to your computer and use it in GitHub Desktop.
Mnist example with Pytorch fully generated with chatgpt 4o-preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import torch.optim as optim | |
import torch.nn.functional as F | |
from torchvision import datasets, transforms | |
from torch.utils.data import DataLoader | |
# Step 1: Load and Preprocess the MNIST Dataset | |
# Define transformations for the training and testing data | |
transform = transforms.Compose([ | |
transforms.ToTensor(), # Convert images to PyTorch tensors | |
transforms.Normalize((0.1307,), (0.3081,)) # Normalize with mean and std of MNIST | |
]) | |
# Load the training and testing datasets | |
train_dataset = datasets.MNIST(root='data', train=True, download=True, transform=transform) | |
test_dataset = datasets.MNIST(root='data', train=False, download=True, transform=transform) | |
# Define data loaders for batching | |
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True) | |
test_loader = DataLoader(dataset=test_dataset, batch_size=1000, shuffle=False) | |
# Step 2: Define the Neural Network Model | |
class Net(nn.Module): | |
def __init__(self): | |
super(Net, self).__init__() | |
# Define layers | |
self.fc1 = nn.Linear(28 * 28, 512) # Input layer | |
self.fc2 = nn.Linear(512, 256) # Hidden layer | |
self.fc3 = nn.Linear(256, 10) # Output layer | |
def forward(self, x): | |
x = x.view(-1, 28 * 28) # Flatten the input tensor | |
x = F.relu(self.fc1(x)) # Apply ReLU activation | |
x = F.relu(self.fc2(x)) | |
x = self.fc3(x) # Output layer (no activation here) | |
return x | |
# Instantiate the network | |
model = Net() | |
# Step 3: Define the Loss Function and Optimizer | |
criterion = nn.CrossEntropyLoss() # Combines LogSoftmax and NLLLoss | |
optimizer = optim.Adam(model.parameters(), lr=0.001) | |
# Step 4: Train the Model | |
num_epochs = 5 # Number of epochs for training | |
for epoch in range(num_epochs): | |
model.train() # Set the model to training mode | |
total_loss = 0 | |
for batch_idx, (data, target) in enumerate(train_loader): | |
# Zero the parameter gradients | |
optimizer.zero_grad() | |
# Forward pass | |
output = model(data) | |
loss = criterion(output, target) | |
# Backward pass and optimize | |
loss.backward() | |
optimizer.step() | |
total_loss += loss.item() | |
# Print loss every 100 batches | |
if batch_idx % 100 == 0: | |
print(f'Epoch [{epoch+1}/{num_epochs}], Batch [{batch_idx}], Loss: {loss.item():.4f}') | |
avg_loss = total_loss / len(train_loader) | |
print(f'Epoch [{epoch+1}/{num_epochs}] Average Loss: {avg_loss:.4f}') | |
# Step 5: Evaluate the Model | |
model.eval() # Set the model to evaluation mode | |
correct = 0 | |
total = 0 | |
with torch.no_grad(): # Disable gradient computation | |
for data, target in test_loader: | |
outputs = model(data) | |
_, predicted = torch.max(outputs.data, 1) | |
total += target.size(0) | |
correct += (predicted == target).sum().item() | |
accuracy = 100 * correct / total | |
print(f'Accuracy of the model on the 10,000 test images: {accuracy:.2f}%') | |
# Optional: Save the Model | |
# Uncomment the lines below if you wish to save the trained model | |
# torch.save(model.state_dict(), 'mnist_model.pth') | |
# print('Model saved to mnist_model.pth') | |
torch.save(model.state_dict(), 'mnist_model.pth') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment