{"cells":[{"cell_type":"markdown","source":["To run the Jupyter notebook on Google Colab:\n","1) Open Google Colab:\n","https://colab.research.google.com\n","2) In the pop-up window, click the \"Upload\" tab\n","3) Click \"Choose file\" → select your .ipynb file\n","4) Colab will open your notebook immediately.\n","5) In Colab in the top-right corner click \"Connect\"\n","6) In Colab in the bottom-right corner click \"T4(Python 3)\", select \"Change runtime type\" and make sure you use GPU\n","\n","No need to install any python packages, everything is preinstalled in Colab"],"metadata":{"id":"iA4VpINKcFwo"}},{"cell_type":"markdown","source":["To run the Jupyter notebook locally on your laptop you need to install all the packages (pytorch, matplotlib):\n","1) pip install --upgrade pip\n","2) pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu\n","3) pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126\n","4) pip install matplotlib\n","5) pip install numpy\n","6) pip install scikit-learn\n","\n"],"metadata":{"id":"R21IKO9QcOKz"}},{"cell_type":"markdown","metadata":{"id":"G9izkKnAuKp7"},"source":["# Import all the necessary libraries"]},{"cell_type":"code","execution_count":47,"metadata":{"id":"tVyBJYPZuKp8","executionInfo":{"status":"ok","timestamp":1763145452049,"user_tz":480,"elapsed":23,"user":{"displayName":"Rasul Kairgeldin","userId":"08252840589446120769"}}},"outputs":[],"source":["import torch\n","import torch.nn as nn\n","import torch.optim as optim\n","from torchvision import datasets, transforms\n","from torch.utils.data import DataLoader\n","import matplotlib.pyplot as plt\n","import seaborn as sns\n","from sklearn.metrics import confusion_matrix\n","import numpy as np\n","from matplotlib.colors import LogNorm\n"]},{"cell_type":"markdown","metadata":{"id":"xNcwYQvBuKp9"},"source":["# Load MNIST dataset"]},{"cell_type":"markdown","metadata":{"id":"nRurFljAuKp9"},"source":["##### Compose multiple dataset preprocessing steps and load the dataset:\n","transforms.ToTensor() - converts to pytorch tensor, brings scale from 0-255 to 0-1\n","\n","transforms.Normalize((0.1307,), (0.3081,)) - normalizes by mean and variance"]},{"cell_type":"code","execution_count":3,"metadata":{"id":"zMQx0F49uKp9","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1763142966265,"user_tz":480,"elapsed":3255,"user":{"displayName":"Rasul Kairgeldin","userId":"08252840589446120769"}},"outputId":"e9c538c4-64f0-46ab-8d22-1f794b1289d5"},"outputs":[{"output_type":"stream","name":"stderr","text":["100%|██████████| 9.91M/9.91M [00:00<00:00, 19.8MB/s]\n","100%|██████████| 28.9k/28.9k [00:00<00:00, 236kB/s]\n","100%|██████████| 1.65M/1.65M [00:00<00:00, 4.50MB/s]\n","100%|██████████| 4.54k/4.54k [00:00<00:00, 10.5MB/s]\n"]}],"source":["transform = transforms.Compose([\n"," transforms.ToTensor(),\n"," transforms.Normalize((0.1307,), (0.3081,))\n","])\n","\n","train_data = datasets.MNIST(root=\"./data\", train=True, download=True, transform=transform)\n","validation_data = datasets.MNIST(root=\"./data\", train=False, download=True, transform=transform)"]},{"cell_type":"markdown","metadata":{"id":"USGQUXUauKp8"},"source":["# Define LeNet-5 model architecture\n","##### Model takes as an input images of 28x28 pixels grayscale (single channel)\n","