{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "technological-script",
   "metadata": {},
   "source": [
    "## 启动并管理一个新的 Experiment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reported-somerset",
   "metadata": {},
   "source": [
    "### 1. 定义搜索空间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "potential-williams",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_space = {\n",
    "    \"C\": {\"_type\":\"quniform\",\"_value\":[0.1, 1, 0.1]},\n",
    "    \"kernel\": {\"_type\":\"choice\",\"_value\":[\"linear\", \"rbf\", \"poly\", \"sigmoid\"]},\n",
    "    \"degree\": {\"_type\":\"choice\",\"_value\":[1, 2, 3, 4]},\n",
    "    \"gamma\": {\"_type\":\"quniform\",\"_value\":[0.01, 0.1, 0.01]},\n",
    "    \"coef0\": {\"_type\":\"quniform\",\"_value\":[0.01, 0.1, 0.01]}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greek-archive",
   "metadata": {},
   "source": [
    "### 2. 配置 Experiment "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fiscal-expansion",
   "metadata": {},
   "outputs": [],
   "source": [
    "from nni.experiment import Experiment\n",
    "experiment = Experiment('local')\n",
    "experiment.config.experiment_name = 'Example'\n",
    "experiment.config.trial_concurrency = 2\n",
    "experiment.config.max_trial_number = 10\n",
    "experiment.config.search_space = search_space\n",
    "experiment.config.trial_command = 'python3 main.py'\n",
    "experiment.config.trial_code_directory = './'\n",
    "experiment.config.tuner.name = 'TPE'\n",
    "experiment.config.tuner.class_args['optimize_mode'] = 'maximize'\n",
    "experiment.config.training_service.use_active_gpu = True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "received-tattoo",
   "metadata": {},
   "source": [
    "### 3. 启动 Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "pleasant-patent",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-05 12:12:19] Creating experiment, Experiment ID: wdt0le3v\n",
      "[2021-03-05 12:12:19] Starting web server...\n",
      "[2021-03-05 12:12:20] Setting up...\n",
      "[2021-03-05 12:12:20] Web UI URLs: http://127.0.0.1:8080 http://10.0.1.5:8080 http://172.17.0.1:8080\n"
     ]
    }
   ],
   "source": [
    "experiment.start(8080)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "miniature-prison",
   "metadata": {},
   "source": [
    "### 4. Experiment 查看和管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "animated-english",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'RUNNING'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "alpha-ottawa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[TrialResult(parameter={'C': 0.30000000000000004, 'kernel': 'linear', 'degree': 3, 'gamma': 0.03, 'coef0': 0.07}, value=0.9888888888888889, trialJobId='VLqU9'),\n",
       " TrialResult(parameter={'C': 0.5, 'kernel': 'sigmoid', 'degree': 1, 'gamma': 0.03, 'coef0': 0.07}, value=0.8888888888888888, trialJobId='DLo6r')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.export_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "unique-rendering",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'DLo6r': [TrialMetricData(timestamp=1614946351592, trialJobId='DLo6r', parameterId='1', type='FINAL', sequence=0, data=0.8888888888888888)],\n",
       " 'VLqU9': [TrialMetricData(timestamp=1614946351607, trialJobId='VLqU9', parameterId='0', type='FINAL', sequence=0, data=0.9888888888888889)]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_job_metrics()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welsh-difference",
   "metadata": {},
   "source": [
    "### 5. 停止 Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "technological-cleanup",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-05 12:12:40] Stopping experiment, please wait...\n",
      "[2021-03-05 12:12:42] Experiment stopped\n"
     ]
    }
   ],
   "source": [
    "experiment.stop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nni-dev",
   "language": "python",
   "name": "nni-dev"
  },
  "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.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}