{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "white-electron",
   "metadata": {},
   "source": [
    "## Connect and Manage an Exist Experiment"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "recent-italic",
   "metadata": {},
   "source": [
    "### 1. Connect Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "statistical-repair",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-05 12:18:28] Connect to port 8080 success, experiment id is DH8pVfXc, status is RUNNING.\n"
     ]
    }
   ],
   "source": [
    "from nni.experiment import Experiment\n",
    "experiment = Experiment.connect(8080)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "defensive-scratch",
   "metadata": {},
   "source": [
    "### 2. Experiment View & Control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "independent-touch",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 'DH8pVfXc',\n",
       " 'revision': 4,\n",
       " 'execDuration': 10,\n",
       " 'logDir': '/home/ningshang/nni-experiments/DH8pVfXc',\n",
       " 'nextSequenceId': 1,\n",
       " 'params': {'authorName': 'default',\n",
       "  'experimentName': 'example_sklearn-classification',\n",
       "  'trialConcurrency': 1,\n",
       "  'maxExecDuration': 3600,\n",
       "  'maxTrialNum': 100,\n",
       "  'searchSpace': '{\"C\": {\"_type\": \"uniform\", \"_value\": [0.1, 1]}, \"kernel\": {\"_type\": \"choice\", \"_value\": [\"linear\", \"rbf\", \"poly\", \"sigmoid\"]}, \"degree\": {\"_type\": \"choice\", \"_value\": [1, 2, 3, 4]}, \"gamma\": {\"_type\": \"uniform\", \"_value\": [0.01, 0.1]}, \"coef0\": {\"_type\": \"uniform\", \"_value\": [0.01, 0.1]}}',\n",
       "  'trainingServicePlatform': 'local',\n",
       "  'tuner': {'builtinTunerName': 'TPE',\n",
       "   'classArgs': {'optimize_mode': 'maximize'},\n",
       "   'checkpointDir': '/home/ningshang/nni-experiments/DH8pVfXc/checkpoint'},\n",
       "  'versionCheck': True,\n",
       "  'clusterMetaData': [{'key': 'trial_config',\n",
       "    'value': {'command': 'python3 main.py',\n",
       "     'codeDir': '/home/ningshang/nni/examples/trials/sklearn/classification/.',\n",
       "     'gpuNum': 0}}]},\n",
       " 'startTime': 1614946699989}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_experiment_profile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "printable-bookmark",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-05 12:18:32] (root) Successfully update maxTrialNum.\n"
     ]
    }
   ],
   "source": [
    "experiment.update_max_trial_number(200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "marine-serial",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 'DH8pVfXc',\n",
       " 'revision': 5,\n",
       " 'execDuration': 14,\n",
       " 'logDir': '/home/ningshang/nni-experiments/DH8pVfXc',\n",
       " 'nextSequenceId': 1,\n",
       " 'params': {'authorName': 'default',\n",
       "  'experimentName': 'example_sklearn-classification',\n",
       "  'trialConcurrency': 1,\n",
       "  'maxExecDuration': 3600,\n",
       "  'maxTrialNum': 200,\n",
       "  'searchSpace': '{\"C\": {\"_type\": \"uniform\", \"_value\": [0.1, 1]}, \"kernel\": {\"_type\": \"choice\", \"_value\": [\"linear\", \"rbf\", \"poly\", \"sigmoid\"]}, \"degree\": {\"_type\": \"choice\", \"_value\": [1, 2, 3, 4]}, \"gamma\": {\"_type\": \"uniform\", \"_value\": [0.01, 0.1]}, \"coef0\": {\"_type\": \"uniform\", \"_value\": [0.01, 0.1]}}',\n",
       "  'trainingServicePlatform': 'local',\n",
       "  'tuner': {'builtinTunerName': 'TPE',\n",
       "   'classArgs': {'optimize_mode': 'maximize'},\n",
       "   'checkpointDir': '/home/ningshang/nni-experiments/DH8pVfXc/checkpoint'},\n",
       "  'versionCheck': True,\n",
       "  'clusterMetaData': [{'key': 'trial_config',\n",
       "    'value': {'command': 'python3 main.py',\n",
       "     'codeDir': '/home/ningshang/nni/examples/trials/sklearn/classification/.',\n",
       "     'gpuNum': 0}}]},\n",
       " 'startTime': 1614946699989}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "experiment.get_experiment_profile()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "opened-lounge",
   "metadata": {},
   "source": [
    "### 3. Stop Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "emotional-machinery",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2021-03-05 12:18:36] Stopping experiment, please wait...\n",
      "[2021-03-05 12:18:38] 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
}
