Torch - cnn

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import numpy as np
import time

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torch.autograd import Variable

import helper
from torchvision import datasets, transforms

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                              transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
                             ])
# Download and load the training data
trainset = datasets.MNIST('MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Download and load the test data
testset = datasets.MNIST('MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)

Epoch: 1/5.. Loss: 1.4250.. Test accuracy: 0.8135.. 0.0588 s/batch

Epoch: 1/5.. Loss: 0.4746.. Test accuracy: 0.9095.. 0.0116 s/batch

Epoch: 1/5.. Loss: 0.3003.. Test accuracy: 0.9307.. 0.0115 s/batch

...

Epoch: 5/5.. Loss: 0.0278.. Test accuracy: 0.9899.. 0.0113 s/batch

Epoch: 5/5.. Loss: 0.0205.. Test accuracy: 0.9897.. 0.0114 s/batch

Saving and loading models

Test save & load

Last updated

Was this helpful?