From a5dd9cd3fec931392a677909c5402eab1a09ba19 Mon Sep 17 00:00:00 2001 From: Stefano Carrazza Date: Fri, 28 Feb 2020 22:54:27 +0100 Subject: [PATCH] removing useless notebooks --- notebooks/SaveLoadModel.ipynb | 128 ----------- notebooks/Tensorflow Testing.ipynb | 344 ----------------------------- 2 files changed, 472 deletions(-) delete mode 100644 notebooks/SaveLoadModel.ipynb delete mode 100644 notebooks/Tensorflow Testing.ipynb diff --git a/notebooks/SaveLoadModel.ipynb b/notebooks/SaveLoadModel.ipynb deleted file mode 100644 index 8f332af..0000000 --- a/notebooks/SaveLoadModel.ipynb +++ /dev/null @@ -1,128 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 82, - "metadata": {}, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "import numpy as np" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Save custom model" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": {}, - "outputs": [], - "source": [ - "class Integrand(tf.Module):\n", - "\n", - " @tf.function(\n", - " input_signature=[\n", - " tf.TensorSpec(shape=None, dtype=tf.float64),\n", - " tf.TensorSpec(shape=None, dtype=tf.float64)\n", - " ])\n", - " def quick_integrand(self, xarr, n_dim):\n", - " \"\"\"Le page test function\"\"\"\n", - " a = tf.constant(0.1, dtype=tf.float64)\n", - " n100 = tf.cast(100*n_dim, dtype=tf.float64)\n", - " pref = tf.pow(1.0/a/np.sqrt(np.pi), n_dim)\n", - " coef = tf.reduce_sum(tf.range(n100+1))\n", - " coef += tf.reduce_sum(tf.square( (xarr-1.0/2.0)/a ), axis=1)\n", - " coef -= (n100+1)*n100/2.0\n", - " return pref*tf.exp(-coef)" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "INFO:tensorflow:Assets written to: model/assets\n" - ] - } - ], - "source": [ - "original_model = Integrand()\n", - "tf.saved_model.save(original_model, 'model')" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": {}, - "outputs": [], - "source": [ - "xx = np.array([[0.2]])\n", - "r1 = original_model.quick_integrand(xx, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Load custom model from file" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": {}, - "outputs": [], - "source": [ - "k = tf.saved_model.load('model')" - ] - }, - { - "cell_type": "code", - "execution_count": 87, - "metadata": {}, - "outputs": [], - "source": [ - "r2 = k.quick_integrand(xx, 2)" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": {}, - "outputs": [], - "source": [ - "assert r1 == r2, 'Model save/load has problems!'" - ] - } - ], - "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.5" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/notebooks/Tensorflow Testing.ipynb b/notebooks/Tensorflow Testing.ipynb deleted file mode 100644 index 9d5b4bc..0000000 --- a/notebooks/Tensorflow Testing.ipynb +++ /dev/null @@ -1,344 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "tf.Tensor(9, shape=(), dtype=int32)\n", - "tf.Tensor(7, shape=(), dtype=int32)\n", - "tf.Tensor([7 9], shape=(2,), dtype=int64)\n" - ] - } - ], - "source": [ - "@tf.function\n", - "def add(a, b):\n", - " return a + b\n", - "print(add(4,5))\n", - "aa = tf.constant(3)\n", - "bb = tf.constant(4)\n", - "cc = tf.constant(np.array([3,4]))\n", - "dd = tf.constant(np.array([4,5]))\n", - "print(add(aa,bb))\n", - "print(add(cc,dd))" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": {}, - "outputs": [], - "source": [ - "n_dim = 3\n", - "BINS_MAX = 5\n", - "n_events = int(2e1)\n", - "DTYPE=tf.float64\n", - "DTYPEINT=tf.int64\n", - "x = tf.Variable(tf.zeros(n_dim, dtype=DTYPE))\n", - "div_index = tf.Variable(tf.zeros((n_dim), dtype=DTYPEINT))\n", - "reg_i = 0.0\n", - "reg_f = 1.0\n", - "\n", - "def int_me(i):\n", - " return tf.constant(i, dtype = DTYPEINT)\n", - "def float_me(i):\n", - " return tf.constant(i, dtype = DTYPE)\n", - "\n", - "ione = int_me(1)\n", - "izero = int_me(0)\n", - "fone = float_me(1)\n", - "fzero = float_me(0)\n", - "\n", - "shape_rn = tf.TensorSpec(shape=(None,1), dtype=DTYPE)\n", - "shape_sub = tf.TensorSpec(shape=(None, BINS_MAX), dtype = DTYPE)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The first step is to be able to do the generation of the random numbers in a batched way" - ] - }, - { - "cell_type": "code", - "execution_count": 135, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(3, 5)\n", - "IN shape: (1000, 3)\n", - "OUT shape x: (1000, 3)\n", - "OUT shape index: (1000, 3)\n", - "OUT shape weight: (1000,)\n" - ] - } - ], - "source": [ - "ione = int_me(1)\n", - "izero = int_me(0)\n", - "fone = float_me(1)\n", - "fzero = float_me(0)\n", - "n_events = 1000\n", - "\n", - "shape_rn = tf.TensorSpec(shape=(None, n_dim), dtype=DTYPE)\n", - "shape_sub = tf.TensorSpec(shape=(n_dim, BINS_MAX), dtype = DTYPE)\n", - "\n", - "@tf.function(input_signature=[shape_rn, shape_sub])\n", - "def gen_one_dim(rn, subdivision):\n", - " \"\"\" Digest one random number\n", - " \"\"\"\n", - " # Get the corresponding random number\n", - " xn = BINS_MAX*(1.0 - rn)\n", - " int_xn = tf.maximum(tf.cast(0, DTYPEINT),\n", - " tf.minimum(tf.cast(xn, DTYPEINT), BINS_MAX))\n", - " # In practice int_xn = int(xn)-1 unless xn < 1\n", - " aux_rand = xn - tf.cast(int_xn, dtype=DTYPE)\n", - " # Now get the indices that will be used with this subdivision\n", - " # If the index is 0, we cannot get the index-1 so...\n", - " ind_f = tf.transpose(int_xn)\n", - " ind_i = tf.maximum(ind_f - 1, 0)\n", - " gather_f = tf.gather(subdivision, ind_f, batch_dims=1)\n", - " gather_i_tmp = tf.gather(subdivision, ind_i, batch_dims=1)\n", - " # Now the ones that had a \"fake 0\" need to be set to 0\n", - " ind_is_0 = tf.equal(ind_f, 0)\n", - " gather_i = tf.where(ind_is_0, fzero, gather_i_tmp)\n", - " # Now compute the random number for this dimension\n", - " x_ini = tf.transpose(gather_i)\n", - " xdelta = tf.transpose(gather_f) - x_ini\n", - " rand_x = x_ini + xdelta*aux_rand\n", - " x = reg_i + rand_x*(reg_f - reg_i)\n", - " weights = tf.reduce_prod(xdelta*BINS_MAX, axis=1)\n", - " return x, int_xn, weights\n", - "oner = tf.random.uniform((n_events, n_dim), minval = 0.0, maxval=1.0, dtype=DTYPE)\n", - "#gen_one_dim(oner, divisions)\n", - "x, ind, w = gen_one_dim(oner, divisions)\n", - "print(divisions.shape)\n", - "print(f\"IN shape: {oner.shape}\")\n", - "print(f\"OUT shape x: {x.shape}\")\n", - "print(f\"OUT shape index: {ind.shape}\")\n", - "print(f\"OUT shape weight: {w.shape}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The same must be also possible for the integrand" - ] - }, - { - "cell_type": "code", - "execution_count": 136, - "metadata": {}, - "outputs": [], - "source": [ - "@tf.function\n", - "def quick_integrand(xarr, n_dim = None):\n", - " \"\"\"Le page test function\"\"\"\n", - " if n_dim is None:\n", - " n_dim = xarr.shape[-1]\n", - " a = tf.constant(0.1, dtype=DTYPE)\n", - " n100 = tf.cast(100*n_dim, dtype=DTYPE)\n", - " pref = tf.pow(1.0/a/np.sqrt(np.pi), n_dim)\n", - " coef = fzero\n", - " for i in range(n100+1):\n", - " coef += i\n", - " for i in range(n_dim):\n", - " coef += tf.pow( (x[:,i]-1.0/2.0)/a, 2)\n", - " coef -= (n100+1)*n100/2.0\n", - " return pref*tf.exp(-coef)\n", - "tmp = quick_integrand(x, n_dim=n_dim)/n_events\n", - "tmp2 = tf.square(tmp)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "And, finally, the same must also be true for the creation of the array of partial results per BIN" - ] - }, - { - "cell_type": "code", - "execution_count": 243, - "metadata": {}, - "outputs": [], - "source": [ - "my_dim = 2\n", - "all_ind = ind[:,0:my_dim]" - ] - }, - { - "cell_type": "code", - "execution_count": 255, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 1.9 s, sys: 2.36 ms, total: 1.91 s\n", - "Wall time: 1.91 s\n" - ] - } - ], - "source": [ - "%%time \n", - "np_i = all_ind.numpy()\n", - "np_tmp2 = tmp2.numpy()\n", - "arr_res2 = np.zeros((my_dim, BINS_MAX))\n", - "for i in range(n_events):\n", - " for j in range(my_dim):\n", - " arr_res2[j, np_i[i, j]] += tmp2[i]" - ] - }, - { - "cell_type": "code", - "execution_count": 210, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 127 ms, sys: 3.22 ms, total: 130 ms\n", - "Wall time: 127 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "if my_dim == 1:\n", - " arr_res2 = []\n", - " for i in range(BINS_MAX):\n", - " mask = tf.equal(all_ind, i)[:,0]\n", - " res_tmp = tf.reduce_sum(tf.boolean_mask(tmp2, mask))\n", - " arr_res2.append(res_tmp.numpy())" - ] - }, - { - "cell_type": "code", - "execution_count": 258, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 28.6 ms, sys: 3.24 ms, total: 31.8 ms\n", - "Wall time: 26.6 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "def consume_events(res2, ind):\n", - " arr_res2 = []\n", - " all_bins = tf.range(BINS_MAX, dtype = DTYPEINT)\n", - " for j in range(my_dim):\n", - " eq = tf.transpose(tf.equal(ind[:,j:j+1], all_bins))\n", - " res_tmp = tf.reduce_sum(tf.where(eq, res2, fzero))\n", - " arr_res2.append(res_tmp)\n", - " return arr_res2\n", - "xx = consume_events(tmp2, all_ind)" - ] - }, - { - "cell_type": "code", - "execution_count": 252, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "TensorShape([5, 1000])" - ] - }, - "execution_count": 252, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "all_bins = tf.range(BINS_MAX, dtype = DTYPEINT)\n", - "mask = tf.transpose(tf.equal(all_ind[:,0:1], all_bins))\n", - "res_tmp = tf.where(mask, tmp2, fzero)\n", - "res_tmp.shape" - ] - }, - { - "cell_type": "code", - "execution_count": 246, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 246, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tf.equal(all_ind[:,0:1], all_bins)" - ] - }, - { - "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.4" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}