From 6bfc29f4b89514b8eaa9ceaad0381ad9086a73dd Mon Sep 17 00:00:00 2001 From: "sweep-nightly[bot]" <131841235+sweep-nightly[bot]@users.noreply.github.com> Date: Tue, 24 Oct 2023 04:13:43 +0000 Subject: [PATCH] Sandbox run src/main.py --- src/main.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main.py b/src/main.py index 5211dcd..7b4e310 100644 --- a/src/main.py +++ b/src/main.py @@ -1,23 +1,25 @@ -from PIL import Image +import logging + +import numpy as np import torch import torch.nn as nn import torch.optim as optim -from torchvision import datasets, transforms from torch.utils.data import DataLoader -import numpy as np -import logging +from torchvision import datasets, transforms # Step 1: Load MNIST Data and Preprocess -transform = transforms.Compose([ - transforms.ToTensor(), - transforms.Normalize((0.5,), (0.5,)) -]) +transform = transforms.Compose( + [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))] +) -logging.basicConfig(filename='training.log', level=logging.INFO, format='%(asctime)s %(message)s') +logging.basicConfig( + filename="training.log", level=logging.INFO, format="%(asctime)s %(message)s" +) -trainset = datasets.MNIST('.', download=True, train=True, transform=transform) +trainset = datasets.MNIST(".", download=True, train=True, transform=transform) trainloader = DataLoader(trainset, batch_size=64, shuffle=True) + # Step 2: Define the PyTorch Model class Net(nn.Module): def __init__(self): @@ -25,7 +27,7 @@ def __init__(self): self.fc1 = nn.Linear(28 * 28, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64, 10) - + def forward(self, x): x = x.view(-1, 28 * 28) x = nn.functional.relu(self.fc1(x)) @@ -33,6 +35,7 @@ def forward(self, x): x = self.fc3(x) return nn.functional.log_softmax(x, dim=1) + # Step 3: Train the Model model = Net() optimizer = optim.SGD(model.parameters(), lr=0.01) @@ -47,6 +50,6 @@ def forward(self, x): loss = criterion(output, labels) loss.backward() optimizer.step() - logging.info('Epoch: %s, Loss: %s', epoch, loss.item()) + logging.info("Epoch: %s, Loss: %s", epoch, loss.item()) -torch.save(model.state_dict(), "mnist_model.pth") \ No newline at end of file +torch.save(model.state_dict(), "mnist_model.pth")