{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trained Model Visualization\n",
    "\n",
    "Run this notebook to visualize models trained by [Training.ipynb](Training.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Please first select the **language** for which you want to visualize the trained library embeddings. More configuration options are available in the config section below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:08.283447Z",
     "start_time": "2019-03-08T15:41:08.279699Z"
    }
   },
   "outputs": [],
   "source": [
    "language = 'javascript'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:07.301955Z",
     "start_time": "2019-03-08T15:41:06.970983Z"
    }
   },
   "outputs": [],
   "source": [
    "import gc\n",
    "import gzip\n",
    "import collections\n",
    "import math\n",
    "import os\n",
    "import urllib.request\n",
    "import zipfile\n",
    "from pathlib import Path\n",
    "import json\n",
    "import scipy\n",
    "import re\n",
    "import sys\n",
    "\n",
    "import numpy as np\n",
    "from sklearn.manifold import TSNE as tsne\n",
    "\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CONFIGURATION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:08.283447Z",
     "start_time": "2019-03-08T15:41:08.279699Z"
    }
   },
   "outputs": [],
   "source": [
    "rootDir = '../datasets/' + language + '/models/'\n",
    "dim = 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LOAD DATA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Update parameter language to visualize a different model. Leave the rest untouched."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:16.179526Z",
     "start_time": "2019-03-08T15:41:08.497222Z"
    }
   },
   "outputs": [],
   "source": [
    "filename = rootDir + 'vectorsHD_dim{}.json.gz'.format(dim)\n",
    "with gzip.open(filename, 'r') as f:\n",
    "    d = json.loads(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:16.198157Z",
     "start_time": "2019-03-08T15:41:16.181117Z"
    }
   },
   "outputs": [],
   "source": [
    "x = [item[1] for item in d.items()]\n",
    "y = [item[0] for item in d.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export to JSON file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:16.209064Z",
     "start_time": "2019-03-08T15:41:16.199758Z"
    }
   },
   "outputs": [],
   "source": [
    "def to_json(vectors, labels, data_dict, output_file):\n",
    "    dat = {}\n",
    "    max_x = np.amax(vectors, axis=0)[0].astype(\"float\")\n",
    "    max_y = np.amax(vectors, axis=0)[1].astype(\"float\")\n",
    "    dat[\"dimensions\"] = {\n",
    "        \"x\": [-max_x, max_x],\n",
    "        \"y\": [-max_y, max_y]\n",
    "    }\n",
    "    if vectors.shape[1] > 2:\n",
    "        max_z = np.amax(vectors, axis=0)[2].astype(\"float\")\n",
    "        dat[\"dimensions\"][\"z\"] = [-max_z, max_z]\n",
    "    dat[\"labels\"] = labels\n",
    "    dat[\"data\"] = vectors.astype(\"float\").tolist()\n",
    "    with open(output_file, 'w') as f:\n",
    "        json.dump(dat, f, indent=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize using t-SNE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T15:41:16.219553Z",
     "start_time": "2019-03-08T15:41:16.210141Z"
    }
   },
   "outputs": [],
   "source": [
    "perplexity = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T20:08:59.969079Z",
     "start_time": "2019-03-08T15:41:17.171194Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[t-SNE] Computing 31 nearest neighbors...\n",
      "[t-SNE] Indexed 9303 samples in 0.020s...\n",
      "[t-SNE] Computed neighbors for 9303 samples in 11.226s...\n",
      "[t-SNE] Computed conditional probabilities for sample 1000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 2000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 3000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 4000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 5000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 6000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 7000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 8000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 9000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 9303 / 9303\n",
      "[t-SNE] Mean sigma: 0.143119\n",
      "[t-SNE] Computed conditional probabilities in 0.114s\n",
      "[t-SNE] Iteration 50: error = 107.7475052, gradient norm = 0.0222344 (50 iterations in 4.231s)\n",
      "[t-SNE] Iteration 100: error = 95.4837570, gradient norm = 0.0210705 (50 iterations in 2.850s)\n",
      "[t-SNE] Iteration 150: error = 92.4931183, gradient norm = 0.0298389 (50 iterations in 2.668s)\n",
      "[t-SNE] Iteration 200: error = 91.5808334, gradient norm = 0.0301400 (50 iterations in 2.381s)\n",
      "[t-SNE] Iteration 250: error = 91.0968475, gradient norm = 0.0129199 (50 iterations in 2.500s)\n",
      "[t-SNE] KL divergence after 250 iterations with early exaggeration: 91.096848\n",
      "[t-SNE] Iteration 300: error = 3.4705703, gradient norm = 0.0013836 (50 iterations in 2.048s)\n",
      "[t-SNE] Iteration 350: error = 2.8273294, gradient norm = 0.0006482 (50 iterations in 1.947s)\n",
      "[t-SNE] Iteration 400: error = 2.5126038, gradient norm = 0.0004066 (50 iterations in 1.921s)\n",
      "[t-SNE] Iteration 450: error = 2.3229363, gradient norm = 0.0002951 (50 iterations in 1.904s)\n",
      "[t-SNE] Iteration 500: error = 2.1973619, gradient norm = 0.0002334 (50 iterations in 1.929s)\n",
      "[t-SNE] Iteration 550: error = 2.1084485, gradient norm = 0.0001907 (50 iterations in 1.982s)\n",
      "[t-SNE] Iteration 600: error = 2.0424447, gradient norm = 0.0001628 (50 iterations in 2.053s)\n",
      "[t-SNE] Iteration 650: error = 1.9912609, gradient norm = 0.0001420 (50 iterations in 2.039s)\n",
      "[t-SNE] Iteration 700: error = 1.9504861, gradient norm = 0.0001263 (50 iterations in 2.036s)\n",
      "[t-SNE] Iteration 750: error = 1.9173018, gradient norm = 0.0001181 (50 iterations in 2.038s)\n",
      "[t-SNE] Iteration 800: error = 1.8900375, gradient norm = 0.0001031 (50 iterations in 2.040s)\n",
      "[t-SNE] Iteration 850: error = 1.8673825, gradient norm = 0.0000969 (50 iterations in 2.030s)\n",
      "[t-SNE] Iteration 900: error = 1.8486816, gradient norm = 0.0000887 (50 iterations in 2.034s)\n",
      "[t-SNE] Iteration 950: error = 1.8329999, gradient norm = 0.0000838 (50 iterations in 2.000s)\n",
      "[t-SNE] Iteration 1000: error = 1.8198990, gradient norm = 0.0000798 (50 iterations in 1.934s)\n",
      "[t-SNE] Iteration 1050: error = 1.8090582, gradient norm = 0.0000789 (50 iterations in 1.930s)\n",
      "[t-SNE] Iteration 1100: error = 1.7999498, gradient norm = 0.0000734 (50 iterations in 1.941s)\n",
      "[t-SNE] Iteration 1150: error = 1.7918466, gradient norm = 0.0000724 (50 iterations in 1.927s)\n",
      "[t-SNE] Iteration 1200: error = 1.7845335, gradient norm = 0.0000694 (50 iterations in 1.981s)\n",
      "[t-SNE] Iteration 1250: error = 1.7779895, gradient norm = 0.0000650 (50 iterations in 2.072s)\n",
      "[t-SNE] Iteration 1300: error = 1.7717209, gradient norm = 0.0000632 (50 iterations in 2.040s)\n",
      "[t-SNE] Iteration 1350: error = 1.7657207, gradient norm = 0.0000620 (50 iterations in 2.024s)\n",
      "[t-SNE] Iteration 1400: error = 1.7602031, gradient norm = 0.0000610 (50 iterations in 2.027s)\n",
      "[t-SNE] Iteration 1450: error = 1.7550756, gradient norm = 0.0000607 (50 iterations in 2.029s)\n",
      "[t-SNE] Iteration 1500: error = 1.7505226, gradient norm = 0.0000582 (50 iterations in 2.016s)\n",
      "[t-SNE] Iteration 1550: error = 1.7461953, gradient norm = 0.0000560 (50 iterations in 2.049s)\n",
      "[t-SNE] Iteration 1600: error = 1.7422445, gradient norm = 0.0000572 (50 iterations in 2.082s)\n",
      "[t-SNE] Iteration 1650: error = 1.7385638, gradient norm = 0.0000558 (50 iterations in 1.968s)\n",
      "[t-SNE] Iteration 1700: error = 1.7350513, gradient norm = 0.0000562 (50 iterations in 1.945s)\n",
      "[t-SNE] Iteration 1750: error = 1.7319361, gradient norm = 0.0000544 (50 iterations in 1.937s)\n",
      "[t-SNE] Iteration 1800: error = 1.7290660, gradient norm = 0.0000568 (50 iterations in 1.962s)\n",
      "[t-SNE] Iteration 1850: error = 1.7265776, gradient norm = 0.0000534 (50 iterations in 1.957s)\n",
      "[t-SNE] Iteration 1900: error = 1.7241763, gradient norm = 0.0000507 (50 iterations in 1.968s)\n",
      "[t-SNE] Iteration 1950: error = 1.7218366, gradient norm = 0.0000508 (50 iterations in 1.972s)\n",
      "[t-SNE] Iteration 2000: error = 1.7194295, gradient norm = 0.0000552 (50 iterations in 1.967s)\n",
      "[t-SNE] Iteration 2050: error = 1.7173176, gradient norm = 0.0000507 (50 iterations in 1.939s)\n",
      "[t-SNE] Iteration 2100: error = 1.7153486, gradient norm = 0.0000493 (50 iterations in 2.047s)\n",
      "[t-SNE] Iteration 2150: error = 1.7134378, gradient norm = 0.0000503 (50 iterations in 2.096s)\n",
      "[t-SNE] Iteration 2200: error = 1.7117368, gradient norm = 0.0000498 (50 iterations in 2.059s)\n",
      "[t-SNE] Iteration 2250: error = 1.7099743, gradient norm = 0.0000472 (50 iterations in 2.058s)\n",
      "[t-SNE] Iteration 2300: error = 1.7082273, gradient norm = 0.0000456 (50 iterations in 2.093s)\n",
      "[t-SNE] Iteration 2350: error = 1.7062901, gradient norm = 0.0000459 (50 iterations in 2.109s)\n",
      "[t-SNE] Iteration 2400: error = 1.7043762, gradient norm = 0.0000453 (50 iterations in 2.064s)\n",
      "[t-SNE] Iteration 2450: error = 1.7025768, gradient norm = 0.0000452 (50 iterations in 2.062s)\n",
      "[t-SNE] Iteration 2500: error = 1.7008382, gradient norm = 0.0000454 (50 iterations in 2.059s)\n",
      "[t-SNE] Iteration 2550: error = 1.6993433, gradient norm = 0.0000471 (50 iterations in 2.050s)\n",
      "[t-SNE] Iteration 2600: error = 1.6979427, gradient norm = 0.0000438 (50 iterations in 2.006s)\n",
      "[t-SNE] Iteration 2650: error = 1.6966156, gradient norm = 0.0000432 (50 iterations in 2.073s)\n",
      "[t-SNE] Iteration 2700: error = 1.6953560, gradient norm = 0.0000428 (50 iterations in 2.094s)\n",
      "[t-SNE] Iteration 2750: error = 1.6939144, gradient norm = 0.0000426 (50 iterations in 2.109s)\n",
      "[t-SNE] Iteration 2800: error = 1.6926644, gradient norm = 0.0000448 (50 iterations in 2.109s)\n",
      "[t-SNE] Iteration 2850: error = 1.6915461, gradient norm = 0.0000393 (50 iterations in 2.062s)\n",
      "[t-SNE] Iteration 2900: error = 1.6902906, gradient norm = 0.0000393 (50 iterations in 2.074s)\n",
      "[t-SNE] Iteration 2950: error = 1.6889079, gradient norm = 0.0000388 (50 iterations in 2.077s)\n",
      "[t-SNE] Iteration 3000: error = 1.6874970, gradient norm = 0.0000400 (50 iterations in 2.079s)\n",
      "[t-SNE] KL divergence after 3000 iterations: 1.687497\n"
     ]
    }
   ],
   "source": [
    "model = tsne(n_components=2, random_state=42, perplexity=perplexity, n_iter=3000, verbose=3)\n",
    "tvis_data = model.fit_transform(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-08T20:09:01.457610Z",
     "start_time": "2019-03-08T20:08:59.970376Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'../datasets/javascript/models/tsne-lib-p10-neg1-dim100.json'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = rootDir + \"tsne-lib-p{}-neg{}-dim{}.json\".format(perplexity, 1, dim)\n",
    "to_json(tvis_data, list(y), None, filename)\n",
    "filename"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x7fd873542780>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 165268 characters]\n",
      "text/plain": [
       "<Figure size 1152x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.figure(figsize=(16,10))\n",
    "plt.scatter(tvis_data[:, 0], tvis_data[:, 1], 20)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2019-03-08T15:37:55.532Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(rootDir + 'vectors2D_dim{}.json'.format(dim), 'w') as f:\n",
    "    vec2d = {y[i]: tvis_data[i].tolist() for i,_ in enumerate(y)}\n",
    "    json.dump(vec2d, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2019-02-07T18:03:24.616Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[t-SNE] Computing 31 nearest neighbors...\n",
      "[t-SNE] Indexed 9303 samples in 0.022s...\n",
      "[t-SNE] Computed neighbors for 9303 samples in 11.522s...\n",
      "[t-SNE] Computed conditional probabilities for sample 1000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 2000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 3000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 4000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 5000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 6000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 7000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 8000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 9000 / 9303\n",
      "[t-SNE] Computed conditional probabilities for sample 9303 / 9303\n",
      "[t-SNE] Mean sigma: 0.143119\n",
      "[t-SNE] Computed conditional probabilities in 0.120s\n",
      "[t-SNE] Iteration 50: error = 107.6567230, gradient norm = 0.0070863 (50 iterations in 35.621s)\n",
      "[t-SNE] Iteration 100: error = 94.2924805, gradient norm = 0.0195196 (50 iterations in 14.476s)\n",
      "[t-SNE] Iteration 150: error = 91.9057007, gradient norm = 0.0056610 (50 iterations in 13.415s)\n",
      "[t-SNE] Iteration 200: error = 91.3501205, gradient norm = 0.0031637 (50 iterations in 13.850s)\n",
      "[t-SNE] Iteration 250: error = 91.1188431, gradient norm = 0.0027662 (50 iterations in 13.203s)\n",
      "[t-SNE] KL divergence after 250 iterations with early exaggeration: 91.118843\n",
      "[t-SNE] Iteration 300: error = 3.1094999, gradient norm = 0.0009410 (50 iterations in 12.558s)\n",
      "[t-SNE] Iteration 350: error = 2.4586110, gradient norm = 0.0003429 (50 iterations in 12.524s)\n",
      "[t-SNE] Iteration 400: error = 2.1546137, gradient norm = 0.0001844 (50 iterations in 12.720s)\n",
      "[t-SNE] Iteration 450: error = 1.9791923, gradient norm = 0.0001163 (50 iterations in 13.230s)\n",
      "[t-SNE] Iteration 500: error = 1.8665340, gradient norm = 0.0000829 (50 iterations in 13.115s)\n",
      "[t-SNE] Iteration 550: error = 1.7896781, gradient norm = 0.0000653 (50 iterations in 13.159s)\n",
      "[t-SNE] Iteration 600: error = 1.7364216, gradient norm = 0.0000533 (50 iterations in 13.162s)\n",
      "[t-SNE] Iteration 650: error = 1.7000519, gradient norm = 0.0000492 (50 iterations in 13.161s)\n",
      "[t-SNE] Iteration 700: error = 1.6752447, gradient norm = 0.0000456 (50 iterations in 13.137s)\n",
      "[t-SNE] Iteration 750: error = 1.6581482, gradient norm = 0.0000448 (50 iterations in 13.144s)\n",
      "[t-SNE] Iteration 800: error = 1.6462100, gradient norm = 0.0000420 (50 iterations in 13.178s)\n",
      "[t-SNE] Iteration 850: error = 1.6369768, gradient norm = 0.0000388 (50 iterations in 13.346s)\n",
      "[t-SNE] Iteration 900: error = 1.6290200, gradient norm = 0.0000366 (50 iterations in 13.992s)\n",
      "[t-SNE] Iteration 950: error = 1.6217809, gradient norm = 0.0000379 (50 iterations in 13.870s)\n",
      "[t-SNE] Iteration 1000: error = 1.6154275, gradient norm = 0.0000342 (50 iterations in 13.513s)\n",
      "[t-SNE] Iteration 1050: error = 1.6096733, gradient norm = 0.0000329 (50 iterations in 14.058s)\n",
      "[t-SNE] Iteration 1100: error = 1.6041507, gradient norm = 0.0000317 (50 iterations in 13.327s)\n",
      "[t-SNE] Iteration 1150: error = 1.5991559, gradient norm = 0.0000326 (50 iterations in 13.334s)\n",
      "[t-SNE] Iteration 1200: error = 1.5949341, gradient norm = 0.0000340 (50 iterations in 13.325s)\n",
      "[t-SNE] Iteration 1250: error = 1.5915806, gradient norm = 0.0000326 (50 iterations in 13.294s)\n",
      "[t-SNE] Iteration 1300: error = 1.5886773, gradient norm = 0.0000291 (50 iterations in 13.209s)\n",
      "[t-SNE] Iteration 1350: error = 1.5857329, gradient norm = 0.0000278 (50 iterations in 13.322s)\n",
      "[t-SNE] Iteration 1400: error = 1.5829086, gradient norm = 0.0000293 (50 iterations in 14.168s)\n",
      "[t-SNE] Iteration 1450: error = 1.5801417, gradient norm = 0.0000269 (50 iterations in 13.674s)\n",
      "[t-SNE] Iteration 1500: error = 1.5773402, gradient norm = 0.0000265 (50 iterations in 13.514s)\n",
      "[t-SNE] Iteration 1550: error = 1.5742844, gradient norm = 0.0000265 (50 iterations in 13.471s)\n",
      "[t-SNE] Iteration 1600: error = 1.5715859, gradient norm = 0.0000252 (50 iterations in 13.587s)\n",
      "[t-SNE] Iteration 1650: error = 1.5688660, gradient norm = 0.0000253 (50 iterations in 13.978s)\n",
      "[t-SNE] Iteration 1700: error = 1.5662684, gradient norm = 0.0000259 (50 iterations in 13.648s)\n",
      "[t-SNE] Iteration 1750: error = 1.5638725, gradient norm = 0.0000259 (50 iterations in 13.541s)\n",
      "[t-SNE] Iteration 1800: error = 1.5615935, gradient norm = 0.0000244 (50 iterations in 13.490s)\n",
      "[t-SNE] Iteration 1850: error = 1.5596457, gradient norm = 0.0000233 (50 iterations in 13.448s)\n",
      "[t-SNE] Iteration 1900: error = 1.5576208, gradient norm = 0.0000230 (50 iterations in 13.473s)\n",
      "[t-SNE] Iteration 1950: error = 1.5556624, gradient norm = 0.0000230 (50 iterations in 13.476s)\n",
      "[t-SNE] Iteration 2000: error = 1.5536836, gradient norm = 0.0000209 (50 iterations in 13.703s)\n",
      "[t-SNE] Iteration 2050: error = 1.5516764, gradient norm = 0.0000214 (50 iterations in 14.207s)\n",
      "[t-SNE] Iteration 2100: error = 1.5496585, gradient norm = 0.0000220 (50 iterations in 13.634s)\n",
      "[t-SNE] Iteration 2150: error = 1.5478342, gradient norm = 0.0000222 (50 iterations in 13.478s)\n",
      "[t-SNE] Iteration 2200: error = 1.5461465, gradient norm = 0.0000232 (50 iterations in 13.513s)\n",
      "[t-SNE] Iteration 2250: error = 1.5445511, gradient norm = 0.0000228 (50 iterations in 13.454s)\n",
      "[t-SNE] Iteration 2300: error = 1.5431828, gradient norm = 0.0000228 (50 iterations in 13.568s)\n",
      "[t-SNE] Iteration 2350: error = 1.5418226, gradient norm = 0.0000212 (50 iterations in 13.393s)\n",
      "[t-SNE] Iteration 2400: error = 1.5404736, gradient norm = 0.0000245 (50 iterations in 13.491s)\n",
      "[t-SNE] Iteration 2450: error = 1.5391823, gradient norm = 0.0000212 (50 iterations in 13.440s)\n",
      "[t-SNE] Iteration 2500: error = 1.5378101, gradient norm = 0.0000224 (50 iterations in 13.420s)\n",
      "[t-SNE] Iteration 2550: error = 1.5364201, gradient norm = 0.0000218 (50 iterations in 13.477s)\n",
      "[t-SNE] Iteration 2600: error = 1.5350839, gradient norm = 0.0000221 (50 iterations in 13.435s)\n",
      "[t-SNE] Iteration 2650: error = 1.5338449, gradient norm = 0.0000203 (50 iterations in 13.386s)\n",
      "[t-SNE] Iteration 2700: error = 1.5325999, gradient norm = 0.0000214 (50 iterations in 13.415s)\n",
      "[t-SNE] Iteration 2750: error = 1.5313745, gradient norm = 0.0000205 (50 iterations in 13.424s)\n",
      "[t-SNE] Iteration 2800: error = 1.5303091, gradient norm = 0.0000217 (50 iterations in 13.404s)\n",
      "[t-SNE] Iteration 2850: error = 1.5292397, gradient norm = 0.0000210 (50 iterations in 13.382s)\n",
      "[t-SNE] Iteration 2900: error = 1.5282142, gradient norm = 0.0000204 (50 iterations in 13.547s)\n",
      "[t-SNE] Iteration 2950: error = 1.5271881, gradient norm = 0.0000190 (50 iterations in 13.406s)\n",
      "[t-SNE] Iteration 3000: error = 1.5260144, gradient norm = 0.0000194 (50 iterations in 13.649s)\n",
      "[t-SNE] KL divergence after 3000 iterations: 1.526014\n"
     ]
    }
   ],
   "source": [
    "perplexity = 10\n",
    "model_3d = tsne(n_components=3, random_state=42, perplexity=perplexity, n_iter=3000, verbose=3)\n",
    "tvis_data_3d = model_3d.fit_transform(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2019-02-07T18:03:25.034Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'../datasets/javascript/models/3d_tsne-lib-p10-neg1-dim100.json'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = rootDir + '3d_' + \"tsne-lib-p{}-neg{}-dim{}.json\".format(perplexity, 1, dim)\n",
    "to_json(tvis_data_3d, list(y), None, filename)\n",
    "filename"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fd872419c50>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 224484 characters]\n",
      "text/plain": [
       "<Figure size 1152x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "fig = plt.figure(figsize=(16,10))\n",
    "ax = fig.add_subplot(111, projection='3d')\n",
    "ax.scatter(tvis_data_3d[:,0], tvis_data_3d[:,1], tvis_data_3d[:,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2019-02-07T18:03:25.289Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(rootDir + 'vectors3D_dim{}.json'.format(dim), 'w') as f:\n",
    "    vec3d = {y[i]: tvis_data_3d[i].tolist() for i,_ in enumerate(y)}\n",
    "    json.dump(vec3d, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
