diff --git a/Test.ipynb b/Test.ipynb deleted file mode 100644 index 4ca738159d67171ce1f430fa77f28ca621b77e5e..0000000000000000000000000000000000000000 --- a/Test.ipynb +++ /dev/null @@ -1,237 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Test Pytorch" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "tensor([[ 0.6650, 2.0888, -0.3361, -0.6212, 0.6276, -0.3091, 1.6601, -0.1404,\n", - " 0.0224, -1.1360],\n", - " [-0.8311, -1.6385, 0.3011, 1.2989, 0.2328, -0.0061, 0.4039, 0.3956,\n", - " -0.2067, -0.8877],\n", - " [-0.2899, -1.7310, -0.4318, 1.3837, 3.1771, 1.7354, 0.3680, -0.0743,\n", - " -0.1610, 0.8281],\n", - " [-0.2591, -2.0913, 0.8630, -0.3533, 0.9323, -1.4520, 0.4476, -0.2588,\n", - " -0.0963, 1.8449],\n", - " [ 0.4599, 0.3258, 0.7780, 0.6943, -0.4343, -0.0536, -0.1049, 0.2867,\n", - " 0.5493, -1.2934],\n", - " [-0.6990, 0.0783, -0.8745, 1.2521, 1.5363, 0.8770, -0.5319, -0.2629,\n", - " 0.7732, -0.5001],\n", - " [-0.2902, -0.8901, 0.1904, 0.7456, 0.5802, 0.0443, -1.2447, 2.1954,\n", - " 0.5382, 0.2219],\n", - " [ 1.0372, -0.7516, 0.7940, 0.8207, 0.6601, 0.0317, 0.1410, -1.7062,\n", - " -0.6549, 0.6287],\n", - " [ 0.6680, 1.0136, -0.0813, -1.4382, 0.4640, 1.2923, -1.0299, 0.5684,\n", - " 1.6626, -1.1921],\n", - " [-1.1864, -0.6625, -1.0846, 0.7550, -0.8748, 0.2835, -1.4264, 1.0279,\n", - " -0.6046, 0.6298],\n", - " [-1.9253, -0.4960, -0.8379, 1.8726, -2.0282, 0.0869, 1.2508, 0.0390,\n", - " -1.7213, 0.3269],\n", - " [ 1.7196, -1.6188, -0.4604, -1.0196, -0.8883, 1.2987, 0.4795, 0.5581,\n", - " -1.0138, -0.2184],\n", - " [-0.6600, 0.5816, -0.6574, 0.4684, 1.2546, -0.4140, -0.2636, 0.4267,\n", - " 0.1736, -1.5019],\n", - " [-0.8852, 0.6677, -1.3074, -1.2241, -1.4054, 0.0919, 1.5832, 1.4357,\n", - " -1.9016, 0.7274]])\n", - "AlexNet(\n", - " (features): Sequential(\n", - " (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))\n", - " (1): ReLU(inplace=True)\n", - " (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n", - " (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n", - " (4): ReLU(inplace=True)\n", - " (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n", - " (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n", - " (7): ReLU(inplace=True)\n", - " (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n", - " (9): ReLU(inplace=True)\n", - " (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n", - " (11): ReLU(inplace=True)\n", - " (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n", - " )\n", - " (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))\n", - " (classifier): Sequential(\n", - " (0): Dropout(p=0.5, inplace=False)\n", - " (1): Linear(in_features=9216, out_features=4096, bias=True)\n", - " (2): ReLU(inplace=True)\n", - " (3): Dropout(p=0.5, inplace=False)\n", - " (4): Linear(in_features=4096, out_features=4096, bias=True)\n", - " (5): ReLU(inplace=True)\n", - " (6): Linear(in_features=4096, out_features=1000, bias=True)\n", - " )\n", - ")\n" - ] - } - ], - "source": [ - "import torch\n", - "\n", - "N, D = 14, 10\n", - "x = torch.randn(N, D).type(torch.FloatTensor)\n", - "print(x)\n", - "\n", - "from torchvision import models\n", - "\n", - "alexnet = models.alexnet()\n", - "print(alexnet)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise 1: CNN on CIFAR10" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "See if cuda is available ==> it is not" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CUDA is not available. Training on CPU ...\n" - ] - } - ], - "source": [ - "import torch\n", - "\n", - "# check if CUDA is available\n", - "train_on_gpu = torch.cuda.is_available()\n", - "\n", - "if not train_on_gpu:\n", - " print(\"CUDA is not available. Training on CPU ...\")\n", - "else:\n", - " print(\"CUDA is available! Training on GPU ...\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Load the CIFAR10 dataset" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Files already downloaded and verified\n", - "Files already downloaded and verified\n" - ] - } - ], - "source": [ - "import numpy as np\n", - "from torchvision import datasets, transforms\n", - "from torch.utils.data.sampler import SubsetRandomSampler\n", - "\n", - "# number of subprocesses to use for data loading\n", - "num_workers = 0\n", - "# how many samples per batch to load\n", - "batch_size = 20\n", - "# percentage of training set to use as validation\n", - "valid_size = 0.2\n", - "\n", - "# convert data to a normalized torch.FloatTensor\n", - "transform = transforms.Compose(\n", - " [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]\n", - ")\n", - "\n", - "# choose the training and test datasets\n", - "train_data = datasets.CIFAR10(\"data\", train=True, download=True, transform=transform)\n", - "test_data = datasets.CIFAR10(\"data\", train=False, download=True, transform=transform)\n", - "\n", - "# obtain training indices that will be used for validation\n", - "num_train = len(train_data)\n", - "indices = list(range(num_train))\n", - "np.random.shuffle(indices)\n", - "split = int(np.floor(valid_size * num_train))\n", - "train_idx, valid_idx = indices[split:], indices[:split]\n", - "\n", - "# define samplers for obtaining training and validation batches\n", - "train_sampler = SubsetRandomSampler(train_idx)\n", - "valid_sampler = SubsetRandomSampler(valid_idx)\n", - "\n", - "# prepare data loaders (combine dataset and sampler)\n", - "train_loader = torch.utils.data.DataLoader(\n", - " train_data, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers\n", - ")\n", - "valid_loader = torch.utils.data.DataLoader(\n", - " train_data, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers\n", - ")\n", - "test_loader = torch.utils.data.DataLoader(\n", - " test_data, batch_size=batch_size, num_workers=num_workers\n", - ")\n", - "\n", - "# specify the image classes\n", - "classes = [\n", - " \"airplane\",\n", - " \"automobile\",\n", - " \"bird\",\n", - " \"cat\",\n", - " \"deer\",\n", - " \"dog\",\n", - " \"frog\",\n", - " \"horse\",\n", - " \"ship\",\n", - " \"truck\",\n", - "]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "env", - "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.10.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}