Last active
September 10, 2024 16:23
-
-
Save devanshuDesai/9f06681d8939afd04f8fab5ac5f5dbf8 to your computer and use it in GitHub Desktop.
A neural network written in PyTorch with > 99% accuracy on the MNIST dataset.
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 | |
from torch import nn | |
from tqdm import tqdm_notebook | |
class CNN(nn.Module): | |
def __init__(self, input_size, num_classes): | |
""" | |
init convolution and activation layers | |
Args: | |
input_size: (1,28,28) | |
num_classes: 10 | |
""" | |
super(CNN, self).__init__() | |
self.layer1 = nn.Sequential( | |
nn.Conv2d(input_size[0], 32, kernel_size=5), | |
nn.ReLU(), | |
nn.MaxPool2d(kernel_size=2)) | |
self.layer2 = nn.Sequential( | |
nn.Conv2d(32, 64, kernel_size=5), | |
nn.ReLU(), | |
nn.MaxPool2d(kernel_size=2)) | |
self.fc1 = nn.Linear(4 * 4 * 64, num_classes) | |
def forward(self, x): | |
""" | |
forward function describes how input tensor is transformed to output tensor | |
Args: | |
x: (Nx1x28x28) tensor | |
""" | |
x = self.layer1(x) | |
x = self.layer2(x) | |
x = x.reshape(x.size(0), -1) | |
x = self.fc1(x) | |
return x | |
model = CNN((1, 28, 28), 10) | |
opts = { | |
'lr': 1e-3, | |
'epochs': 20, | |
'batch_size': 64 | |
} | |
optimizer = torch.optim.Adam(model.parameters(), opts['lr']) | |
criterion = torch.nn.CrossEntropyLoss() # loss function | |
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=opts['batch_size'], shuffle=True) | |
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=opts['batch_size'], shuffle=True) | |
for epoch in range(opts['epochs']): | |
train_loss = [] | |
for i, (data, labels) in tqdm_notebook(enumerate(train_loader), total=len(train_loader)): | |
# pass data through network | |
outputs = model(data) | |
loss = criterion(outputs, labels) | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
train_loss.append(loss.item()) | |
test_loss = [] | |
test_accuracy = [] | |
for i, (data, labels) in enumerate(test_loader): | |
# pass data through network | |
outputs = model(data) | |
_, predicted = torch.max(outputs.data, 1) | |
loss = criterion(outputs, labels) | |
test_loss.append(loss.item()) | |
test_accuracy.append((predicted == labels).sum().item() / predicted.size(0)) | |
print('epoch: {}, train loss: {}, test loss: {}, test accuracy: {}'.format(epoch, np.mean(train_loss), np.mean(test_loss), np.mean(test_accuracy))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment