. Ann With Mnist 1
[1]:
12
# we will download 60K train images and 10k test images
# First we will need to flatten out 28*28 images to 784[3]:
1234
!python3.11 -m pip install torchvision
stdout
Collecting torchvision
Downloading torchvision-0.26.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (5.5 kB)
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torchvision) (2.4.3)
Collecting torch==2.11.0 (from torchvision)
Downloading torch-2.11.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (29 kB)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torchvision) (10.4.0)
Requirement already satisfied: filelock in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (3.24.3)
Requirement already satisfied: typing-extensions>=4.10.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (4.15.0)
Requirement already satisfied: setuptools<82 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (65.5.0)
Requirement already satisfied: sympy>=1.13.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (1.14.0)
Requirement already satisfied: networkx>=2.5.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (3.6.1)
Requirement already satisfied: jinja2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (3.1.4)
Requirement already satisfied: fsspec>=0.8.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from torch==2.11.0->torchvision) (2026.2.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from sympy>=1.13.3->torch==2.11.0->torchvision) (1.3.0)
Requirement already satisfied: MarkupSafe>=2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from jinja2->torch==2.11.0->torchvision) (3.0.2)
Downloading torchvision-0.26.0-cp311-cp311-macosx_11_0_arm64.whl (1.9 MB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.9/1.9 MB[0m [31m383.4 kB/s[0m eta [36m0:00:00[0ma [36m0:00:01[0m
[?25hDownloading torch-2.11.0-cp311-cp311-macosx_11_0_arm64.whl (80.5 MB)
[2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m80.5/80.5 MB[0m [31m1.6 MB/s[0m eta [36m0:00:00[0m00:01[0m00:02[0m0m
[?25hInstalling collected packages: torch, torchvision
Attempting uninstall: torch
Found existing installation: torch 2.10.0
Uninstalling torch-2.10.0:
Successfully uninstalled torch-2.10.0
Successfully installed torch-2.11.0 torchvision-0.26.0
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m24.3.1[0m[39;49m -> [0m[32;49m26.0.1[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip3 install --upgrade pip[0m
[2]:
1
# lets start with importing pytorch [2]:
123
import torch
import torch.nn as nn # import neural network
import torch.nn.functional as F [3]:
123456
from torchvision import datasets,transforms
import numpy as np
import pandas as pd
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader[6]:
12
# MNIST Image file convert to Tensors
[15]:
1
transform=transforms.ToTensor()[16]:
1
train_data=datasets.MNIST(root='../Data',train=True,download=True,transform=transform)[17]:
1
test_data=datasets.MNIST(root='../Data',train=False,download=True,transform=transform)[18]:
12
type(train_data) # this is torch vision
Out[18]:
torchvision.datasets.mnist.MNIST
[19]:
1
train_data[0] # first tensorOut[19]:
(tensor([[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0118, 0.0706, 0.0706, 0.0706,
0.4941, 0.5333, 0.6863, 0.1020, 0.6510, 1.0000, 0.9686, 0.4980,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.1176, 0.1412, 0.3686, 0.6039, 0.6667, 0.9922, 0.9922, 0.9922,
0.9922, 0.9922, 0.8824, 0.6745, 0.9922, 0.9490, 0.7647, 0.2510,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.1922,
0.9333, 0.9922, 0.9922, 0.9922, 0.9922, 0.9922, 0.9922, 0.9922,
0.9922, 0.9843, 0.3647, 0.3216, 0.3216, 0.2196, 0.1529, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0706,
0.8588, 0.9922, 0.9922, 0.9922, 0.9922, 0.9922, 0.7765, 0.7137,
0.9686, 0.9451, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.3137, 0.6118, 0.4196, 0.9922, 0.9922, 0.8039, 0.0431, 0.0000,
0.1686, 0.6039, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0549, 0.0039, 0.6039, 0.9922, 0.3529, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.5451, 0.9922, 0.7451, 0.0078, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0431, 0.7451, 0.9922, 0.2745, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.1373, 0.9451, 0.8824, 0.6275,
0.4235, 0.0039, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.3176, 0.9412, 0.9922,
0.9922, 0.4667, 0.0980, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.1765, 0.7294,
0.9922, 0.9922, 0.5882, 0.1059, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0627,
0.3647, 0.9882, 0.9922, 0.7333, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.9765, 0.9922, 0.9765, 0.2510, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.1804, 0.5098,
0.7176, 0.9922, 0.9922, 0.8118, 0.0078, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.1529, 0.5804, 0.8980, 0.9922,
0.9922, 0.9922, 0.9804, 0.7137, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0941, 0.4471, 0.8667, 0.9922, 0.9922, 0.9922,
0.9922, 0.7882, 0.3059, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0902, 0.2588, 0.8353, 0.9922, 0.9922, 0.9922, 0.9922, 0.7765,
0.3176, 0.0078, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0706, 0.6706,
0.8588, 0.9922, 0.9922, 0.9922, 0.9922, 0.7647, 0.3137, 0.0353,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.2157, 0.6745, 0.8863, 0.9922,
0.9922, 0.9922, 0.9922, 0.9569, 0.5216, 0.0431, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.5333, 0.9922, 0.9922, 0.9922,
0.8314, 0.5294, 0.5176, 0.0627, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000],
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000, 0.0000]]]),
5)
[20]:
1
type(train_data[0]) # this is a tuple Out[20]:
tuple
[21]:
12
# if we see the length of the this tuple it will be 2
len(train_data[0]) # the first value of tuple indicates actuall arraya and second one is the digitOut[21]:
2
[22]:
1
train_data[0][1] # this array represent the number 5Out[22]:
5
[23]:
1
image,label=train_data[0][24]:
1
image.shape # since this is greyscaled image hence firts is 1 . Out[24]:
torch.Size([1, 28, 28])
[25]:
123
plt.imshow(image.reshape((28,28))) # handwritten 5
# since matplot it has default color mapping hence we see this dark purpole and yellow color
# but this is actually a grey scale imageOut[25]:
<matplotlib.image.AxesImage at 0x13602f750>
[26]:
1
plt.imshow(image.reshape((28,28)),cmap='gist_yarg') # handwritten 5 in greyscale Out[26]:
<matplotlib.image.AxesImage at 0x1357f49d0>
[27]:
123456
# right now out input has 60 K , we will load the training data in images
torch.manual_seed(101)
train_loader=DataLoader(train_data,batch_size=100,shuffle=True) # we will only be loading 100 image at a time as batch
# shuffle is require bacause sometime data is organized or sorted
# all the 0 images , all the 0 images
# this will create problem in network hence we do sorting[28]:
1
test_loader=DataLoader(test_data,batch_size=500,shuffle=False) #shuffle is not required here and we can also increase batch size[29]:
123
# lets see the images in grid first with labels
from torchvision.utils import make_grid
np.set_printoptions(formatter=dict(int=lambda x:f'{x:4}'))[30]:
123
# first batch
for image,label in train_loader:
break[31]:
1
image.shape # 100 images 1 colore channel and 28*28 imagesOut[31]:
torch.Size([100, 1, 28, 28])
[32]:
12345
# print first 12 labels
print('Labels: ',label[:12].numpy())
im=make_grid(image[:12],nrow=12) # default n_row is 8
plt.figure(figsize=(10,4))
plt.imshow(np.transpose(im.numpy(),(1,2,0)))stdout
Labels: [ 7 2 3 5 8 5 3 6 9 9 1 3]
Out[32]:
<matplotlib.image.AxesImage at 0x136124f90>
[47]:
12345678910111213141516
class MultiLayerPerceptron(nn.Module):
def __init__(self,in_size=784,out_size=10,layers=[120,84]): # 28*28 when expanded size will be 784, since there are 10 numbers in
super().__init__()
# since there are 10 numbers in output hence out_size is 8 ,
# we will only create 2 hidden layers for now hence 120,84 is there
self.fc1=nn.Linear(in_size,layers[0]) # fully connected layer using linear function
self.fc2=nn.Linear(layers[0],layers[1])
self.fc3=nn.Linear(layers[1],out_size)
def forward(self,X):
X=F.relu(self.fc1(X))
X=F.relu(self.fc2(X))
X=self.fc3(X)
return F.log_softmax(X,dim=1) # multi class classification
[49]:
123
torch.manual_seed(101)
model=MultiLayerPerceptron()
modelOut[49]:
MultiLayerPerceptron(
(fc1): Linear(in_features=784, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)
[50]:
12
for param in model.parameters():
print(param.numel()) # all the parameters stdout
94080
120
10080
84
840
10
[51]:
12
# if we use CNN we wll have lot less param
[52]:
12
creterion=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),lr=0.001)[53]:
12
# flattening the images
image.view(100,-1).shapeOut[53]:
torch.Size([100, 784])
[54]:
123456789101112131415161718192021222324252627282930313233343536373839404142
import time
start_time=time.time()
# Training
epochs=10
train_losses=[]
test_losses=[]
train_correct=[]
test_correct=[]
for i in range(epochs):
trn_correct=0
tst_correct=0
for b,(x_train,y_train) in enumerate(train_loader):
b+=1
y_pred=model(x_train.view(100,-1))
loss=creterion(y_pred,y_train)
predicted=torch.max(y_pred.data,1)[1] # [0.1,0.0,....0.8]
batch_corr=(predicted==y_train).sum()
trn_correct+=batch_corr
optimizer.zero_grad()
loss.backward()
optimizer.step()
if b%200==0:
accuracy=trn_correct.item()*100/(100*b)
print(f"Epoch {i} batch {b} loss: {loss.item()} accuracy :{accuracy}")
train_losses.append(loss)
train_correct.append(trn_correct)
with torch.no_grad():
for b,(X_test,y_test) in enumerate(test_loader):
y_val=model(X_test.view(500,-1))
predicted=torch.max(y_val.data,1)[1]
tst_correct+=(predicted==y_test).sum()
loss=creterion(y_val,y_test)
test_losses.append(loss)
test_correct.append(tst_correct)
total_time=time.time()-start_time
print(f"Duration :{total_time/60}")stdout
Epoch 0 batch 200 loss: 0.2356237769126892 accuracy :83.245
Epoch 0 batch 400 loss: 0.3533071279525757 accuracy :87.5325
Epoch 0 batch 600 loss: 0.13765765726566315 accuracy :89.58166666666666
Epoch 1 batch 200 loss: 0.2450992614030838 accuracy :94.88
Epoch 1 batch 400 loss: 0.139908567070961 accuracy :95.1075
Epoch 1 batch 600 loss: 0.07150521874427795 accuracy :95.37666666666667
Epoch 2 batch 200 loss: 0.08816862106323242 accuracy :96.565
Epoch 2 batch 400 loss: 0.06208188831806183 accuracy :96.66
Epoch 2 batch 600 loss: 0.07086564600467682 accuracy :96.72166666666666
Epoch 3 batch 200 loss: 0.11753450334072113 accuracy :97.63
Epoch 3 batch 400 loss: 0.055651258677244186 accuracy :97.4825
Epoch 3 batch 600 loss: 0.0344504714012146 accuracy :97.52333333333333
Epoch 4 batch 200 loss: 0.10662715137004852 accuracy :98.165
Epoch 4 batch 400 loss: 0.04711269214749336 accuracy :97.995
Epoch 4 batch 600 loss: 0.054924558848142624 accuracy :98.01666666666667
Epoch 5 batch 200 loss: 0.009507461450994015 accuracy :98.44
Epoch 5 batch 400 loss: 0.03661220148205757 accuracy :98.505
Epoch 5 batch 600 loss: 0.057259634137153625 accuracy :98.395
Epoch 6 batch 200 loss: 0.031195903196930885 accuracy :98.845
Epoch 6 batch 400 loss: 0.06201673671603203 accuracy :98.69
Epoch 6 batch 600 loss: 0.08318546414375305 accuracy :98.67
Epoch 7 batch 200 loss: 0.06914353370666504 accuracy :99.16
Epoch 7 batch 400 loss: 0.07898400723934174 accuracy :98.9475
Epoch 7 batch 600 loss: 0.04138811677694321 accuracy :98.88833333333334
Epoch 8 batch 200 loss: 0.005578312091529369 accuracy :99.26
Epoch 8 batch 400 loss: 0.006579724606126547 accuracy :99.1425
Epoch 8 batch 600 loss: 0.09109019488096237 accuracy :99.105
Epoch 9 batch 200 loss: 0.008466478437185287 accuracy :99.405
Epoch 9 batch 400 loss: 0.011405331082642078 accuracy :99.305
Epoch 9 batch 600 loss: 0.023204762488603592 accuracy :99.295
Duration :0.36086444854736327
[46]:
1
x_train.view(100,-1).shapeOut[46]:
torch.Size([100, 784])
[63]:
123
plt.plot([x.item() for x in train_losses],label="Training Loss")
plt.plot([x.item() for x in test_losses],label="Test Loss")
plt.legend()Out[63]:
<matplotlib.legend.Legend at 0x146bbcb90>
[69]:
123
train_acc=[x/600 for x in train_correct]
test_acc=[x/100 for x in test_correct]
[70]:
123
plt.plot(train_acc,label="Training Accuracy")
plt.plot(test_acc,label="Test Accuracy")
plt.legend()Out[70]:
<matplotlib.legend.Legend at 0x146c13250>
[71]:
1
test_load_all=DataLoader(test_data,batch_size=100000,shuffle=False)[72]:
123456
with torch.no_grad():
correct=0
for X_test,y_test in test_load_all:
y_val=model(X_test.view(len(X_test),-1))
predicted=torch.max(y_val,1)[1]
correct+=(predicted==y_test).sum()[73]:
1
100*correct.item()/len(test_data)Out[73]:
97.82
[74]:
1
confusion_matrix(predicted.view(-1),y_test.view(-1))Out[74]:
array([[ 970, 0, 5, 0, 1, 2, 5, 2, 3, 1],
[ 0, 1125, 3, 0, 0, 0, 3, 7, 0, 3],
[ 1, 3, 1002, 1, 1, 0, 2, 8, 4, 0],
[ 3, 2, 4, 989, 0, 10, 1, 2, 3, 2],
[ 0, 0, 3, 0, 968, 3, 1, 2, 2, 9],
[ 0, 0, 0, 6, 0, 865, 5, 1, 3, 4],
[ 1, 1, 3, 0, 5, 2, 940, 0, 1, 0],
[ 1, 0, 3, 4, 0, 1, 0, 985, 2, 0],
[ 3, 4, 9, 3, 1, 7, 1, 2, 951, 3],
[ 1, 0, 0, 7, 6, 2, 0, 19, 5, 987]])
[ ]:
1
Previous
Start of course
Next
. Image Filters And Kernals