{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Prototyping Models" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "[](https://colab.research.google.com/github/fostiropoulos/ablator/blob/v0.0.1-mp/docs/source/notebooks/Prototyping-models.ipynb)\n", "\n", "Let's say you have a novel idea for a model architecture and you want to run ablation study on it with `ablator`. Ablator simplifies the process of prototyping your model, allowing you to swiftly construct and evaluate your innovative concept. Once a prototype runs smoothly and behave as expected, you can scale it to a parallel ablation study of multiple trials with minimal code change.\n", "\n", "This chapter covers prototyping a model using Ablator. We will train a simple neural network model on the popular **Fashion-mnist** dataset.\n", "\n", "There are three main steps to run a prototype experiment in ablator:\n", "\n", "- Configure the prototype experiment.\n", "\n", "- Create model wrapper that defines boiler-plate code for training and evaluating models.\n", "\n", "- Create the prototype trainer and launch the experiment." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "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": {}, "outputs": [], "source": [ "from ablator import (ModelConfig, OptimizerConfig, TrainConfig, RunConfig,\n", " ModelWrapper, ProtoTrainer, configclass)\n", "\n", "import torch\n", "import torch.nn as nn\n", "import torchvision\n", "import torchvision.transforms as transforms\n", "\n", "from sklearn.metrics import f1_score, accuracy_score\n", "\n", "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Launch the prototype experiment" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Configure the experiment" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We will follow the same steps as in the previous tutorial on [Configuration Basics](./Configuration-Basics.ipynb) to configure the experiment:\n", "\n", "Here's a summary of how we will configure it:\n", "\n", "- **Model Configuration**: dimensions for the layers of the model.\n", "\n", "- **Optimizer Configuration**: adam (lr = 0.001).\n", "\n", "- **Train Configuration**: `batch_size` = 32, `epochs` = 20.\n", "\n", "- **Running Configuration**: CPU as hardware and a random seed for the experiment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Model configuration\n", "\n", "For the model configuration, we define hyperparameters `input_size`, `hidden_size`, and `num_classes` as [stateful](./Configuration-Basics.ipynb#Ablator-custom-data-types-for-stateful-experiment-design) integer config attributes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "CustomModelConfig(input_size=784, hidden_size=256, num_classes=10)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@configclass\n", "class CustomModelConfig(ModelConfig):\n", " input_size :int\n", " hidden_size :int \n", " num_classes :int\n", "\n", "model_config = CustomModelConfig(\n", " input_size = 28*28, # 28x28 image flattened\n", " hidden_size = 256, \n", " num_classes = 10\n", " )\n", "model_config" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the hyperparameters are defined as stateful, we must provide concrete values when initializing the `model_config` object.\n", "\n", "