{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "papermill": {
     "duration": 0.011744,
     "end_time": "2020-09-10T07:41:17.891158",
     "exception": false,
     "start_time": "2020-09-10T07:41:17.879414",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# MLP Clustering Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T07:41:17.925061Z",
     "iopub.status.busy": "2020-09-10T07:41:17.923712Z",
     "iopub.status.idle": "2020-09-10T07:41:21.918562Z",
     "shell.execute_reply": "2020-09-10T07:41:21.916960Z"
    },
    "papermill": {
     "duration": 4.013729,
     "end_time": "2020-09-10T07:41:21.919015",
     "exception": false,
     "start_time": "2020-09-10T07:41:17.905286",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "import matplotlib.pylab as plt\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "from IPython import display\n",
    "\n",
    "from src.visualization import draw_mlp_clustering_report, run_double_spectral_cluster\n",
    "from src.utils import get_weights_paths, build_clustering_results\n",
    "from src.experiment_tagging import get_model_path, MODEL_TAG_LOOKUP\n",
    "from src.spectral_cluster_model import SHUFFLE_METHODS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T07:41:21.945754Z",
     "iopub.status.busy": "2020-09-10T07:41:21.944608Z",
     "iopub.status.idle": "2020-09-10T07:41:21.961062Z",
     "shell.execute_reply": "2020-09-10T07:41:21.962300Z"
    },
    "papermill": {
     "duration": 0.032974,
     "end_time": "2020-09-10T07:41:21.962673",
     "exception": false,
     "start_time": "2020-09-10T07:41:21.929699",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "clustering_results = {}\n",
    "\n",
    "model_tags = ('MNIST', 'MNIST+DROPOUT', 'MNIST+L1REG', 'MNIST+MOD-INIT',\n",
    "              'HALVES-SAME-MNIST', 'HALVES-MNIST',\n",
    "              'FASHION', 'FASHION+DROPOUT', 'FASHION+L1REG', 'FASHION+MOD-INIT',\n",
    "              'HALVES-SAME-FASHION', 'HALVES-FASHION',\n",
    "              'CIFAR10', 'CIFAR10+DROPOUT', 'CIFAR10+L1REG', 'CIFAR10+MOD-INIT',\n",
    "              'HALVES-SAME-CIFAR10', 'HALVES-CIFAR10')\n",
    "mlp_model_paths = {model_tag: get_model_path(model_tag) for model_tag in model_tags}\n",
    "\n",
    "n_clust = 12\n",
    "n_samples = 50\n",
    "n_workers = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T07:41:21.990951Z",
     "iopub.status.busy": "2020-09-10T07:41:21.990104Z",
     "iopub.status.idle": "2020-09-10T11:14:31.907066Z",
     "shell.execute_reply": "2020-09-10T11:14:31.907760Z"
    },
    "papermill": {
     "duration": 12789.934434,
     "end_time": "2020-09-10T11:14:31.908091",
     "exception": false,
     "start_time": "2020-09-10T07:41:21.973657",
     "status": "completed"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "scrolled": false,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|          | 0/18 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  6%|▌         | 1/18 [09:28<2:41:10, 568.86s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 11%|█         | 2/18 [18:55<2:31:29, 568.08s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 17%|█▋        | 3/18 [25:27<2:08:50, 515.39s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 22%|██▏       | 4/18 [34:49<2:03:30, 529.32s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 28%|██▊       | 5/18 [47:09<2:08:24, 592.68s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 33%|███▎      | 6/18 [59:50<2:08:36, 643.03s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 39%|███▉      | 7/18 [1:12:53<2:05:34, 684.98s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 44%|████▍     | 8/18 [1:25:52<1:58:52, 713.22s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 50%|█████     | 9/18 [1:36:12<1:42:46, 685.21s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 56%|█████▌    | 10/18 [1:48:15<1:32:52, 696.60s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 61%|██████    | 11/18 [2:00:00<1:21:34, 699.17s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 67%|██████▋   | 12/18 [2:11:55<1:10:23, 703.93s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 72%|███████▏  | 13/18 [2:23:48<58:52, 706.58s/it]  "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 78%|███████▊  | 14/18 [2:37:27<49:20, 740.20s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 83%|████████▎ | 15/18 [2:46:11<33:46, 675.36s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 89%|████████▉ | 16/18 [2:58:44<23:17, 698.81s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 94%|█████████▍| 17/18 [3:13:13<12:29, 749.97s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "100%|██████████| 18/18 [3:31:08<00:00, 847.39s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "100%|██████████| 18/18 [3:31:08<00:00, 703.82s/it]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 795592 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 873436 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 697488 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 987620 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 834788 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 857632 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 842016 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 877864 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 787972 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 1018160 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 772784 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 751492 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 807296 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 900384 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 681828 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 943336 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 859908 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 865184 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for model_name, path in tqdm(mlp_model_paths.items()):\n",
    "    \n",
    "    clustering_results[model_name] = run_double_spectral_cluster(path, n_clusters=n_clust,\n",
    "                                                                 n_samples=n_samples, n_workers=n_workers)\n",
    "\n",
    "    draw_mlp_clustering_report(path, clustering_results[model_name], n_cluster=n_clust, title=model_name)\n"
   ]
  }
 ],
 "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.7.5"
  },
  "papermill": {
   "duration": 12800.754745,
   "end_time": "2020-09-10T11:14:37.579117",
   "environment_variables": {},
   "exception": null,
   "input_path": "./notebooks/mlp-clustering.ipynb",
   "output_path": "./notebooks/mlp-clustering.ipynb",
   "parameters": {},
   "start_time": "2020-09-10T07:41:16.824372",
   "version": "1.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}