From 46089e2fe7d6c0af2a90c03e657e87ae3c14951e 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:14:32 +0000 Subject: [PATCH 1/3] feat: Add CNN class and training function in cnn.p --- src/cnn.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/cnn.py diff --git a/src/cnn.py b/src/cnn.py new file mode 100644 index 0000000..da739a0 --- /dev/null +++ b/src/cnn.py @@ -0,0 +1,31 @@ +import torch +import torch.nn as nn +import torch.optim as optim + +class CNN(nn.Module): + def __init__(self): + super(CNN, self).__init__() + self.conv1 = nn.Conv2d(1, 32, kernel_size=5) + self.conv2 = nn.Conv2d(32, 64, kernel_size=5) + self.fc1 = nn.Linear(4*4*64, 1024) + self.fc2 = nn.Linear(1024, 10) + + def forward(self, x): + x = nn.functional.relu(self.conv1(x)) + x = nn.functional.max_pool2d(x, 2) + x = nn.functional.relu(self.conv2(x)) + x = nn.functional.max_pool2d(x, 2) + x = x.view(-1, 4*4*64) + x = nn.functional.relu(self.fc1(x)) + x = self.fc2(x) + return nn.functional.log_softmax(x, dim=1) + +def train_model(model, dataloader, criterion, optimizer, epochs=3): + for epoch in range(epochs): + for images, labels in dataloader: + optimizer.zero_grad() + output = model(images) + loss = criterion(output, labels) + loss.backward() + optimizer.step() + return model From ea205a59557c7c8a820406a76fd17a488a935b83 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:16:01 +0000 Subject: [PATCH 2/3] Sandbox run src/cnn.py --- src/cnn.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cnn.py b/src/cnn.py index da739a0..2494387 100644 --- a/src/cnn.py +++ b/src/cnn.py @@ -1,13 +1,12 @@ -import torch import torch.nn as nn -import torch.optim as optim + class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(1, 32, kernel_size=5) self.conv2 = nn.Conv2d(32, 64, kernel_size=5) - self.fc1 = nn.Linear(4*4*64, 1024) + self.fc1 = nn.Linear(4 * 4 * 64, 1024) self.fc2 = nn.Linear(1024, 10) def forward(self, x): @@ -15,13 +14,14 @@ def forward(self, x): x = nn.functional.max_pool2d(x, 2) x = nn.functional.relu(self.conv2(x)) x = nn.functional.max_pool2d(x, 2) - x = x.view(-1, 4*4*64) + x = x.view(-1, 4 * 4 * 64) x = nn.functional.relu(self.fc1(x)) x = self.fc2(x) return nn.functional.log_softmax(x, dim=1) + def train_model(model, dataloader, criterion, optimizer, epochs=3): - for epoch in range(epochs): + for _epoch in range(epochs): for images, labels in dataloader: optimizer.zero_grad() output = model(images) From 9357842ed4cc1c3d112a3d5dedab063f92c0b1fa 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:17:08 +0000 Subject: [PATCH 3/3] feat: Updated src/main.py --- src/main.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/main.py b/src/main.py index 243a31e..55b663f 100644 --- a/src/main.py +++ b/src/main.py @@ -5,6 +5,7 @@ from torchvision import datasets, transforms from torch.utils.data import DataLoader import numpy as np +from cnn import CNN, train_model # Step 1: Load MNIST Data and Preprocess transform = transforms.Compose([ @@ -31,18 +32,12 @@ def forward(self, x): return nn.functional.log_softmax(x, dim=1) # Step 3: Train the Model -model = Net() +model = CNN() optimizer = optim.SGD(model.parameters(), lr=0.01) -criterion = nn.NLLLoss() +criterion = nn.CrossEntropyLoss() # Training loop epochs = 3 -for epoch in range(epochs): - for images, labels in trainloader: - optimizer.zero_grad() - output = model(images) - loss = criterion(output, labels) - loss.backward() - optimizer.step() +model = train_model(model, trainloader, criterion, optimizer, epochs) torch.save(model.state_dict(), "mnist_model.pth") \ No newline at end of file