{ "cells": [ { "cell_type": "markdown", "id": "b9f2b893-473c-4d7d-a32e-7286b12e44f0", "metadata": {}, "source": [ "# Logistic Regression" ] }, { "cell_type": "code", "execution_count": 4, "id": "58af7936-7eb9-43dd-bd15-9e6a818e3e3a", "metadata": {}, "outputs": [], "source": [ "import torch\n", "import torch.nn as nn\n", "import torchvision\n", "import torchvision.transforms as transforms" ] }, { "cell_type": "code", "execution_count": 5, "id": "00be9829-58aa-4203-95a4-52ecb592c40b", "metadata": {}, "outputs": [], "source": [ "input_size = 28 * 28 # 784\n", "num_classes = 10\n", "num_epochs = 5\n", "batch_size = 100\n", "learning_rate = 0.001" ] }, { "cell_type": "code", "execution_count": 6, "id": "417788ea-0246-40d0-b091-df1918d1df30", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cifar10.ipynb \u001b[34mdata\u001b[m\u001b[m ionosphere.ipynb stanfordcars.ipynb\n", "cifar_net.pth fmnist.ipynb mnist.ipynb\n" ] } ], "source": [ "!ls" ] }, { "cell_type": "code", "execution_count": 7, "id": "b573b506-a6f5-41cb-a802-4c067a84a0e0", "metadata": {}, "outputs": [], "source": [ "train_dataset = torchvision.datasets.MNIST(root='./data', \n", " train=True, \n", " transform=transforms.ToTensor(),\n", " download=True)\n", "\n", "test_dataset = torchvision.datasets.MNIST(root='./data', \n", " train=False, \n", " transform=transforms.ToTensor())" ] }, { "cell_type": "code", "execution_count": 5, "id": "dcbf626b-d1c7-4123-aa52-3bb0aa2559cc", "metadata": {}, "outputs": [], "source": [ "train_loader = torch.utils.data.DataLoader(dataset=train_dataset, \n", " batch_size=batch_size, \n", " shuffle=True)\n", "\n", "test_loader = torch.utils.data.DataLoader(dataset=test_dataset, \n", " batch_size=batch_size, \n", " shuffle=False)" ] }, { "cell_type": "code", "execution_count": 8, "id": "0d0d5ae0-afe2-46f8-844b-b4170859c05e", "metadata": {}, "outputs": [], "source": [ "use_mps = torch.backends.mps.is_available()\n", "device = torch.device('mps' if use_mps else 'cpu')\n", "model = nn.Linear(input_size, num_classes).to(device)" ] }, { "cell_type": "code", "execution_count": 9, "id": "ccfd1acf-298c-4979-837c-f6e7bcdd2872", "metadata": {}, "outputs": [], "source": [ "criterion = nn.CrossEntropyLoss() #used with linear output. nll is paired with softmax act at out.\n", "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) " ] }, { "cell_type": "code", "execution_count": 10, "id": "35c39c55-81fb-4fa1-be99-2ea434420ff3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch [1/5], Step [100/600], Loss: 2.2347\n", "Epoch [1/5], Step [200/600], Loss: 2.1419\n", "Epoch [1/5], Step [300/600], Loss: 2.0190\n", "Epoch [1/5], Step [400/600], Loss: 1.9189\n", "Epoch [1/5], Step [500/600], Loss: 1.8591\n", "Epoch [1/5], Step [600/600], Loss: 1.7623\n", "Epoch [2/5], Step [100/600], Loss: 1.6987\n", "Epoch [2/5], Step [200/600], Loss: 1.6702\n", "Epoch [2/5], Step [300/600], Loss: 1.6237\n", "Epoch [2/5], Step [400/600], Loss: 1.5532\n", "Epoch [2/5], Step [500/600], Loss: 1.5135\n", "Epoch [2/5], Step [600/600], Loss: 1.5104\n", "Epoch [3/5], Step [100/600], Loss: 1.3992\n", "Epoch [3/5], Step [200/600], Loss: 1.3885\n", "Epoch [3/5], Step [300/600], Loss: 1.3234\n", "Epoch [3/5], Step [400/600], Loss: 1.2412\n", "Epoch [3/5], Step [500/600], Loss: 1.3231\n", "Epoch [3/5], Step [600/600], Loss: 1.3018\n", "Epoch [4/5], Step [100/600], Loss: 1.2082\n", "Epoch [4/5], Step [200/600], Loss: 1.2837\n", "Epoch [4/5], Step [300/600], Loss: 1.2118\n", "Epoch [4/5], Step [400/600], Loss: 1.2367\n", "Epoch [4/5], Step [500/600], Loss: 1.0584\n", "Epoch [4/5], Step [600/600], Loss: 1.1082\n", "Epoch [5/5], Step [100/600], Loss: 1.0643\n", "Epoch [5/5], Step [200/600], Loss: 1.0167\n", "Epoch [5/5], Step [300/600], Loss: 1.0806\n", "Epoch [5/5], Step [400/600], Loss: 1.0155\n", "Epoch [5/5], Step [500/600], Loss: 0.9915\n", "Epoch [5/5], Step [600/600], Loss: 1.0352\n" ] } ], "source": [ "#train\n", "model.train()\n", "total_step = len(train_loader)\n", "for epoch in range(num_epochs):\n", " for i, (images, labels) in enumerate(train_loader):\n", " # Reshape images to (batch_size, input_size)\n", " images = images.reshape(-1, input_size)\n", " images, labels = images.to(device), labels.to(device)\n", " \n", " # Forward pass\n", " outputs = model(images)\n", " loss = criterion(outputs, labels)\n", " \n", " # Backward and optimize\n", " optimizer.zero_grad()\n", " loss.backward()\n", " optimizer.step()\n", " \n", " if (i+1) % 100 == 0:\n", " print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' .format(epoch+1, num_epochs, i+1, total_step, loss.item()))" ] }, { "cell_type": "code", "execution_count": 11, "id": "9491921b-eb05-4e10-b9ac-f339bbf6ccb0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy of the model on the 10000 test images: 82.73999786376953 %\n" ] } ], "source": [ "#test\n", "model.eval()\n", "with torch.no_grad():\n", " correct = 0\n", " total = 0\n", " for images, labels in test_loader:\n", " images = images.reshape(-1, input_size)\n", " images, labels = images.to(device), labels.to(device)\n", "\n", " outputs = model(images)\n", " _, predicted = torch.max(outputs.data, 1)\n", " total += labels.size(0)\n", " correct += (predicted == labels).sum()\n", "\n", " print('Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }