{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T13:53:26.552385Z",
     "start_time": "2018-10-03T13:53:26.342168Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.85, -0.95, -1.  , -0.65,  0.3 , -0.65,  0.55, -0.05])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import h5py\n",
    "from sklearn.model_selection import train_test_split\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",
    "\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",
    "\n",
    "x_test = (x_test - 50.0)/20.0\n",
    "\n",
    "x_test[9]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T13:41:28.745978Z",
     "start_time": "2018-10-03T13:41:26.725681Z"
    }
   },
   "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)                      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T15:57:53.584259Z",
     "start_time": "2018-10-03T15:57:53.321389Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "255.54943084716797\n"
     ]
    }
   ],
   "source": [
    "import scnets as scn\n",
    "import mxnet as mx\n",
    "from mxnet import nd\n",
    "import time\n",
    "\n",
    "\n",
    "bsize = 4000\n",
    "y_true = nd.array(y_test[9], ctx=mx.gpu())\n",
    "reps = 1000\n",
    "\n",
    "start = time.time()\n",
    "for icv in np.arange(reps):\n",
    "    x_t = x_test[0:bsize]\n",
    "    res2 = mod.predict(x_t)\n",
    "    err = nd.abs(y_true - res2)/y_true\n",
    "    finerr = 100*nd.mean(err)\n",
    "end = time.time()\n",
    "print(1000*(end - start))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T08:45:32.141435Z",
     "start_time": "2018-10-03T08:45:31.874129Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using MXNet backend\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.17115497589111328\n"
     ]
    }
   ],
   "source": [
    "import scnets as scn\n",
    "import mxnet as mx\n",
    "from mxnet import nd\n",
    "import time\n",
    "\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",
    "#size = np.expand_dims(size, axis = 0)\n",
    "size.shape\n",
    "\n",
    "y_true = nd.array(y_test[9], ctx=mx.gpu())\n",
    "\n",
    "reps = 1000\n",
    "start = time.time()\n",
    "#res1 = model.predict(size)\n",
    "#data_iter = mx.io.NDArrayIter(size, None, 1)\n",
    "for icv in np.arange(reps):\n",
    "    size = np.random.randint(30, 71, (1,8))\n",
    "    size = (size - 50.0)/20.0\n",
    "    res2 = mod.predict(size)\n",
    "    err = nd.abs(y_true - res2)/y_true\n",
    "    finerr = nd.sum(err)\n",
    "    \n",
    "end = time.time()\n",
    "print(1000*(end - start)/reps)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T08:45:41.210914Z",
     "start_time": "2018-10-03T08:45:41.205195Z"
    }
   },
   "outputs": [],
   "source": [
    "import scnets as scn\n",
    "import mxnet as mx\n",
    "from mxnet import nd\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",
    "#     err2cp = nd.zeros_like(err2, ctx=mx.cpu())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T08:45:59.234753Z",
     "start_time": "2018-10-03T08:45:57.287759Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.9373879432678223\n"
     ]
    }
   ],
   "source": [
    "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)   # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-10-03T08:45:54.615638Z",
     "start_time": "2018-10-03T08:45:54.610110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "235.14212"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-30T15:58:44.524220Z",
     "start_time": "2018-09-30T15:58:44.518399Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.__getitem__(0).asscalar()"
   ]
  },
  {
   "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
}