{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The config dir path: /Users/xuanyidong/Desktop/AutoDL-Projects/configs\n"
     ]
    }
   ],
   "source": [
    "#####################################################\n",
    "# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.06 #\n",
    "#####################################################\n",
    "import os, sys, math\n",
    "import numpy as np\n",
    "from pathlib import Path\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import torch\n",
    "from xautodl.xmisc.scheduler_utils import CosineParamScheduler, MultiStepParamScheduler\n",
    "from xautodl.xmisc.scheduler_utils import LRMultiplier, WarmupParamScheduler\n",
    "\n",
    "__file__ = os.path.dirname(os.path.realpath(\"__file__\"))\n",
    "\n",
    "config_dir = (Path(__file__).parent / \"..\" / \"configs\").resolve()\n",
    "print(\"The config dir path: {:}\".format(config_dir))\n",
    "\n",
    "def draw(steps, lrs):\n",
    "    plt.close()\n",
    "    dpi, width, height = 200, 1400, 800\n",
    "    figsize = width / float(dpi), height / float(dpi)\n",
    "    fig = plt.figure(figsize=figsize)\n",
    "    ax = fig.add_subplot(111)\n",
    "\n",
    "    plt.plot(steps, lrs)\n",
    "    plt.title(\"Plot Cosine Decayed LR with Warmup\")\n",
    "    plt.xlabel(\"steps\")\n",
    "    plt.ylabel(\"learning rate\")\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22524 characters]\n",
      "text/plain": [
       "<Figure size 504x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "p = torch.nn.Parameter(torch.zeros(0))\n",
    "opt = torch.optim.SGD([p], lr=5)\n",
    "multiplier = WarmupParamScheduler(\n",
    "    CosineParamScheduler(0.1, 0.0001),\n",
    "    warmup_factor = 0.001,\n",
    "    warmup_length = 0.05,\n",
    "    warmup_method = 'linear'\n",
    ")\n",
    "total = 100\n",
    "scheduler = LRMultiplier(opt, multiplier, total)\n",
    "steps, lrs = [], []\n",
    "\n",
    "for _iter in range(total * 2):\n",
    "    p.sum().backward()\n",
    "    opt.step()\n",
    "    lrs.append(opt.param_groups[0][\"lr\"])\n",
    "    steps.append(_iter)\n",
    "\n",
    "    scheduler.step()\n",
    "draw(steps, lrs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
