{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:20:36.613702Z", "start_time": "2018-09-30T16:20:35.532595Z" } }, "outputs": [ { "data": { "text/plain": [ "array([67., 31., 30., 37., 56., 37., 61., 49.])" ] }, "execution_count": 1, "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", "\n", "\n", "x_test[9]\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:20:49.933501Z", "start_time": "2018-09-30T16:20:45.837033Z" } }, "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": null, "metadata": {}, "outputs": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:21:01.661357Z", "start_time": "2018-09-30T16:21:01.485033Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.16714763641357422\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": 5, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:21:08.815235Z", "start_time": "2018-09-30T16:21:08.809272Z" } }, "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": 11, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:49:04.385979Z", "start_time": "2018-09-30T16:49:02.703408Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.672537088394165\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": 8, "metadata": { "ExecuteTime": { "end_time": "2018-09-30T16:21:45.903809Z", "start_time": "2018-09-30T16:21:45.898205Z" } }, "outputs": [ { "data": { "text/plain": [ "248.17133" ] }, "execution_count": 8, "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }