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 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 new file mode 100644 index 0000000..e7712a6 --- /dev/null +++ b/advanced_ml_excercise_1_2.ipynb @@ -0,0 +1,472 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "id": "qSYsmgG9TN6A", + "is_executing": true + }, + "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\n", + "import wandb" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vf8nB_ZITR5R", + "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" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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", + "\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" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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", + "\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" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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", + "\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" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "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.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": [ + "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", + "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", + "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", + " wandb.log({\"loss\": loss})\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}%\")\n", + "\n", + "wandb.finish()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "smLBBMQHTUiB" + }, + "outputs": [], + "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}\")" + ] + }, + { + "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", + "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": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "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" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} 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}