{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T12:55:42.888397Z", "start_time": "2018-10-04T12:55:42.659586Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset has been loaded\n" ] }, { "data": { "text/plain": [ "array([69., 49., 70., 40., 38., 64., 47., 57.])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "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", "x_test[19]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T12:55:49.443991Z", "start_time": "2018-10-04T12:55:48.164551Z" } }, "outputs": [], "source": [ "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', 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=['/input_12'], \n", " context=mx.gpu(), \n", " label_names=None)\n", "mod.bind(for_training=False, \n", " data_shapes=[('/input_12', (1,8))], \n", " label_shapes=mod._label_shapes)\n", "mod.set_params(arg_params, aux_params, allow_missing=True) \n", "\n" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "ExecuteTime": { "end_time": "2018-10-05T07:14:49.903509Z", "start_time": "2018-10-05T07:12:05.473313Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "differential_evolution step 1: f(x)= 1.51365\n", "differential_evolution step 2: f(x)= 1.39543\n", "differential_evolution step 3: f(x)= 1.39543\n", "differential_evolution step 4: f(x)= 1.3645\n", "differential_evolution step 5: f(x)= 1.3645\n", "differential_evolution step 6: f(x)= 1.3645\n", "differential_evolution step 7: f(x)= 0.990742\n", "differential_evolution step 8: f(x)= 0.990742\n", "differential_evolution step 9: f(x)= 0.990742\n", "differential_evolution step 10: f(x)= 0.966112\n", "differential_evolution step 11: f(x)= 0.966112\n", "differential_evolution step 12: f(x)= 0.966112\n", "differential_evolution step 13: f(x)= 0.590478\n", "differential_evolution step 14: f(x)= 0.590478\n", "differential_evolution step 15: f(x)= 0.590478\n", "differential_evolution step 16: f(x)= 0.590478\n", "differential_evolution step 17: f(x)= 0.590478\n", "differential_evolution step 18: f(x)= 0.590478\n", "differential_evolution step 19: f(x)= 0.590478\n", "differential_evolution step 20: f(x)= 0.590478\n", "differential_evolution step 21: f(x)= 0.590478\n", "differential_evolution step 22: f(x)= 0.590478\n", "differential_evolution step 23: f(x)= 0.590478\n", "differential_evolution step 24: f(x)= 0.590478\n", "differential_evolution step 25: f(x)= 0.590478\n", "differential_evolution step 26: f(x)= 0.590478\n", "differential_evolution step 27: f(x)= 0.590478\n", "differential_evolution step 28: f(x)= 0.590478\n", "differential_evolution step 29: f(x)= 0.590478\n", "differential_evolution step 30: f(x)= 0.590478\n", "differential_evolution step 31: f(x)= 0.590478\n", "differential_evolution step 32: f(x)= 0.590478\n", "differential_evolution step 33: f(x)= 0.590478\n", "differential_evolution step 34: f(x)= 0.590478\n", "differential_evolution step 35: f(x)= 0.590478\n", "differential_evolution step 36: f(x)= 0.590478\n", "differential_evolution step 37: f(x)= 0.590478\n", "differential_evolution step 38: f(x)= 0.590478\n", "differential_evolution step 39: f(x)= 0.590478\n", "differential_evolution step 40: f(x)= 0.590478\n", "differential_evolution step 41: f(x)= 0.590478\n", "differential_evolution step 42: f(x)= 0.590478\n", "differential_evolution step 43: f(x)= 0.590478\n", "differential_evolution step 44: f(x)= 0.590478\n", "differential_evolution step 45: f(x)= 0.590478\n", "differential_evolution step 46: f(x)= 0.590478\n", "differential_evolution step 47: f(x)= 0.590478\n", "differential_evolution step 48: f(x)= 0.590478\n", "differential_evolution step 49: f(x)= 0.590478\n", "differential_evolution step 50: f(x)= 0.590478\n", "differential_evolution step 51: f(x)= 0.590478\n", "differential_evolution step 52: f(x)= 0.590478\n", "differential_evolution step 53: f(x)= 0.590478\n", "differential_evolution step 54: f(x)= 0.590478\n", "differential_evolution step 55: f(x)= 0.590478\n", "differential_evolution step 56: f(x)= 0.590478\n", "differential_evolution step 57: f(x)= 0.590478\n", "differential_evolution step 58: f(x)= 0.590478\n", "differential_evolution step 59: f(x)= 0.590478\n", "differential_evolution step 60: f(x)= 0.590478\n", "differential_evolution step 61: f(x)= 0.590478\n", "differential_evolution step 62: f(x)= 0.590478\n", "differential_evolution step 63: f(x)= 0.590478\n", "differential_evolution step 64: f(x)= 0.590478\n", "differential_evolution step 65: f(x)= 0.590478\n", "differential_evolution step 66: f(x)= 0.590478\n", "differential_evolution step 67: f(x)= 0.590478\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0mrecombination\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0mmaxiter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m300\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 48\u001b[0;31m popsize=130)\n\u001b[0m\u001b[1;32m 49\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/scipy/optimize/_differentialevolution.py\u001b[0m in \u001b[0;36mdifferential_evolution\u001b[0;34m(func, bounds, args, strategy, maxiter, popsize, tol, mutation, recombination, seed, callback, disp, polish, init, atol)\u001b[0m\n\u001b[1;32m 221\u001b[0m \u001b[0mcallback\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 222\u001b[0m disp=disp, init=init, atol=atol)\n\u001b[0;32m--> 223\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msolver\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 224\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/scipy/optimize/_differentialevolution.py\u001b[0m in \u001b[0;36msolve\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 585\u001b[0m \u001b[0;31m# evolve the population by a generation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 586\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 587\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 588\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mStopIteration\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 589\u001b[0m \u001b[0mwarning_flag\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/scipy/optimize/_differentialevolution.py\u001b[0m in \u001b[0;36m__next__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 708\u001b[0m \u001b[0;31m# determine the energy of the objective function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 709\u001b[0;31m \u001b[0menergy\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 710\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nfev\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 711\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mmxmod_loss\u001b[0;34m(x, y_true, mxmod)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmxmod_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_true\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmxmod\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0my_t\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_true\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgpu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0mx_np\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mx_np\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx_np\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m50.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m20.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/mxnet/ndarray/utils.py\u001b[0m in \u001b[0;36marray\u001b[0;34m(source_array, ctx, dtype)\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0m_sparse_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource_array\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mctx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 146\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource_array\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mctx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 147\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/mxnet/ndarray/ndarray.py\u001b[0m in \u001b[0;36marray\u001b[0;34m(source_array, ctx, dtype)\u001b[0m\n\u001b[1;32m 2433\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'source_array must be array like object'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2434\u001b[0m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mempty\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msource_array\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2435\u001b[0;31m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msource_array\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2436\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2437\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/mxnet/ndarray/ndarray.py\u001b[0m in \u001b[0;36m__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 442\u001b[0m \u001b[0mindexing_dispatch_code\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_indexing_dispatch_code\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 443\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mindexing_dispatch_code\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0m_NDARRAY_BASIC_INDEXING\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 444\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_set_nd_basic_indexing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 445\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mindexing_dispatch_code\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0m_NDARRAY_ADVANCED_INDEXING\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 446\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_set_nd_advanced_indexing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/mxnet/ndarray/ndarray.py\u001b[0m in \u001b[0;36m_set_nd_basic_indexing\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 708\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgeneric\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 709\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbroadcast_to\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 710\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sync_copyfrom\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 711\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# value might be a list or a tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 712\u001b[0m \u001b[0mvalue_nd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_prepare_value_nd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda3/envs/dp2/lib/python3.5/site-packages/mxnet/ndarray/ndarray.py\u001b[0m in \u001b[0;36m_sync_copyfrom\u001b[0;34m(self, source_array)\u001b[0m\n\u001b[1;32m 874\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 875\u001b[0m \u001b[0msource_array\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata_as\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mctypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mc_void_p\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 876\u001b[0;31m ctypes.c_size_t(source_array.size)))\n\u001b[0m\u001b[1;32m 877\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 878\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_slice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "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", "\n", "def loss_func(x, mats, lams, targ_spec):\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.mean(diff)\n", "\n", "def mxmod_loss(x, y_true, mxmod):\n", " y_t = nd.array(y_true, ctx=mx.gpu())\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 = mxmod.predict(x_np)\n", " err = nd.abs(y_t - res2)/y_t\n", " err2 = 100*nd.mean(err).asscalar()\n", " #ez2 = err2.copyto(mx.cpu())\n", " return err2\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[39]\n", "\n", "bounds = [(30,70), (30,70), (30, 70), (30, 70), (30, 70), (30, 70), (30, 70), (30, 70)]\n", "# result = differential_evolution(func=loss_func, \n", "# bounds=bounds, \n", "# args=(mats, lams, targ_spec),\n", "# tol=1e-5,\n", "# disp=True,\n", "# maxiter=100,\n", "# popsize=3)\n", "\n", "result = differential_evolution(func=mxmod_loss, \n", " bounds=bounds, \n", " args=(targ_spec, mod),\n", " tol=1e-5,\n", " disp=True,\n", " mutation=(0.5, 1.0),\n", " strategy='best1bin', \n", " recombination=0.3,\n", " maxiter=300,\n", " popsize=130)\n", "\n", "\n", "\n", "print(np.round(result.x), result.fun)\n", "print(x_test[39])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T12:47:42.194162Z", "start_time": "2018-10-03T12:47:42.167882Z" } }, "outputs": [], "source": [ "mod" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T11:53:25.995891Z", "start_time": "2018-10-03T11:53:25.969647Z" } }, "outputs": [], "source": [ "result" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T09:33:55.247901Z", "start_time": "2018-10-03T09:33:55.213518Z" } }, "outputs": [], "source": [ "from PyGMO.problem import base\n", "import scnets as scn\n", "import mxnet as mx\n", "from mxnet import nd\n", "import snlay as snlay\n", "\n", "mats = np.array([3, 4, 3, 4, 3, 4, 3, 4])\n", "lams = np.linspace(300, 1200, 256)\n", "center = np.zeros(8) + 57\n", "targ_spec = y_test[19]\n", "\n", "def give_loss(x_in, y_true, mxmod):\n", " y_t = nd.array(y_true, ctx=mx.gpu())\n", " res2 = mxmod.predict(x_in)\n", " err = nd.abs(y_t - res2)/y_t\n", " err2 = nd.sum(err).asscalar()\n", " #ez2 = err2.copyto(mx.cpu())\n", " return err2\n", "\n", "\n", "\n", "\n", "\n", "def mxmod_loss(x_in, y_true, mxmod):\n", " y_t = nd.array(y_true, ctx=mx.gpu())\n", " res2 = mxmod.predict(x_in)\n", " err = nd.abs(y_t - res2)/y_t\n", " err2 = 100*nd.mean(err).asscalar()\n", " #ez2 = err2.copyto(mx.cpu())\n", " return err2\n", "\n", "def get_best_ans(islets):\n", " champ_fits = np.array([islets[idx].population.champion.f[0] for idx in range(len(islets))])\n", " minidx = np.argmin(champ_fits)\n", " midx = np.asscalar(minidx)\n", " best_x = np.array(archi[midx].population.champion.x)\n", " return best_x, np.amin(champ_fits)\n", "\n", "def loss_func_mxmod(x):\n", " x_np = np.expand_dims(np.array(x), axis=0)\n", " #spec_ac = snlay.calc_spectrum(x_np, mats, lams)\n", " return mxmod_loss(x_np, targ_spec, mod)\n", "\n", "\n", "def loss_func(x):\n", " x_np = np.array(x)\n", " spec_ac = snlay.calc_spectrum(x_np, mats, lams)\n", " diff = np.abs(targ_spec - spec_ac)/targ_spec\n", " return 100*np.mean(diff)\n", "\n", "class my_problem(base):\n", " def __init__(self, dim=8):\n", " # First we call the constructor of the base class telling PyGMO\n", " super(my_problem,self).__init__(dim)\n", " self.set_bounds(30, 70)\n", " \n", " # Reimplement the virtual method that defines the objective function.\n", " def _objfun_impl(self, x):\n", " # Compute the sphere function\n", " #f = loss_func(x)\n", " f = loss_func_mxmod(x)\n", " return (f, )\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T10:06:06.528488Z", "start_time": "2018-10-03T10:06:06.361623Z" }, "scrolled": true }, "outputs": [], "source": [ "# import time\n", "# size = np.random.randint(30, 71, (1,8))\n", "# #spec_ac = snlay.calc_spectrum(size, mats, lams)\n", "# size = (size - 50.0)/20.0\n", "\n", "# ans = 1.00\n", "# cd = give_loss(x_in=size, y_true=y_test[6], mxmod=mod)\n", "\n", "\n", "# reps = 1000\n", "# start = time.time()\n", "# for icv in np.arange(reps):\n", "# cc = give_loss(x_in=size, y_true=y_test[6], mxmod=mod)\n", "# #ans = cc.asscalar()\n", "\n", "# end = time.time()\n", "# print(1000*(end - start)/reps) # \n", "\n", "\n", "\n", "\n", "import PyGMO as pyg\n", "\n", "prob = my_problem(dim=8) # Create a 10-dimensional problem\n", "\n", "#algo = pyg.algorithm.bee_colony(gen=50) # 500 generations of bee_colony algorithm\n", "\n", "# # # prob = pyg.problem.schwefel(dim = 8)\n", "algo = pyg.algorithm.de(gen = 100)\n", "archi = pyg.archipelago(algo,prob,1,7, topology = pyg.topology.ring())\n", "\n", "\n", "\n", "\n", "# epocs = 1\n", "# es = np.arange(epocs)\n", "# opt_hist = []\n", "# for epch in range(epocs):\n", "# archi.evolve(1)\n", "# x, f = get_best_ans(archi)\n", "# opt_hist.append(f)\n", "# print(f)\n", "\n", "# print(x)\n", "\n", "# print(50 + 20*x_test[19])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-01T17:47:25.496340Z", "start_time": "2018-10-01T17:47:25.472208Z" } }, "outputs": [], "source": [ "opt_hist" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-03T08:04:23.798532Z", "start_time": "2018-10-03T08:04:23.443702Z" } }, "outputs": [], "source": [ "fig1 = plt.figure(figsize=(5,3))\n", "ax = fig1.add_subplot(1,1,1)\n", "#ax.set_title('silica coated gold')\n", "ax.set_xlabel('Evolution')\n", "ax.set_ylabel('Objective')\n", "ax.semilogy(es, opt_hist)\n", "ax.spines['top'].set_visible(False)\n", "ax.spines['right'].set_visible(False)" ] }, { "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" } }, "nbformat": 4, "nbformat_minor": 2 }