{ "cells": [ { "cell_type": "code", "execution_count": 74, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T07:56:11.075394Z", "start_time": "2018-10-21T07:56:11.041609Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/hegder/anaconda3/lib/python3.7/site-packages/ipyparallel/client/client.py:459: RuntimeWarning: \n", " Controller appears to be listening on localhost, but not on this machine.\n", " If this is true, you should specify Client(...,sshserver='you@Gamma')\n", " or instruct your controller to listen on an external IP.\n", " RuntimeWarning)\n" ] }, { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ipyparallel as ipp\n", "\n", "rc = ipp.Client()\n", "dview = rc[:]\n", "\n", "rc.ids" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T07:56:20.877563Z", "start_time": "2018-10-21T07:56:14.818972Z" }, "attributes": { "classes": [], "id": "", "n": "413" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "Dataset has been loaded\n", "[stdout:0] Dataset has been loaded\n", "[stdout:1] Dataset has been loaded\n", "[stdout:2] Dataset has been loaded\n", "[stdout:3] Dataset has been loaded\n", "[stdout:4] Dataset has been loaded\n", "[stdout:5] Dataset has been loaded\n", "[stdout:6] Dataset has been loaded\n", "[stdout:7] Dataset has been loaded\n" ] } ], "source": [ "%%px --local\n", "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import numpy as np\n", "import mxnet as mx\n", "#import matplotlib.pyplot as plt\n", "import h5py\n", "from sklearn.model_selection import train_test_split\n", "# # # #import jtplot submodule from jupyterthemes\n", "# # # from jupyterthemes import jtplot\n", "# # # #currently installed theme will be used to\n", "# # # #set plot style if no arguments provided\n", "# # # jtplot.style()\n", "\n", "#now load this dataset \n", "h5f = h5py.File('./datasets/s8_sio2tio2_v2.h5','r')\n", "X = h5f['sizes'][:]\n", "Y = h5f['spectrum'][:]\n", "\n", "#create a train - test split of the dataset\n", "x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.4, random_state=42)\n", "# normalize inputs \n", "#x_test = (x_test - 50)/20 \n", "\n", "print(\"Dataset has been loaded\")\n", "\n", "\n", "\n", "# # Step1: Load the model in MXNet\n", "\n", "# # # Use the same prefix and epoch parameters we used in save_mxnet_model API.\n", "sym, arg_params, aux_params = mx.model.load_checkpoint(prefix='./models/my_mod_convprel', epoch=0)\n", "\n", "# We use the data_names and data_shapes returned by save_mxnet_model API.\n", "mod = mx.mod.Module(symbol=sym, \n", " data_names=['/first_input6'], \n", " context=mx.gpu(), \n", " label_names=None)\n", "mod.bind(for_training=False, \n", " data_shapes=[('/first_input6', (1,8))], \n", " label_shapes=mod._label_shapes)\n", "mod.set_params(arg_params, aux_params, allow_missing=True) \n", "\n", "\n", "#resnet - my_mod_bet input_21\n", "#fullycon - my_mod_fullycon first_input2\n", "#conv1d - my_mod_conv1d - first_input4\n", "#convprel - my_mod_convprel - first_input6" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T07:56:42.662949Z", "start_time": "2018-10-21T07:56:42.561201Z" }, "code_folding": [], "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:0] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:1] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:2] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:3] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:4] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:5] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:6] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "[stdout:7] \n", "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "\n", "%%px --local\n", "%load_ext autoreload\n", "%autoreload 2\n", "\n", "\n", "#%%px --local\n", "import time\n", "import de2 as de\n", "import loss_defs as ld\n", "\n", "bnds = [(30, 70)]*8\n", "mats = np.array([3, 4, 3, 4, 3, 4, 3, 4])\n", "lams = np.linspace(300, 1200, 256)\n", "targ_spec = y_test[29]\n", "\n", "\n", "from sklearn.model_selection import ParameterGrid\n", "param_grid = {\n", " 'psize': [80, 160, 320], \n", " 'psnew': [20, 40],\n", " 'crossp': [0.4, 0.5, 0.6, 0.7, 0.8]\n", " }\n", "grid = ParameterGrid(param_grid) \n", "\n", "def dE_stat(psize,psnew,its_first,its_second,mut,crossp):\n", " return psize, psnew, crossp\n", "\n", "\n", "def de_stat(psize,psnew,its_first,its_second,mut,crossp):\n", " run_time_tot = []\n", " run_pmre = []\n", " reps = 20\n", " targ_spec2 = np.tile(targ_spec, (psize,1))\n", " for rep in range(reps):\n", "# print(rep)\n", " start = time.time()\n", " pop, f, b, hstry = de.de_g(\n", " fobj=ld.mxmod_arr_loss, \n", " bounds=bnds, \n", " mut=mut,\n", " crossp=crossp,\n", " popsize=psize, \n", " its=its_first, \n", " target=targ_spec2, \n", " mxmodel=mod) \n", " marg = int(psnew/5)\n", " pnew1 = pop[np.argsort(f)][:psnew-marg]\n", " pnew2 = pop[np.argsort(f)][psnew-marg:psnew]\n", " pnew = np.concatenate((pnew1, pnew2))\n", " b, c, hstry = de.de_c(\n", " fobj=ld.loss_func, \n", " bounds=bnds, \n", " pop=pnew, \n", " history=hstry, \n", " it_start=its_first, \n", " mut=mut,\n", " crossp=crossp,\n", " popsize=psnew, \n", " its=its_second, \n", " target=targ_spec, \n", " mats=mats, \n", " lams=lams)\n", " end = time.time()\n", " run_time_tot.append((end - start)/60.0)\n", " run_pmre.append(c)\n", " return np.mean(run_pmre), np.std(run_pmre), np.mean(run_time_tot)\n", " \n", " \n", "\n", "# pm, pstd, rmd = de_stat(psize=80,psnew=10,its_first=500,its_second=100,mut=0.8,crossp=0.7)\n", "\n", "\n", "# pm, pstd, rmd\n", "\n", "\n", "\n", "# print(grid)\n", "# for mem in grid:\n", "# for key in mem:\n", "# print(key, mem[key])" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T07:56:47.477288Z", "start_time": "2018-10-21T07:56:47.446944Z" } }, "outputs": [], "source": [ "\n", "def ff(x):\n", " for pset in grid:\n", "# print(x,pset)\n", " pm, pstd, rmd = de_stat(psize=pset['psize'],\n", " psnew=pset['psnew'],\n", " its_first=500,\n", " its_second=300,\n", " mut=x,\n", " crossp=pset['crossp']\n", " )\n", " print(pm)\n", " print(pstd)\n", " print(rmd)\n", " \n", "\n", " \n", "#asyn = dview.map_async(ff,[0.5 ,0.6, 0.7, 0.8, 0.9, 1.0])\n", "#ff(0.5)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T14:43:52.453854Z", "start_time": "2018-10-21T14:43:52.430772Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'crossp': 0.4, 'psize': 80, 'psnew': 20}\n", "{'crossp': 0.4, 'psize': 80, 'psnew': 40}\n", "{'crossp': 0.4, 'psize': 160, 'psnew': 20}\n", "{'crossp': 0.4, 'psize': 160, 'psnew': 40}\n", "{'crossp': 0.4, 'psize': 320, 'psnew': 20}\n", "{'crossp': 0.4, 'psize': 320, 'psnew': 40}\n", "{'crossp': 0.5, 'psize': 80, 'psnew': 20}\n", "{'crossp': 0.5, 'psize': 80, 'psnew': 40}\n", "{'crossp': 0.5, 'psize': 160, 'psnew': 20}\n", "{'crossp': 0.5, 'psize': 160, 'psnew': 40}\n", "{'crossp': 0.5, 'psize': 320, 'psnew': 20}\n", "{'crossp': 0.5, 'psize': 320, 'psnew': 40}\n", "{'crossp': 0.6, 'psize': 80, 'psnew': 20}\n", "{'crossp': 0.6, 'psize': 80, 'psnew': 40}\n", "{'crossp': 0.6, 'psize': 160, 'psnew': 20}\n", "{'crossp': 0.6, 'psize': 160, 'psnew': 40}\n", "{'crossp': 0.6, 'psize': 320, 'psnew': 20}\n", "{'crossp': 0.6, 'psize': 320, 'psnew': 40}\n", "{'crossp': 0.7, 'psize': 80, 'psnew': 20}\n", "{'crossp': 0.7, 'psize': 80, 'psnew': 40}\n", "{'crossp': 0.7, 'psize': 160, 'psnew': 20}\n", "{'crossp': 0.7, 'psize': 160, 'psnew': 40}\n", "{'crossp': 0.7, 'psize': 320, 'psnew': 20}\n", "{'crossp': 0.7, 'psize': 320, 'psnew': 40}\n", "{'crossp': 0.8, 'psize': 80, 'psnew': 20}\n", "{'crossp': 0.8, 'psize': 80, 'psnew': 40}\n", "{'crossp': 0.8, 'psize': 160, 'psnew': 20}\n", "{'crossp': 0.8, 'psize': 160, 'psnew': 40}\n", "{'crossp': 0.8, 'psize': 320, 'psnew': 20}\n", "{'crossp': 0.8, 'psize': 320, 'psnew': 40}\n" ] }, { "data": { "text/plain": [ "['1.1301009177342074\\n0.4433545102972204\\n1.9973992935816451\\n1.1978118829893494\\n0.5433340634490728\\n3.9798421380917235\\n1.185659489902775\\n0.5229318402795373\\n2.0346258413791656\\n1.0203257728233819\\n0.4328800961143291\\n3.9803607704242068\\n0.8853474755214226\\n0.39025710533254293\\n2.121337319811185\\n0.8745737336598876\\n0.40638905130018776\\n4.080368780096372\\n1.136967865218111\\n0.7886459836783719\\n1.98240611632665\\n',\n", " '1.7131988937392013\\n0.6082128954795456\\n2.0491744035482404\\n1.428553914278536\\n0.44388601231540326\\n4.026420084039371\\n1.5048834539525133\\n0.6004320686610364\\n2.070323901772499\\n1.3652300738904153\\n0.5752924032686485\\n4.047638735373815\\n1.2851497406207808\\n0.4712458455064607\\n2.1543499658505123\\n1.288555356129053\\n0.5452378126342693\\n4.131543535788855\\n',\n", " '2.0202662132221905\\n0.5519051815259742\\n2.0411330630381896\\n1.9525635908247334\\n0.4583166928303958\\n4.0116366086403525\\n1.7012429992895874\\n0.5010234174726487\\n2.05347172955672\\n1.7794735270108073\\n0.5739565246000088\\n4.039100284576416\\n1.902040312708279\\n0.590235390140152\\n2.148336291710535\\n1.6673563120526818\\n0.4281516235207753\\n4.117947609623273\\n',\n", " '2.602639688174307\\n0.6152539758296448\\n2.0382266837358474\\n2.3995031384104224\\n0.6499501393566456\\n3.994401550889015\\n2.259091084659232\\n0.602963186643796\\n2.056499874989192\\n2.2681213876612434\\n0.4265081657964298\\n4.004326575398446\\n2.3096017439957017\\n0.5134323378774367\\n2.1406100873152414\\n2.1120005176863494\\n0.5716520001785659\\n4.098231108983358\\n2.155047865321666\\n0.7828864333850875\\n2.000000069737434\\n',\n", " '2.6307611743968344\\n0.5994124514860908\\n2.056917774875959\\n2.9493241444314537\\n0.6988332871974442\\n4.03848379433155\\n2.67712050359448\\n0.633608073041573\\n2.090317832430203\\n2.4557335895738275\\n0.619746104257869\\n4.082432220975559\\n2.4134195710515063\\n0.4391665495051974\\n2.1765918880701065\\n2.5009232355992004\\n0.7942216982459094\\n4.158314067324002\\n',\n", " '2.9633835944933495\\n0.4875300123238345\\n2.0318510516484585\\n3.1881074389096407\\n0.8280600156469394\\n4.002757206956545\\n2.857410062376154\\n0.6435796953353261\\n2.059861578543981\\n2.6423645770530033\\n0.6120867705147262\\n4.032538525462151\\n2.7043254624645554\\n0.6426567614916722\\n2.1510924446582793\\n2.6640555496411666\\n0.38322716764518094\\n4.115076087315877\\n']" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ " for pset in grid:\n", " print(pset)\n", "asyn.stdout" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T15:11:23.567612Z", "start_time": "2018-10-21T15:11:23.545535Z" } }, "outputs": [], "source": [ "\n", "\n", "outs=[]\n", "for out in asyn.stdout:\n", " outs.append(out.splitlines())\n", "\n", "outs2 = sum(outs, [])\n", "\n", "outs_f = np.array([float(o) for o in outs2])\n", "outs_f\n", "\n", "a =np.reshape(outs_f, (int(outs_f.size/3),3))\n", "pm = a[:,0]\n", "pstd = a[:,1]\n", "rmd = a[:,2]\n", "\n", "# vals = np.array([float(mem[:-1]) for mem in asyn.stdout])\n", "#asyn.stdout\n", "#asyn.stdout" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "ExecuteTime": { "end_time": "2018-10-21T15:11:27.651372Z", "start_time": "2018-10-21T15:11:27.630132Z" } }, "outputs": [ { "data": { "text/plain": [ "array([1.13010092, 1.19781188, 1.18565949, 1.02032577, 0.88534748,\n", " 0.87457373, 1.13696787, 1.71319889, 1.42855391, 1.50488345,\n", " 1.36523007, 1.28514974, 1.28855536, 0.94324173, 2.02026621,\n", " 1.95256359, 1.701243 , 1.77947353, 1.90204031, 1.66735631,\n", " 1.46799337, 2.60263969, 2.39950314, 2.25909108, 2.26812139,\n", " 2.30960174, 2.11200052, 2.15504787, 2.63076117, 2.94932414,\n", " 2.6771205 , 2.45573359, 2.41341957, 2.50092324, 2.37576157,\n", " 2.96338359, 3.18810744, 2.85741006, 2.64236458, 2.70432546,\n", " 2.66405555, 2.73309118])" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-20T13:32:36.565912Z", "start_time": "2018-10-20T13:32:21.309964Z" }, "attributes": { "classes": [], "id": "", "n": "414" }, "scrolled": true }, "outputs": [], "source": [ "import time\n", "import de2 as de\n", "import loss_defs as ld\n", "\n", "bnds = [(30, 70)]*8\n", "mats = np.array([3, 4, 3, 4, 3, 4, 3, 4])\n", "lams = np.linspace(300, 1200, 256)\n", "targ_spec = y_test[29]\n", "\n", "psize = 320\n", "targ_spec2 = np.tile(targ_spec, (psize,1))\n", "its_first = 500\n", "psnew = 20\n", "its_second = 300\n", "reps = 100\n", "\n", "\n", "run_hist = []\n", "run_time1 = []\n", "run_time2 = []\n", "run_time_tot = []\n", "run_pmre = []\n", "run_best = []\n", "\n", "for rep in range(reps):\n", " print(\"iteration \", rep)\n", " start = time.time()\n", " pop, f, b, hstry = de.de_g(fobj=ld.mxmod_arr_loss, bounds=bnds, popsize=psize, its=its_first, target=targ_spec2, mxmodel=mod) \n", " #pop, f, b, hstry = de2(fobj=mxmod_arr_loss, bounds=bnds, popsize=psize, its=its_first) \n", " end1 = time.time()\n", " marg = int(psnew/5)\n", " pnew1 = pop[np.argsort(f)][:psnew-marg]\n", " pnew2 = pop[np.argsort(f)][psnew-marg:psnew]\n", " pnew = np.concatenate((pnew1, pnew2))\n", " #b, c, hstry = de_stage2(fobj=loss_func, bounds=bnds, popint=pnew, history=hstry, itprev=its_first, popsize=psnew, its=its_second)\n", " # b, c, hstry = de.de_c(fobj=loss_func, bounds=bnds, popsize=80, its=500)\n", " b, c, hstry = de.de_c(fobj=ld.loss_func, bounds=bnds, pop=pnew, history=hstry, it_start=its_first, popsize=psnew, its=its_second, target=targ_spec, mats=mats, lams=lams)\n", " end = time.time()\n", " run_time1.append((end1 - start)/60.0)\n", " run_time2.append((end - end1)/60.0)\n", " run_time_tot.append((end - start)/60.0)\n", " run_pmre.append(c)\n", " run_best.append(b)\n", " run_hist.append(np.asarray(hstry))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-20T10:21:20.278082Z", "start_time": "2018-10-20T10:21:20.249204Z" } }, "outputs": [], "source": [ "pm, pstd, rm, rstd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-19T18:01:38.465084Z", "start_time": "2018-10-19T18:01:38.382183Z" } }, "outputs": [], "source": [ "# import jtplot module in notebook\n", "from jupyterthemes import jtplot\n", "\n", "# choose which theme to inherit plotting style from\n", "# onedork | grade3 | oceans16 | chesterish | monokai | solarizedl | solarizedd\n", "#jtplot.style(theme='monokai')\n", "\n", "# set \"context\" (paper, notebook, talk, poster)\n", "# scale font-size of ticklabels, legend, etc.\n", "# remove spines from x and y axes and make grid dashed\n", "#jtplot.style(context='talk', fscale=1.4, spines=False, gridlines='--')\n", "\n", "# turn on X- and Y-axis tick marks (default=False)\n", "# turn off the axis grid lines (default=True)\n", "# and set the default figure size\n", "#jtplot.style(ticks=True, grid=False, figsize=(6, 4.5))\n", "jtplot.style(grid=False, figsize=(4,2), fscale=0.7, s)\n", "# reset default matplotlib rcParams\n", "#jtplot.reset()\n", "plt.s\n", "plt.hist(run_pmre, bins=[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1.0, 2.5, 3], density=True, stacked=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-20T05:57:55.035771Z", "start_time": "2018-10-20T05:57:54.969003Z" } }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "ax.hist(run_pmre, bins=np.linspace(0, 2, 33), density=True, stacked=True, histtype='stepfilled')\n", "ax.spines['top'].set_visible(False)\n", "ax.spines['right'].set_visible(False)\n", "ax.set_xlim(left=0.0)\n", "#ax.spines['bottom'].set_visible(False)\n", "#ax.spines['left'].set_visible(False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-20T15:36:41.834584Z", "start_time": "2018-10-20T15:36:41.830750Z" } }, "outputs": [], "source": [ "a =np.arange(9)\n", "a =np.reshape(a, (int(a.size/3),3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-20T15:37:45.105731Z", "start_time": "2018-10-20T15:37:45.100307Z" } }, "outputs": [], "source": [ "a[:,0]" ] }, { "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.0" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }