{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "papermill": {
     "duration": 0.011359,
     "end_time": "2020-09-10T11:14:40.146848",
     "exception": false,
     "start_time": "2020-09-10T11:14:40.135489",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# CNN Clustering Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T11:14:40.167661Z",
     "iopub.status.busy": "2020-09-10T11:14:40.166935Z",
     "iopub.status.idle": "2020-09-10T11:14:45.642932Z",
     "shell.execute_reply": "2020-09-10T11:14:45.641246Z"
    },
    "papermill": {
     "duration": 5.489865,
     "end_time": "2020-09-10T11:14:45.643417",
     "exception": false,
     "start_time": "2020-09-10T11:14:40.153552",
     "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 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": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T11:14:45.676838Z",
     "iopub.status.busy": "2020-09-10T11:14:45.676155Z",
     "iopub.status.idle": "2020-09-10T11:14:45.688164Z",
     "shell.execute_reply": "2020-09-10T11:14:45.688737Z"
    },
    "papermill": {
     "duration": 0.03,
     "end_time": "2020-09-10T11:14:45.688971",
     "exception": false,
     "start_time": "2020-09-10T11:14:45.658971",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "clustering_results_l1 = {}\n",
    "clustering_results_l2 = {}\n",
    "\n",
    "model_tags = ('CNN-MNIST', 'CNN-MNIST+DROPOUT', 'CNN-MNIST+L1REG', 'CNN-MNIST+MOD-INIT',\n",
    "              'CNN-STACKED-SAME-MNIST', 'CNN-STACKED-MNIST',\n",
    "              'CNN-FASHION', 'CNN-FASHION+DROPOUT', 'CNN-FASHION+L1REG', 'CNN-FASHION+MOD-INIT',\n",
    "              'CNN-STACKED-SAME-FASHION', 'CNN-STACKED-FASHION',\n",
    "              'CNN-CIFAR10', 'CNN-CIFAR10+DROPOUT', 'CNN-CIFAR10+L1REG', 'CNN-CIFAR10+MOD-INIT',\n",
    "              'CNN-STACKED-SAME-CIFAR10', 'CNN-STACKED-CIFAR10')\n",
    "model_tags = ('CNN-STACKED-FASHION',)\n",
    "model_paths = {model_tag: get_model_path(model_tag, filter_='all')[-5:] for model_tag in model_tags}\n",
    "\n",
    "n_clust = 12\n",
    "n_samples = 50\n",
    "n_workers = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-10T11:14:45.710437Z",
     "iopub.status.busy": "2020-09-10T11:14:45.709146Z",
     "iopub.status.idle": "2020-09-10T11:16:17.358345Z",
     "shell.execute_reply": "2020-09-10T11:16:17.358878Z"
    },
    "papermill": {
     "duration": 91.662893,
     "end_time": "2020-09-10T11:16:17.359320",
     "exception": false,
     "start_time": "2020-09-10T11:14:45.696427",
     "status": "completed"
    },
    "scrolled": false,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1/1 [01:30<00:00, 90.93s/it]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 101120 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 100924 characters]\n",
      "text/plain": [
       "<Figure size 1440x2160 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# L1 filter norm\n",
    "\n",
    "for model_name, paths in tqdm(model_paths.items()):\n",
    "    \n",
    "    for path in paths:\n",
    "    \n",
    "        clustering_results_l1[model_name] = run_double_spectral_cluster(path, n_clusters=n_clust,\n",
    "                                                                        n_samples=n_samples, \n",
    "                                                                        n_workers=n_workers,\n",
    "                                                                        eigen_solver='arpack')\n",
    "        draw_mlp_clustering_report(path, clustering_results_l1[model_name],\n",
    "                                   n_cluster=n_clust, title=model_name)\n"
   ]
  },
  {
   "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.7.5"
  },
  "papermill": {
   "duration": 192.425823,
   "end_time": "2020-09-10T11:17:51.443902",
   "environment_variables": {},
   "exception": null,
   "input_path": "./notebooks/cnn-clustering.ipynb",
   "output_path": "./notebooks/cnn-clustering.ipynb",
   "parameters": {},
   "start_time": "2020-09-10T11:14:39.018079",
   "version": "1.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
