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
## 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
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
for images, labels in trainloader: | |
print("Image batch dimensions:", images.shape) | |
print("Image label dimensions:", labels.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
import matplotlib.pyplot as plt | |
import numpy as np | |
## functions to show an image | |
def imshow(img): | |
#img = img / 2 + 0.5 # unnormalize | |
npimg = img.numpy() | |
plt.imshow(np.transpose(npimg, (1, 2, 0))) | |
## get some random training images |
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
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) | |
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, |
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.nn.functional as F | |
import torchvision | |
import torchvision.transforms as transforms |
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
!pip3 install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html |
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 tqdm(range(1, num_epochs+1)): | |
start_time = time.time() | |
scheduler.step() | |
lr = scheduler.get_lr()[0] | |
model.train() | |
train_loss_total = 0.0 | |
num_steps = 0 |
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
def numeric_score(prediction, groundtruth): | |
FP = np.float(np.sum((prediction == 1) & (groundtruth == 0))) | |
FN = np.float(np.sum((prediction == 0) & (groundtruth == 1))) | |
TP = np.float(np.sum((prediction == 1) & (groundtruth == 1))) | |
TN = np.float(np.sum((prediction == 0) & (groundtruth == 0))) | |
return FP, FN, TP, TN | |
def accuracy(prediction, groundtruth): | |
FP, FN, TP, TN = numeric_score(prediction, groundtruth) | |
N = FP + FN + TP + TN |