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
class MyModel(nn.Module): | |
def __init__(self): | |
super(MyModel, self).__init__() | |
# 28x28x1 => 26x26x32 | |
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3) | |
self.d1 = nn.Linear(26 * 26 * 32, 128) | |
self.d2 = nn.Linear(128, 10) | |
def forward(self, x): |
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
## test the model with 1 batch | |
model = MyModel() | |
for images, labels in trainloader: | |
print("batch size:", images.shape) | |
out = model(images) | |
print(out.shape) | |
break |
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
learning_rate = 0.001 | |
num_epochs = 5 | |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | |
model = MyModel() | |
model = model.to(device) | |
criterion = nn.CrossEntropyLoss() | |
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) | |
## compute accuracy |
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
for epoch in range(num_epochs): | |
train_running_loss = 0.0 | |
train_acc = 0.0 | |
model = model.train() | |
## training step | |
for i, (images, labels) in enumerate(trainloader): | |
images = images.to(device) |
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
test_acc = 0.0 | |
for i, (images, labels) in enumerate(testloader, 0): | |
images = images.to(device) | |
labels = labels.to(device) | |
outputs = model(images) | |
test_acc += get_accuracy(outputs, labels, BATCH_SIZE) | |
print('Test Accuracy: %.2f'%( test_acc/i)) |
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
## The usual imports | |
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import torchvision | |
import torchvision.transforms as transforms | |
## for printing image | |
import matplotlib.pyplot as plt | |
import numpy as np |
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
## parameter denoting the batch size | |
BATCH_SIZE = 32 | |
## transformations | |
transform = transforms.Compose( | |
[transforms.ToTensor()]) | |
## download and load training dataset | |
trainset = torchvision.datasets.MNIST(root='./data', train=True, | |
download=True, transform=transform) |
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
image = transforms.ToPILImage(mode='L')(torch.randn(1, 96, 96)) |
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
plt.imshow(image) |
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
## dummy transformation | |
dummy_transform = transforms.Compose( | |
[transforms.RandomRotation(45)]) | |
dummy_result = dummy_transform(image) | |
plt.imshow(dummy_result) |