{ "cells": [ { "cell_type": "code", "execution_count": 413, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T10:18:28.370039Z", "start_time": "2018-10-11T10:18:28.148656Z" } }, "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" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "import numpy as np\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", "import numpy as np\n", "import mxnet as mx\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='my_mod_fullycon', 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_input2'], \n", " context=mx.gpu(), \n", " label_names=None)\n", "mod.bind(for_training=False, \n", " data_shapes=[('/first_input2', (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": 341, "metadata": { "ExecuteTime": { "end_time": "2018-10-10T06:51:51.680142Z", "start_time": "2018-10-10T06:51:51.642525Z" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "def de_stage2(fobj, bounds, popint, history, itprev, mut=0.8, crossp=0.7, popsize=20, its=1000):\n", " #history=[]\n", " dimensions = len(bounds)\n", " min_b, max_b = np.asarray(bounds).T\n", " diff = np.fabs(min_b - max_b)\n", " pop_denorm = min_b + pop * diff\n", " fitness = np.asarray([fobj(ind) for ind in pop_denorm])\n", " best_idx = np.argmin(fitness)\n", " best = pop_denorm[best_idx]\n", " for i in range(its):\n", " #trialarr = np.zeros(popsize)\n", " for j in range(popsize):\n", " idxs = [idx for idx in range(popsize) if idx != j]\n", " a, b, c = pop[np.random.choice(idxs, 3, replace = False)]\n", " #mutant = np.clip(a + mut * (b - c), 0, 1)\n", " mutant = np.clip(pop[best_idx] + mut * (b - c), 0, 1)\n", " cross_points = np.random.rand(dimensions) < crossp\n", " if not np.any(cross_points):\n", " cross_points[np.random.randint(0, dimensions)] = True\n", " trial = np.where(cross_points, mutant, pop[j])\n", " #trialarr[j] = np.where(cross_points, mutant, pop[j])\n", " trial_denorm = min_b + trial * diff\n", " f = fobj(trial_denorm)\n", " if f < fitness[j]:\n", " fitness[j] = f\n", " pop[j] = trial\n", " if f < fitness[best_idx]:\n", " best_idx = j\n", " best = trial_denorm\n", " if i%20 == 0:\n", " print(i, fitness[best_idx])\n", " history.append([i+itprev, fitness[best_idx]])\n", " return best, fitness[best_idx], history\n", "\n", "\n", "\n", "\n", "def de(fobj, bounds, mut=0.8, crossp=0.7, popsize=20, its=1000):\n", " dimensions = len(bounds)\n", " history=[]\n", " pop = np.random.rand(popsize, dimensions)\n", " min_b, max_b = np.asarray(bounds).T\n", " diff = np.fabs(min_b - max_b)\n", " pop_denorm = min_b + pop * diff\n", " fitness = np.asarray([fobj(ind) for ind in pop_denorm])\n", " best_idx = np.argmin(fitness)\n", " best = pop_denorm[best_idx]\n", " for i in range(its):\n", " #trialarr = np.zeros(popsize)\n", " for j in range(popsize):\n", " idxs = [idx for idx in range(popsize) if idx != j]\n", " a, b, c = pop[np.random.choice(idxs, 3, replace = False)]\n", " #mutant = np.clip(a + mut * (b - c), 0, 1)\n", " mutant = np.clip(pop[best_idx] + mut * (b - c), 0, 1)\n", " cross_points = np.random.rand(dimensions) < crossp\n", " if not np.any(cross_points):\n", " cross_points[np.random.randint(0, dimensions)] = True\n", " trial = np.where(cross_points, mutant, pop[j])\n", " #trialarr[j] = np.where(cross_points, mutant, pop[j])\n", " trial_denorm = min_b + trial * diff\n", " f = fobj(trial_denorm)\n", " if f < fitness[j]:\n", " fitness[j] = f\n", " pop[j] = trial\n", " if f < fitness[best_idx]:\n", " best_idx = j\n", " best = trial_denorm\n", " if i%25 == 0:\n", " print(i, fitness[best_idx])\n", " history.append([i, fitness[best_idx]])\n", " return best, fitness[best_idx], history\n", "\n", "\n", "def de2(fobj, bounds, mut=0.8, crossp=0.7, popsize=20, its=1000):\n", " dimensions = len(bounds)\n", " history=[]\n", " pop = np.random.rand(popsize, dimensions)\n", " min_b, max_b = np.asarray(bounds).T\n", " diff = np.fabs(min_b - max_b)\n", " pop_denorm = min_b + pop * diff\n", " fitness = np.asarray(fobj(pop_denorm))\n", " best_idx = np.argmin(fitness)\n", " best = pop_denorm[best_idx]\n", " for i in range(its):\n", " trialarr = np.zeros((popsize, dimensions))\n", " for j in range(popsize):\n", " idxs = [idx for idx in range(popsize) if idx != j]\n", " a, b, c = pop[np.random.choice(idxs, 3, replace = False)]\n", " #mutant = np.clip(a + mut * (b - c), 0, 1)\n", " mutant = np.clip(pop[best_idx] + mut * (b - c), 0, 1)\n", " cross_points = np.random.rand(dimensions) < crossp\n", " if not np.any(cross_points):\n", " cross_points[np.random.randint(0, dimensions)] = True\n", " trialarr[j] = np.where(cross_points, mutant, pop[j])\n", " \n", " \n", " trial_denorm = min_b + trialarr * diff\n", " #print(trial_denorm)\n", " f = fobj(trial_denorm)\n", " for j in range(popsize):\n", " if f[j] < fitness[j]:\n", " fitness[j] = f[j]\n", " pop[j] = trialarr[j]\n", " if f[j] < fitness[best_idx]:\n", " best_idx = j\n", " best = trial_denorm[j]\n", " if i%50 == 0:\n", " print(i, fitness[best_idx])\n", " history.append([i, fitness[best_idx]])\n", " return pop, fitness, best, history\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 414, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T10:32:13.136288Z", "start_time": "2018-10-11T10:18:36.758693Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "iteration 0\n", "0 1.1705265\n", "50 0.4470625\n", "100 0.32243434\n", "150 0.28111637\n", "200 0.21437946\n", "250 0.20201741\n", "300 0.16932511\n", "350 0.16932511\n", "400 0.16932511\n", "450 0.16185972\n", "0 0.3121147107485763\n", "20 0.3121147107485763\n", "40 0.20575947392573551\n", "60 0.20575947392573551\n", "80 0.20575947392573551\n", "100 0.20575947392573551\n", "120 0.1619773697186888\n", "140 0.1619773697186888\n", "160 0.1619773697186888\n", "180 0.14528643576910827\n", "200 0.1421019546761191\n", "220 0.12192860901063259\n", "240 0.09253242337067973\n", "260 0.08082859150315144\n", "280 0.07013973087543791\n", "iteration 1\n", "0 1.2500818\n", "50 0.3227382\n", "100 0.22907153\n", "150 0.21646158\n", "200 0.20283288\n", "250 0.1756429\n", "300 0.16450137\n", "350 0.15796128\n", "400 0.15796128\n", "450 0.1558615\n", "0 0.24526450596507823\n", "20 0.24526450596507823\n", "40 0.24526450596507823\n", "60 0.24526450596507823\n", "80 0.24526450596507823\n", "100 0.24526450596507823\n", "120 0.24526450596507823\n", "140 0.22005031308025427\n", "160 0.12697402424132934\n", "180 0.12697402424132934\n", "200 0.12697402424132934\n", "220 0.12697402424132934\n", "240 0.11092008062789101\n", "260 0.07282972818333444\n", "280 0.07208495130811798\n", "iteration 2\n", "0 1.1091585\n", "50 0.37814587\n", "100 0.3192608\n", "150 0.2471918\n", "200 0.19585466\n", "250 0.19585466\n", "300 0.18117855\n", "350 0.17847842\n", "400 0.1716342\n", "450 0.16229132\n", "0 0.37607692325889464\n", "20 0.37607692325889464\n", "40 0.37607692325889464\n", "60 0.3330564707656615\n", "80 0.3330564707656615\n", "100 0.3330564707656615\n", "120 0.3054040753273184\n", "140 0.30056595386813395\n", "160 0.2372560274974856\n", "180 0.2342672435510109\n", "200 0.22194270193570056\n", "220 0.20433060781452445\n", "240 0.2008148449307781\n", "260 0.19259466427441313\n", "280 0.19259466427441313\n", "iteration 3\n", "0 1.896904\n", "50 0.3967435\n", "100 0.2679344\n", "150 0.17716393\n", "200 0.17716393\n", "250 0.17716393\n", "300 0.16917363\n", "350 0.16231227\n", "400 0.16231227\n", "450 0.1614473\n", "0 0.3608929001962287\n", "20 0.3608929001962287\n", "40 0.3608929001962287\n", "60 0.3608929001962287\n", "80 0.3608929001962287\n", "100 0.2580806115772493\n", "120 0.23025322555288447\n", "140 0.181724619284048\n", "160 0.15870143348935536\n", "180 0.15870143348935536\n", "200 0.1406708686897299\n", "220 0.11690700185881761\n", "240 0.11690700185881761\n", "260 0.11548268043464019\n", "280 0.10501889934993124\n", "iteration 4\n", "0 2.6791306\n", "50 0.37418172\n", "100 0.32684445\n", "150 0.22900505\n", "200 0.22496085\n", "250 0.21092188\n", "300 0.20759842\n", "350 0.17270699\n", "400 0.16482411\n", "450 0.16482411\n", "0 0.6541415220770362\n", "20 0.6541415220770362\n", "40 0.6541415220770362\n", "60 0.6541415220770362\n", "80 0.6541415220770362\n", "100 0.6022119287555459\n", "120 0.5550346370303875\n", "140 0.5550346370303875\n", "160 0.5550346370303875\n", "180 0.5129769561955577\n", "200 0.5129769561955577\n", "220 0.5073713851357137\n", "240 0.4369662361659122\n", "260 0.4369662361659122\n", "280 0.4369662361659122\n" ] } ], "source": [ "\n", "import numpy as np\n", "from scipy.optimize import rosen, differential_evolution\n", "import scnets as scn\n", "import mxnet as mx\n", "from mxnet import nd\n", "import snlay as snlay\n", "import time\n", "\n", "\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", "targ_spec2 = np.tile(targ_spec, (psize,1))\n", "\n", "# def loss_jumper(x, it_cnt=0):\n", "\n", "def mxmod_arr_loss(x):\n", " x_np = np.array(x)\n", " x_np = (x_np - 50.0)/20.0\n", " res2 = mod.predict(x_np)\n", " y_t = nd.array(targ_spec2, ctx=mx.gpu())\n", " err = nd.abs(y_t - res2)/y_t\n", " err2 = 100*nd.mean(err, axis=1).asnumpy()\n", " return err2\n", "\n", "def mxmod_loss(x):\n", " x_np = np.array(x)\n", " x_np = (x_np - 50.0)/20.0\n", " x_np = np.expand_dims(x_np, axis=0)\n", " res2 = mod.predict(x_np)\n", " y_t = nd.array(targ_spec, ctx=mx.gpu())\n", " err = nd.abs(y_t - res2)/y_t\n", " err2 = 100*nd.mean(err).asscalar()\n", " return err2\n", "\n", "def loss_func(x):\n", " #x_np = np.array(x)\n", " #count+=1\n", " spec_ac = snlay.calc_spectrum(x, mats, lams)\n", " diff = np.abs(targ_spec - spec_ac)/targ_spec\n", " return 100*np.amax(diff)\n", "\n", "\n", "\n", "\n", "\n", "bnds = [(30, 70)]*8\n", "\n", "psize = 640\n", "its_first = 500\n", "psnew = 20\n", "its_second = 300\n", "reps = 5\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 = 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", " 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": 415, "metadata": { "ExecuteTime": { "end_time": "2018-10-11T10:54:05.648366Z", "start_time": "2018-10-11T10:54:05.621923Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.06614759 0.07013973 0.09779269 0.19104877 0.43696624]\n", "[2.70871968 2.71356018 2.71965878 2.72612135 2.73761866]\n" ] } ], "source": [ "print(np.sort(run_pmre))\n", "print(np.sort(run_time_tot))\n" ] }, { "cell_type": "code", "execution_count": 303, "metadata": { "ExecuteTime": { "end_time": "2018-10-10T06:20:12.311685Z", "start_time": "2018-10-10T06:20:12.284836Z" } }, "outputs": [ { "data": { "text/plain": [ "array([30., 64., 51., 37., 37., 64., 35., 62.])" ] }, "execution_count": 303, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(b)" ] }, { "cell_type": "code", "execution_count": 304, "metadata": { "ExecuteTime": { "end_time": "2018-10-10T06:20:17.206532Z", "start_time": "2018-10-10T06:20:17.183258Z" } }, "outputs": [ { "data": { "text/plain": [ "array([30., 64., 52., 36., 36., 64., 35., 62.])" ] }, "execution_count": 304, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_test[29]" ] }, { "cell_type": "code", "execution_count": 284, "metadata": { "ExecuteTime": { "end_time": "2018-10-05T18:25:18.944088Z", "start_time": "2018-10-05T18:09:14.380943Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 34.764084177464106\n", "25 8.340754312117314\n", "50 5.596934152569711\n", "75 2.4355046393850808\n", "100 2.4355046393850808\n", "125 2.4134250007110736\n", "150 1.7133162654256295\n", "175 1.7133162654256295\n", "200 1.657703207258774\n", "225 1.357276014324554\n", "250 1.2038289909480697\n", "275 1.2038289909480697\n", "300 1.0010963661625447\n", "325 0.9091072669949225\n", "350 0.8380571414952189\n", "375 0.6046218491740213\n", "400 0.6046218491740213\n", "425 0.5006209308298584\n", "450 0.45559701169843764\n", "475 0.4376195672622949\n", "500 0.3938599657695569\n", "525 0.25238263922875026\n", "550 0.16105660067589186\n", "575 0.16105660067589186\n" ] } ], "source": [ "b, c = de(fobj=loss_func, bounds=bnds, popsize=80, its=600)" ] }, { "cell_type": "code", "execution_count": 286, "metadata": { "ExecuteTime": { "end_time": "2018-10-05T18:27:24.231716Z", "start_time": "2018-10-05T18:27:24.206383Z" } }, "outputs": [ { "data": { "text/plain": [ "array([30., 64., 52., 36., 36., 64., 35., 62.])" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(b)" ] }, { "cell_type": "code", "execution_count": 418, "metadata": { "ExecuteTime": { "end_time": "2018-10-13T05:37:08.770741Z", "start_time": "2018-10-13T05:37:08.742574Z" } }, "outputs": [ { "data": { "text/plain": [ "7.984925229121" ] }, "execution_count": 418, "metadata": {}, "output_type": "execute_result" } ], "source": [ "41**8/(1e12)" ] }, { "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.5.4" }, "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 }