From 8df811951bf1ef253428286ff17542cbed8efbfa Mon Sep 17 00:00:00 2001 From: natal Date: Mon, 18 Nov 2024 16:34:25 +0100 Subject: [PATCH 1/6] initial project commit --- advanced_ml_excercise_1_2.ipynb | 334 ++++++++++++++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 advanced_ml_excercise_1_2.ipynb diff --git a/advanced_ml_excercise_1_2.ipynb b/advanced_ml_excercise_1_2.ipynb new file mode 100644 index 0000000..a8efba5 --- /dev/null +++ b/advanced_ml_excercise_1_2.ipynb @@ -0,0 +1,334 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "qSYsmgG9TN6A" + }, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torch.optim as optim\n", + "from torchvision import datasets, transforms\n", + "from tqdm import tqdm\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "source": [ + "class FashionMNISTClassifier(nn.Module):\n", + " def __init__(self):\n", + " super(FashionMNISTClassifier, self).__init__()\n", + " self.fc1 = nn.Linear(784, 256)\n", + " self.relu1 = nn.ReLU()\n", + " self.fc2 = nn.Linear(256, 128)\n", + " self.relu2 = nn.ReLU()\n", + " self.fc3 = nn.Linear(128, 10)\n", + "\n", + " def forward(self, x):\n", + " x = x.view(-1, 784)\n", + " x = self.fc1(x)\n", + " x = self.relu1(x)\n", + " x = self.fc2(x)\n", + " x = self.relu2(x)\n", + " x = self.fc3(x)\n", + " return x\n", + "\n", + "train_dataset = datasets.FashionMNIST(root='./data', train=True, download=True, transform=transforms.ToTensor())\n", + "test_dataset = datasets.FashionMNIST(root='./data', train=False, download=True, transform=transforms.ToTensor())\n", + "\n", + "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)\n", + "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)\n", + "\n", + "model = FashionMNISTClassifier()\n", + "criterion = nn.CrossEntropyLoss()\n", + "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", + "\n", + "num_epochs = 10\n", + "for epoch in range(num_epochs):\n", + " for batch_idx, (data, target) in enumerate(tqdm(train_loader)):\n", + " optimizer.zero_grad()\n", + " output = model(data)\n", + " loss = criterion(output, target)\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + "correct = 0\n", + "total = 0\n", + "with torch.no_grad():\n", + " for data, target in test_loader:\n", + " output = model(data)\n", + " _, predicted = torch.max(output.data, 1)\n", + " total += target.size(0)\n", + " correct += (predicted == target).sum().item()\n", + "\n", + "accuracy = 100 * correct / total\n", + "print(f'Test Accuracy: {accuracy:.2f}%')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vf8nB_ZITR5R", + "outputId": "aae5559b-dbec-434c-e95b-5119f2aa5bbe" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 26.4M/26.4M [00:02<00:00, 12.6MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 29.5k/29.5k [00:00<00:00, 202kB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 4.42M/4.42M [00:01<00:00, 3.73MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.92MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 938/938 [00:17<00:00, 54.22it/s]\n", + "100%|██████████| 938/938 [00:20<00:00, 44.75it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 63.32it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.20it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 64.70it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 66.50it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 63.62it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 65.36it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.61it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.25it/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Test Accuracy: 88.76%\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "tensor_transform = transforms.ToTensor()\n", + "\n", + "dataset = datasets.FashionMNIST(root = \"./data\",\n", + " train = True,\n", + " download = True,\n", + " transform = tensor_transform)\n", + "\n", + "dataset_test = datasets.FashionMNIST(root = \"./data\",\n", + " train = False,\n", + " download = True,\n", + " transform = tensor_transform)\n", + "\n", + "\n", + "train_loader = torch.utils.data.DataLoader(dataset = dataset,\n", + " batch_size = 32,\n", + " shuffle = True)\n", + "\n", + "test_loader = torch.utils.data.DataLoader(dataset=dataset_test,\n", + " batch_size=32,\n", + " shuffle=False)\n", + "class AE(torch.nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + "\n", + " self.encoder = torch.nn.Sequential(\n", + " torch.nn.Linear(28 * 28, 128),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(128, 64),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(64, 36),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(36, 18),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(18, 9)\n", + " )\n", + "\n", + " self.decoder = torch.nn.Sequential(\n", + " torch.nn.Linear(9, 18),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(18, 36),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(36, 64),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(64, 128),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(128, 28 * 28),\n", + " torch.nn.Sigmoid()\n", + " )\n", + "\n", + " def forward(self, x):\n", + " encoded = self.encoder(x)\n", + " decoded = self.decoder(encoded)\n", + " return decoded\n", + "\n", + "model = AE()\n", + "\n", + "loss_function = torch.nn.MSELoss()\n", + "\n", + "optimizer = torch.optim.Adam(model.parameters(),\n", + " lr = 1e-3,\n", + " weight_decay = 1e-8)\n", + "epochs = 10\n", + "train_losses = []\n", + "test_losses = []\n", + "\n", + "for epoch in range(epochs):\n", + " model.train()\n", + " train_loss = 0\n", + "\n", + " for (image, _) in train_loader:\n", + " image = image.view(-1, 28*28)\n", + "\n", + " reconstructed = model(image)\n", + " loss = loss_function(reconstructed, image)\n", + "\n", + " optimizer.zero_grad()\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + " train_loss += loss.item()\n", + "\n", + " train_losses.append(train_loss / len(train_loader))\n", + "\n", + " model.eval()\n", + " test_loss = 0\n", + " with torch.no_grad():\n", + " for image, _ in test_loader:\n", + " image = image.view(-1, 28*28)\n", + "\n", + " reconstructed = model(image)\n", + " loss = loss_function(reconstructed, image)\n", + " test_loss += loss.item()\n", + "\n", + " test_losses.append(test_loss / len(test_loader))\n", + " print(f\"Epoch {epoch + 1}/{epochs}, Training Loss: {train_losses[-1]:.4f}, Test Loss: {test_losses[-1]:.4f}\")\n", + "\n" + ], + "metadata": { + "id": "smLBBMQHTUiB" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "plt.plot(range(1, epochs + 1), train_losses, label='Training Loss')\n", + "plt.plot(range(1, epochs + 1), test_losses, label='Test Loss')\n", + "plt.xlabel('Epochs')\n", + "plt.ylabel('Loss')\n", + "plt.legend()\n", + "plt.show()\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " test_iter = iter(test_loader)\n", + " images, _ = next(test_iter)\n", + " images = images.view(-1, 28*28)\n", + " reconstructed = model(images)\n", + "\n", + " fig, axes = plt.subplots(2, 10, figsize=(15, 4))\n", + " for i in range(10):\n", + " axes[0, i].imshow(images[i].view(28, 28), cmap='gray')\n", + " axes[0, i].axis('off')\n", + " axes[1, i].imshow(reconstructed[i].view(28, 28), cmap='gray')\n", + " axes[1, i].axis('off')\n", + " plt.suptitle('Original Images (Top Row) and Reconstructed Images (Bottom Row)')\n", + " plt.show()" + ], + "metadata": { + "id": "27APa6U7TZLt" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From 84f282c7ccc6f286e6266a24165081041b91f4eb Mon Sep 17 00:00:00 2001 From: natal Date: Mon, 18 Nov 2024 17:20:01 +0100 Subject: [PATCH 2/6] task1 --- advanced_ml_excercise_1_2.ipynb | 632 ++++++++++++++++---------------- 1 file changed, 311 insertions(+), 321 deletions(-) diff --git a/advanced_ml_excercise_1_2.ipynb b/advanced_ml_excercise_1_2.ipynb index a8efba5..628a79f 100644 --- a/advanced_ml_excercise_1_2.ipynb +++ b/advanced_ml_excercise_1_2.ipynb @@ -1,334 +1,324 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "qSYsmgG9TN6A" + }, + "outputs": [], + "source": [ + "import torch\n", + "import torch.nn as nn\n", + "import torch.optim as optim\n", + "from torchvision import datasets, transforms\n", + "from tqdm import tqdm\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "source": [ + "class FashionMNISTClassifier(nn.Module):\n", + " def __init__(self):\n", + " super(FashionMNISTClassifier, self).__init__()\n", + " self.fc1 = nn.Linear(784, 256)\n", + " self.relu1 = nn.ReLU()\n", + " self.fc2 = nn.Linear(256, 128)\n", + " self.relu2 = nn.ReLU()\n", + " self.fc3 = nn.Linear(128, 10)\n", + "\n", + " def forward(self, x):\n", + " x = x.view(-1, 784)\n", + " x = self.fc1(x)\n", + " x = self.relu1(x)\n", + " x = self.fc2(x)\n", + " x = self.relu2(x)\n", + " x = self.fc3(x)\n", + " return x\n", + "\n", + "\n", + "train_dataset = datasets.FashionMNIST(root=\"./data\", train=True, download=True, transform=transforms.ToTensor())\n", + "test_dataset = datasets.FashionMNIST(root=\"./data\", train=False, download=True, transform=transforms.ToTensor())\n", + "\n", + "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)\n", + "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)\n", + "\n", + "model = FashionMNISTClassifier()\n", + "criterion = nn.CrossEntropyLoss()\n", + "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", + "\n", + "num_epochs = 10\n", + "for epoch in range(num_epochs):\n", + " for batch_idx, (data, target) in enumerate(tqdm(train_loader)):\n", + " optimizer.zero_grad()\n", + " output = model(data)\n", + " loss = criterion(output, target)\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + "correct = 0\n", + "total = 0\n", + "with torch.no_grad():\n", + " for data, target in test_loader:\n", + " output = model(data)\n", + " _, predicted = torch.max(output.data, 1)\n", + " total += target.size(0)\n", + " correct += (predicted == target).sum().item()\n", + "\n", + "accuracy = 100 * correct / total\n", + "print(f\"Test Accuracy: {accuracy:.2f}%\")" + ], + "metadata": { "colab": { - "provenance": [] + "base_uri": "https://localhost:8080/" }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" + "id": "vf8nB_ZITR5R", + "outputId": "aae5559b-dbec-434c-e95b-5119f2aa5bbe" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 26.4M/26.4M [00:02<00:00, 12.6MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz\n" + ] }, - "language_info": { - "name": "python" - } - }, - "cells": [ { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "id": "qSYsmgG9TN6A" - }, - "outputs": [], - "source": [ - "import torch\n", - "import torch.nn as nn\n", - "import torch.optim as optim\n", - "from torchvision import datasets, transforms\n", - "from tqdm import tqdm\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt" - ] + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 29.5k/29.5k [00:00<00:00, 202kB/s]\n" + ] }, { - "cell_type": "code", - "source": [ - "class FashionMNISTClassifier(nn.Module):\n", - " def __init__(self):\n", - " super(FashionMNISTClassifier, self).__init__()\n", - " self.fc1 = nn.Linear(784, 256)\n", - " self.relu1 = nn.ReLU()\n", - " self.fc2 = nn.Linear(256, 128)\n", - " self.relu2 = nn.ReLU()\n", - " self.fc3 = nn.Linear(128, 10)\n", - "\n", - " def forward(self, x):\n", - " x = x.view(-1, 784)\n", - " x = self.fc1(x)\n", - " x = self.relu1(x)\n", - " x = self.fc2(x)\n", - " x = self.relu2(x)\n", - " x = self.fc3(x)\n", - " return x\n", - "\n", - "train_dataset = datasets.FashionMNIST(root='./data', train=True, download=True, transform=transforms.ToTensor())\n", - "test_dataset = datasets.FashionMNIST(root='./data', train=False, download=True, transform=transforms.ToTensor())\n", - "\n", - "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)\n", - "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)\n", - "\n", - "model = FashionMNISTClassifier()\n", - "criterion = nn.CrossEntropyLoss()\n", - "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", - "\n", - "num_epochs = 10\n", - "for epoch in range(num_epochs):\n", - " for batch_idx, (data, target) in enumerate(tqdm(train_loader)):\n", - " optimizer.zero_grad()\n", - " output = model(data)\n", - " loss = criterion(output, target)\n", - " loss.backward()\n", - " optimizer.step()\n", - "\n", - "correct = 0\n", - "total = 0\n", - "with torch.no_grad():\n", - " for data, target in test_loader:\n", - " output = model(data)\n", - " _, predicted = torch.max(output.data, 1)\n", - " total += target.size(0)\n", - " correct += (predicted == target).sum().item()\n", - "\n", - "accuracy = 100 * correct / total\n", - "print(f'Test Accuracy: {accuracy:.2f}%')" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "vf8nB_ZITR5R", - "outputId": "aae5559b-dbec-434c-e95b-5119f2aa5bbe" - }, - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "100%|██████████| 26.4M/26.4M [00:02<00:00, 12.6MB/s]\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Extracting ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", - "\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "100%|██████████| 29.5k/29.5k [00:00<00:00, 202kB/s]\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Extracting ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", - "\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "100%|██████████| 4.42M/4.42M [00:01<00:00, 3.73MB/s]\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Extracting ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", - "\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.92MB/s]\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Extracting ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", - "\n" - ] - }, - { - "output_type": "stream", - "name": "stderr", - "text": [ - "100%|██████████| 938/938 [00:17<00:00, 54.22it/s]\n", - "100%|██████████| 938/938 [00:20<00:00, 44.75it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 63.32it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.20it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 64.70it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 66.50it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 63.62it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 65.36it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.61it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.25it/s]\n" - ] - }, - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Test Accuracy: 88.76%\n" - ] - } - ] + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz\n" + ] }, { - "cell_type": "code", - "source": [ - "tensor_transform = transforms.ToTensor()\n", - "\n", - "dataset = datasets.FashionMNIST(root = \"./data\",\n", - " train = True,\n", - " download = True,\n", - " transform = tensor_transform)\n", - "\n", - "dataset_test = datasets.FashionMNIST(root = \"./data\",\n", - " train = False,\n", - " download = True,\n", - " transform = tensor_transform)\n", - "\n", - "\n", - "train_loader = torch.utils.data.DataLoader(dataset = dataset,\n", - " batch_size = 32,\n", - " shuffle = True)\n", - "\n", - "test_loader = torch.utils.data.DataLoader(dataset=dataset_test,\n", - " batch_size=32,\n", - " shuffle=False)\n", - "class AE(torch.nn.Module):\n", - " def __init__(self):\n", - " super().__init__()\n", - "\n", - "\n", - " self.encoder = torch.nn.Sequential(\n", - " torch.nn.Linear(28 * 28, 128),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(128, 64),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(64, 36),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(36, 18),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(18, 9)\n", - " )\n", - "\n", - " self.decoder = torch.nn.Sequential(\n", - " torch.nn.Linear(9, 18),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(18, 36),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(36, 64),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(64, 128),\n", - " torch.nn.ReLU(),\n", - " torch.nn.Linear(128, 28 * 28),\n", - " torch.nn.Sigmoid()\n", - " )\n", - "\n", - " def forward(self, x):\n", - " encoded = self.encoder(x)\n", - " decoded = self.decoder(encoded)\n", - " return decoded\n", - "\n", - "model = AE()\n", - "\n", - "loss_function = torch.nn.MSELoss()\n", - "\n", - "optimizer = torch.optim.Adam(model.parameters(),\n", - " lr = 1e-3,\n", - " weight_decay = 1e-8)\n", - "epochs = 10\n", - "train_losses = []\n", - "test_losses = []\n", - "\n", - "for epoch in range(epochs):\n", - " model.train()\n", - " train_loss = 0\n", - "\n", - " for (image, _) in train_loader:\n", - " image = image.view(-1, 28*28)\n", - "\n", - " reconstructed = model(image)\n", - " loss = loss_function(reconstructed, image)\n", - "\n", - " optimizer.zero_grad()\n", - " loss.backward()\n", - " optimizer.step()\n", - "\n", - " train_loss += loss.item()\n", - "\n", - " train_losses.append(train_loss / len(train_loader))\n", - "\n", - " model.eval()\n", - " test_loss = 0\n", - " with torch.no_grad():\n", - " for image, _ in test_loader:\n", - " image = image.view(-1, 28*28)\n", - "\n", - " reconstructed = model(image)\n", - " loss = loss_function(reconstructed, image)\n", - " test_loss += loss.item()\n", - "\n", - " test_losses.append(test_loss / len(test_loader))\n", - " print(f\"Epoch {epoch + 1}/{epochs}, Training Loss: {train_losses[-1]:.4f}, Test Loss: {test_losses[-1]:.4f}\")\n", - "\n" - ], - "metadata": { - "id": "smLBBMQHTUiB" - }, - "execution_count": null, - "outputs": [] + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 4.42M/4.42M [00:01<00:00, 3.73MB/s]\n" + ] }, { - "cell_type": "code", - "source": [ - "plt.plot(range(1, epochs + 1), train_losses, label='Training Loss')\n", - "plt.plot(range(1, epochs + 1), test_losses, label='Test Loss')\n", - "plt.xlabel('Epochs')\n", - "plt.ylabel('Loss')\n", - "plt.legend()\n", - "plt.show()\n", - "\n", - "model.eval()\n", - "with torch.no_grad():\n", - " test_iter = iter(test_loader)\n", - " images, _ = next(test_iter)\n", - " images = images.view(-1, 28*28)\n", - " reconstructed = model(images)\n", - "\n", - " fig, axes = plt.subplots(2, 10, figsize=(15, 4))\n", - " for i in range(10):\n", - " axes[0, i].imshow(images[i].view(28, 28), cmap='gray')\n", - " axes[0, i].axis('off')\n", - " axes[1, i].imshow(reconstructed[i].view(28, 28), cmap='gray')\n", - " axes[1, i].axis('off')\n", - " plt.suptitle('Original Images (Top Row) and Reconstructed Images (Bottom Row)')\n", - " plt.show()" - ], - "metadata": { - "id": "27APa6U7TZLt" - }, - "execution_count": null, - "outputs": [] + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz\n", + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.92MB/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Extracting ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "100%|██████████| 938/938 [00:17<00:00, 54.22it/s]\n", + "100%|██████████| 938/938 [00:20<00:00, 44.75it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 63.32it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.20it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 64.70it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 66.50it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 63.62it/s]\n", + "100%|██████████| 938/938 [00:14<00:00, 65.36it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.61it/s]\n", + "100%|██████████| 938/938 [00:13<00:00, 67.25it/s]\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Test Accuracy: 88.76%\n" + ] } - ] + ] + }, + { + "cell_type": "code", + "source": [ + "tensor_transform = transforms.ToTensor()\n", + "\n", + "dataset = datasets.FashionMNIST(root=\"./data\", train=True, download=True, transform=tensor_transform)\n", + "\n", + "dataset_test = datasets.FashionMNIST(root=\"./data\", train=False, download=True, transform=tensor_transform)\n", + "\n", + "\n", + "train_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=32, shuffle=True)\n", + "\n", + "test_loader = torch.utils.data.DataLoader(dataset=dataset_test, batch_size=32, shuffle=False)\n", + "\n", + "\n", + "class AE(torch.nn.Module):\n", + " def __init__(self):\n", + " super().__init__()\n", + "\n", + " self.encoder = torch.nn.Sequential(\n", + " torch.nn.Linear(28 * 28, 128),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(128, 64),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(64, 36),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(36, 18),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(18, 9),\n", + " )\n", + "\n", + " self.decoder = torch.nn.Sequential(\n", + " torch.nn.Linear(9, 18),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(18, 36),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(36, 64),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(64, 128),\n", + " torch.nn.ReLU(),\n", + " torch.nn.Linear(128, 28 * 28),\n", + " torch.nn.Sigmoid(),\n", + " )\n", + "\n", + " def forward(self, x):\n", + " encoded = self.encoder(x)\n", + " decoded = self.decoder(encoded)\n", + " return decoded\n", + "\n", + "\n", + "model = AE()\n", + "\n", + "loss_function = torch.nn.MSELoss()\n", + "\n", + "optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-8)\n", + "epochs = 10\n", + "train_losses = []\n", + "test_losses = []\n", + "\n", + "for epoch in range(epochs):\n", + " model.train()\n", + " train_loss = 0\n", + "\n", + " for image, _ in train_loader:\n", + " image = image.view(-1, 28 * 28)\n", + "\n", + " reconstructed = model(image)\n", + " loss = loss_function(reconstructed, image)\n", + "\n", + " optimizer.zero_grad()\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + " train_loss += loss.item()\n", + "\n", + " train_losses.append(train_loss / len(train_loader))\n", + "\n", + " model.eval()\n", + " test_loss = 0\n", + " with torch.no_grad():\n", + " for image, _ in test_loader:\n", + " image = image.view(-1, 28 * 28)\n", + "\n", + " reconstructed = model(image)\n", + " loss = loss_function(reconstructed, image)\n", + " test_loss += loss.item()\n", + "\n", + " test_losses.append(test_loss / len(test_loader))\n", + " print(f\"Epoch {epoch + 1}/{epochs}, Training Loss: {train_losses[-1]:.4f}, Test Loss: {test_losses[-1]:.4f}\")" + ], + "metadata": { + "id": "smLBBMQHTUiB" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "plt.plot(range(1, epochs + 1), train_losses, label=\"Training Loss\")\n", + "plt.plot(range(1, epochs + 1), test_losses, label=\"Test Loss\")\n", + "plt.xlabel(\"Epochs\")\n", + "plt.ylabel(\"Loss\")\n", + "plt.legend()\n", + "plt.show()\n", + "\n", + "model.eval()\n", + "with torch.no_grad():\n", + " test_iter = iter(test_loader)\n", + " images, _ = next(test_iter)\n", + " images = images.view(-1, 28 * 28)\n", + " reconstructed = model(images)\n", + "\n", + " fig, axes = plt.subplots(2, 10, figsize=(15, 4))\n", + " for i in range(10):\n", + " axes[0, i].imshow(images[i].view(28, 28), cmap=\"gray\")\n", + " axes[0, i].axis(\"off\")\n", + " axes[1, i].imshow(reconstructed[i].view(28, 28), cmap=\"gray\")\n", + " axes[1, i].axis(\"off\")\n", + " plt.suptitle(\"Original Images (Top Row) and Reconstructed Images (Bottom Row)\")\n", + " plt.show()" + ], + "metadata": { + "id": "27APa6U7TZLt" + }, + "execution_count": null, + "outputs": [] + } + ] } \ No newline at end of file From e94d97dbca4a2f7eb2cb82501f8a0f143bf30a17 Mon Sep 17 00:00:00 2001 From: natal Date: Mon, 18 Nov 2024 23:30:21 +0100 Subject: [PATCH 3/6] task2 --- .pre-commit-config.yaml | 20 ++++++++++++++++++++ README.md | 2 +- advanced_ml_excercise_1_2.ipynb | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c1fba16..8c3db5d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,3 +17,23 @@ repos: - id: end-of-file-fixer - id: mixed-line-ending - id: trailing-whitespace + # Black formatter + - repo: https://github.com/psf/black + rev: 24.4.0 + hooks: + - id: black + args: [ "--line-length=120" ] + + # Codespell - Fix common misspellings in text files. + - repo: https://github.com/codespell-project/codespell + rev: v2.2.6 + hooks: + - id: codespell + args: [ --write-changes ] + + # Pyupgrade - automatically upgrade syntax for newer versions of the language. + - repo: https://github.com/asottile/pyupgrade + rev: v3.15.2 + hooks: + - id: pyupgrade + args: [ --py310-plus ] diff --git a/README.md b/README.md index 97b4080..729f8e2 100644 --- a/README.md +++ b/README.md @@ -253,4 +253,4 @@ study.best_params # E.g. {'x': 2.002108042} --- ## Sources: -- [github.com/leggedrobotics/plr-exercise](https://github.com/leggedrobotics/plr-exercise) by @JonasFrey96 \ No newline at end of file +- [github.com/leggedrobotics/plr-exercise](https://github.com/leggedrobotics/plr-exercise) by @JonasFrey96 diff --git a/advanced_ml_excercise_1_2.ipynb b/advanced_ml_excercise_1_2.ipynb index 628a79f..a1d6c8a 100644 --- a/advanced_ml_excercise_1_2.ipynb +++ b/advanced_ml_excercise_1_2.ipynb @@ -321,4 +321,4 @@ "outputs": [] } ] -} \ No newline at end of file +} From 7874c27f88a953a9adf932903654632ff0a7067a Mon Sep 17 00:00:00 2001 From: natal Date: Tue, 19 Nov 2024 00:01:17 +0100 Subject: [PATCH 4/6] task3 --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 82f9275..f4cd0f9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ __pycache__/ # C extensions *.so +*.toml +*lock # Distribution / packaging .Python From df2a87de996cbd322970c01c7d1b9bdce48ceea1 Mon Sep 17 00:00:00 2001 From: natal Date: Tue, 19 Nov 2024 00:48:02 +0100 Subject: [PATCH 5/6] task_4 --- advanced_ml_excercise_1_2.ipynb | 187 ++++++++++++++++---------------- 1 file changed, 95 insertions(+), 92 deletions(-) diff --git a/advanced_ml_excercise_1_2.ipynb b/advanced_ml_excercise_1_2.ipynb index a1d6c8a..f9ad467 100644 --- a/advanced_ml_excercise_1_2.ipynb +++ b/advanced_ml_excercise_1_2.ipynb @@ -1,24 +1,11 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { - "id": "qSYsmgG9TN6A" + "id": "qSYsmgG9TN6A", + "is_executing": true }, "outputs": [], "source": [ @@ -33,57 +20,7 @@ }, { "cell_type": "code", - "source": [ - "class FashionMNISTClassifier(nn.Module):\n", - " def __init__(self):\n", - " super(FashionMNISTClassifier, self).__init__()\n", - " self.fc1 = nn.Linear(784, 256)\n", - " self.relu1 = nn.ReLU()\n", - " self.fc2 = nn.Linear(256, 128)\n", - " self.relu2 = nn.ReLU()\n", - " self.fc3 = nn.Linear(128, 10)\n", - "\n", - " def forward(self, x):\n", - " x = x.view(-1, 784)\n", - " x = self.fc1(x)\n", - " x = self.relu1(x)\n", - " x = self.fc2(x)\n", - " x = self.relu2(x)\n", - " x = self.fc3(x)\n", - " return x\n", - "\n", - "\n", - "train_dataset = datasets.FashionMNIST(root=\"./data\", train=True, download=True, transform=transforms.ToTensor())\n", - "test_dataset = datasets.FashionMNIST(root=\"./data\", train=False, download=True, transform=transforms.ToTensor())\n", - "\n", - "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)\n", - "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)\n", - "\n", - "model = FashionMNISTClassifier()\n", - "criterion = nn.CrossEntropyLoss()\n", - "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", - "\n", - "num_epochs = 10\n", - "for epoch in range(num_epochs):\n", - " for batch_idx, (data, target) in enumerate(tqdm(train_loader)):\n", - " optimizer.zero_grad()\n", - " output = model(data)\n", - " loss = criterion(output, target)\n", - " loss.backward()\n", - " optimizer.step()\n", - "\n", - "correct = 0\n", - "total = 0\n", - "with torch.no_grad():\n", - " for data, target in test_loader:\n", - " output = model(data)\n", - " _, predicted = torch.max(output.data, 1)\n", - " total += target.size(0)\n", - " correct += (predicted == target).sum().item()\n", - "\n", - "accuracy = 100 * correct / total\n", - "print(f\"Test Accuracy: {accuracy:.2f}%\")" - ], + "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -91,26 +28,25 @@ "id": "vf8nB_ZITR5R", "outputId": "aae5559b-dbec-434c-e95b-5119f2aa5bbe" }, - "execution_count": 2, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "100%|██████████| 26.4M/26.4M [00:02<00:00, 12.6MB/s]\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Extracting ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", "\n", @@ -119,15 +55,15 @@ ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "100%|██████████| 29.5k/29.5k [00:00<00:00, 202kB/s]\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Extracting ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", "\n", @@ -136,15 +72,15 @@ ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "100%|██████████| 4.42M/4.42M [00:01<00:00, 3.73MB/s]\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Extracting ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", "\n", @@ -153,23 +89,23 @@ ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.92MB/s]\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Extracting ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", "\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "100%|██████████| 938/938 [00:17<00:00, 54.22it/s]\n", "100%|██████████| 938/938 [00:20<00:00, 44.75it/s]\n", @@ -184,16 +120,72 @@ ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Test Accuracy: 88.76%\n" ] } + ], + "source": [ + "class FashionMNISTClassifier(nn.Module):\n", + " def __init__(self):\n", + " super(FashionMNISTClassifier, self).__init__()\n", + " self.fc1 = nn.Linear(784, 256)\n", + " self.relu1 = nn.ReLU()\n", + " self.fc2 = nn.Linear(256, 128)\n", + " self.relu2 = nn.ReLU()\n", + " self.fc3 = nn.Linear(128, 10)\n", + "\n", + " def forward(self, x):\n", + " x = x.view(-1, 784)\n", + " x = self.fc1(x)\n", + " x = self.relu1(x)\n", + " x = self.fc2(x)\n", + " x = self.relu2(x)\n", + " x = self.fc3(x)\n", + " return x\n", + "\n", + "\n", + "train_dataset = datasets.FashionMNIST(root=\"./data\", train=True, download=True, transform=transforms.ToTensor())\n", + "test_dataset = datasets.FashionMNIST(root=\"./data\", train=False, download=True, transform=transforms.ToTensor())\n", + "\n", + "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)\n", + "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)\n", + "\n", + "model = FashionMNISTClassifier()\n", + "criterion = nn.CrossEntropyLoss()\n", + "optimizer = optim.Adam(model.parameters(), lr=0.001)\n", + "\n", + "num_epochs = 10\n", + "for epoch in range(num_epochs):\n", + " for batch_idx, (data, target) in enumerate(tqdm(train_loader)):\n", + " optimizer.zero_grad()\n", + " output = model(data)\n", + " loss = criterion(output, target)\n", + " loss.backward()\n", + " optimizer.step()\n", + "\n", + "correct = 0\n", + "total = 0\n", + "with torch.no_grad():\n", + " for data, target in test_loader:\n", + " output = model(data)\n", + " _, predicted = torch.max(output.data, 1)\n", + " total += target.size(0)\n", + " correct += (predicted == target).sum().item()\n", + "\n", + "accuracy = 100 * correct / total\n", + "print(f\"Test Accuracy: {accuracy:.2f}%\")" ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "smLBBMQHTUiB" + }, + "outputs": [], "source": [ "tensor_transform = transforms.ToTensor()\n", "\n", @@ -281,15 +273,15 @@ "\n", " test_losses.append(test_loss / len(test_loader))\n", " print(f\"Epoch {epoch + 1}/{epochs}, Training Loss: {train_losses[-1]:.4f}, Test Loss: {test_losses[-1]:.4f}\")" - ], - "metadata": { - "id": "smLBBMQHTUiB" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "27APa6U7TZLt" + }, + "outputs": [], "source": [ "plt.plot(range(1, epochs + 1), train_losses, label=\"Training Loss\")\n", "plt.plot(range(1, epochs + 1), test_losses, label=\"Test Loss\")\n", @@ -313,12 +305,23 @@ " axes[1, i].axis(\"off\")\n", " plt.suptitle(\"Original Images (Top Row) and Reconstructed Images (Bottom Row)\")\n", " plt.show()" - ], - "metadata": { - "id": "27APa6U7TZLt" - }, - "execution_count": null, - "outputs": [] + ] } - ] + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "lab3_obrazy", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.10.11" + } + }, + "nbformat": 4, + "nbformat_minor": 0 } From 79d70c2ae5ce0db9eac8ba255ebe26d327fc673b Mon Sep 17 00:00:00 2001 From: natal Date: Tue, 19 Nov 2024 00:57:36 +0100 Subject: [PATCH 6/6] ex_5 --- advanced_ml_excercise_1_2.ipynb | 201 +++++++++++++++++++++++++++----- optuna_1.py | 12 ++ 2 files changed, 185 insertions(+), 28 deletions(-) create mode 100644 optuna_1.py diff --git a/advanced_ml_excercise_1_2.ipynb b/advanced_ml_excercise_1_2.ipynb index f9ad467..e7712a6 100644 --- a/advanced_ml_excercise_1_2.ipynb +++ b/advanced_ml_excercise_1_2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "id": "qSYsmgG9TN6A", "is_executing": true @@ -15,12 +15,13 @@ "from torchvision import datasets, transforms\n", "from tqdm import tqdm\n", "import numpy as np\n", - "import matplotlib.pyplot as plt" + "import matplotlib.pyplot as plt\n", + "import wandb" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -29,77 +30,144 @@ "outputId": "aae5559b-dbec-434c-e95b-5119f2aa5bbe" }, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mnatalka-ozarek\u001b[0m (\u001b[33mnatalka-ozarek-agh\u001b[0m). Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" + ] + }, + { + "data": { + "text/html": [ + "Tracking run with wandb version 0.18.7" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Run data is saved locally in c:\\Users\\natal\\OneDrive\\Pulpit\\sem 2 st 2\\adv_2\\wandb\\run-20241119_004557-0lg3rxut" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Syncing run happy-bush-1 to Weights & Biases (docs)
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View project at https://wandb.ai/natalka-ozarek-agh/my-awesome-project" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View run at https://wandb.ai/natalka-ozarek-agh/my-awesome-project/runs/0lg3rxut" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, { "name": "stdout", "output_type": "stream", "text": [ "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz\n" + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to ./data\\FashionMNIST\\raw\\train-images-idx3-ubyte.gz\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 26.4M/26.4M [00:02<00:00, 12.6MB/s]\n" + "100%|██████████| 26.4M/26.4M [00:27<00:00, 951kB/s] \n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Extracting ./data/FashionMNIST/raw/train-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "Extracting ./data\\FashionMNIST\\raw\\train-images-idx3-ubyte.gz to ./data\\FashionMNIST\\raw\n", "\n", "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz\n" + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to ./data\\FashionMNIST\\raw\\train-labels-idx1-ubyte.gz\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 29.5k/29.5k [00:00<00:00, 202kB/s]\n" + "100%|██████████| 29.5k/29.5k [00:00<00:00, 2.15MB/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Extracting ./data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "Extracting ./data\\FashionMNIST\\raw\\train-labels-idx1-ubyte.gz to ./data\\FashionMNIST\\raw\n", "\n", "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz\n" + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to ./data\\FashionMNIST\\raw\\t10k-images-idx3-ubyte.gz\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 4.42M/4.42M [00:01<00:00, 3.73MB/s]\n" + "100%|██████████| 4.42M/4.42M [00:02<00:00, 1.51MB/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Extracting ./data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to ./data/FashionMNIST/raw\n", + "Extracting ./data\\FashionMNIST\\raw\\t10k-images-idx3-ubyte.gz to ./data\\FashionMNIST\\raw\n", "\n", "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz\n", - "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz\n" + "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to ./data\\FashionMNIST\\raw\\t10k-labels-idx1-ubyte.gz\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.92MB/s]\n" + "100%|██████████| 5.15k/5.15k [00:00<00:00, 4.60MB/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Extracting ./data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to ./data/FashionMNIST/raw\n", + "Extracting ./data\\FashionMNIST\\raw\\t10k-labels-idx1-ubyte.gz to ./data\\FashionMNIST\\raw\n", "\n" ] }, @@ -107,24 +175,77 @@ "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 938/938 [00:17<00:00, 54.22it/s]\n", - "100%|██████████| 938/938 [00:20<00:00, 44.75it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 63.32it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.20it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 64.70it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 66.50it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 63.62it/s]\n", - "100%|██████████| 938/938 [00:14<00:00, 65.36it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.61it/s]\n", - "100%|██████████| 938/938 [00:13<00:00, 67.25it/s]\n" + "100%|██████████| 938/938 [00:20<00:00, 46.50it/s]\n", + "100%|██████████| 938/938 [00:21<00:00, 43.97it/s]\n", + "100%|██████████| 938/938 [00:19<00:00, 48.62it/s]\n", + "100%|██████████| 938/938 [00:20<00:00, 46.19it/s]\n", + "100%|██████████| 938/938 [00:20<00:00, 44.99it/s]\n", + "100%|██████████| 938/938 [00:20<00:00, 45.16it/s]\n", + "100%|██████████| 938/938 [00:19<00:00, 47.41it/s]\n", + "100%|██████████| 938/938 [00:19<00:00, 47.61it/s]\n", + "100%|██████████| 938/938 [00:21<00:00, 43.90it/s]\n", + "100%|██████████| 938/938 [00:21<00:00, 43.75it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Test Accuracy: 88.76%\n" + "Test Accuracy: 88.53%\n" ] + }, + { + "data": { + "text/html": [ + "\n", + " \n", + "

Run history:


loss▆▅▃▆▅▅▃▆▇▄▅▃▃▅▄▃▆▄█▃▂▆▅▂▂▂▂▃▁▂▄▂▂▂▃▄▂▄▃▃

Run summary:


loss0.24563

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View run happy-bush-1 at: https://wandb.ai/natalka-ozarek-agh/my-awesome-project/runs/0lg3rxut
View project at: https://wandb.ai/natalka-ozarek-agh/my-awesome-project
Synced 4 W&B file(s), 0 media file(s), 3 artifact file(s) and 0 other file(s)" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Find logs at: .\\wandb\\run-20241119_004557-0lg3rxut\\logs" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ @@ -147,6 +268,19 @@ " return x\n", "\n", "\n", + "epochs = 10\n", + "lr = 0.01\n", + "\n", + "run = wandb.init(\n", + " # Set the project where this run will be logged\n", + " project=\"my-awesome-project\",\n", + " # Track hyperparameters and run metadata\n", + " config={\n", + " \"learning_rate\": lr,\n", + " \"epochs\": epochs,\n", + " },\n", + ")\n", + "\n", "train_dataset = datasets.FashionMNIST(root=\"./data\", train=True, download=True, transform=transforms.ToTensor())\n", "test_dataset = datasets.FashionMNIST(root=\"./data\", train=False, download=True, transform=transforms.ToTensor())\n", "\n", @@ -165,6 +299,7 @@ " loss = criterion(output, target)\n", " loss.backward()\n", " optimizer.step()\n", + " wandb.log({\"loss\": loss})\n", "\n", "correct = 0\n", "total = 0\n", @@ -176,7 +311,9 @@ " correct += (predicted == target).sum().item()\n", "\n", "accuracy = 100 * correct / total\n", - "print(f\"Test Accuracy: {accuracy:.2f}%\")" + "print(f\"Test Accuracy: {accuracy:.2f}%\")\n", + "\n", + "wandb.finish()" ] }, { @@ -313,12 +450,20 @@ "provenance": [] }, "kernelspec": { - "display_name": "lab3_obrazy", + "display_name": ".venv", "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.11" } }, diff --git a/optuna_1.py b/optuna_1.py new file mode 100644 index 0000000..16e73eb --- /dev/null +++ b/optuna_1.py @@ -0,0 +1,12 @@ +import optuna + + +def objective(trial): + x = trial.suggest_float("x", -10, 10) + return (x - 2) ** 2 + + +study = optuna.create_study() +study.optimize(objective, n_trials=100) + +study.best_params # E.g. {'x': 2.002108042}