{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "FxRTm4zWpwoo" }, "source": [ "# Ablation experiment" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "uHitmULK0W1v" }, "source": [ "[](https://colab.research.google.com/github/fostiropoulos/ablator/blob/v0.0.1-mp/docs/source/notebooks/HPO-tutorial.ipynb)\n", "\n", "After your prototype has been verified and runs smoothly with `ProtoTrainer`, you can scale it to an ablation study/ HPO and analyze the results.\n", "\n", "In this chapter, we will learn how to set up and launch a parallel experiment for an ablation study with Ablator.\n", "\n", "Similarly to launching a prototype experiment, there are also 3 main steps to run an ablation experiment in ablator:\n", "\n", "- Configure the parallel experiment.\n", "\n", "- Create model wrapper that defines boiler-plate code for training and evaluating models.\n", "\n", "- Create the trainer and launch the experiment.\n", "\n", "Recall from the [Introduction tutorial](./Introduction.ipynb), Ablator combines Optuna for running multiple trials from defined search spaces and Ray back-end for parallelizing the trials. So, an extra step is to start a ray cluster before launching the experiment (but if you don't want to do this, abator will automatically start a ray cluster for you)." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "iw2ie4ik0fiP" }, "source": [ "Let us first import all necessary dependencies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " import ablator\n", "except:\n", " !pip install ablator\n", " print(\"Stopping RUNTIME! Please run again\") # This script automatically restart runtime (if ablator is not found and installing is needed) so changes are applied\n", " import os\n", "\n", " os.kill(os.getpid(), 9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "bPChwlLe_BUq" }, "outputs": [], "source": [ "from ablator import ModelConfig, OptimizerConfig, TrainConfig, ParallelConfig\n", "from ablator import ModelWrapper, ParallelTrainer, configclass\n", "from ablator.config.hpo import SearchSpace\n", "\n", "import torch\n", "import torch.nn as nn\n", "import torchvision\n", "import torchvision.transforms as transforms\n", "\n", "import shutil\n", "from sklearn.metrics import accuracy_score" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Launch the parallel experiment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configure the experiment" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "id": "2u5Ovi0-YrtJ" }, "source": [ "We will follow the same steps as in the tutorial on [Prototyping models](./Prototyping-models.ipynb) to configure the experiment. Here's a summary of how we will configure it:\n", "\n", "- **Model Configuration**: defines hyperparameters for the number of filters and activation function.\n", "\n", "- **Optimizer Configuration**: adam (lr = 0.001).\n", "\n", "- **Train Configuration**: `batch_size = 32`, `epochs = 10`.\n", "\n", "- **Runing Configuration**:\n", " - GPU as hardware, a random seed for the experiment.\n", " - We let the experiment runs HPO for `total_trials = 20` trials, allowing `concurrent_trials = 2` trials to run in parallel.\n", " - We also use a search space for the model and the optimizer.\n", " - Use validation loss as the metric to optimize, in specific, we want to minimize this (`{\"val_loss\": \"min\"}`).\n", "\n", "