Newer
Older
HCAL_project / arbitrary_act_f.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from tensorflow.python.framework import ops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lrelu(x, alpha=0.01):\n",
    "    return np.maximum(alpha*x,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=6.12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [],
   "source": [
    "def HCAL_act(x):\n",
    "    \n",
    "\n",
    "    return lrelu(np.minimum(2.+0.01*x,x))\n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "HCAL_act(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [],
   "source": [
    "np_HCAL_act = np.vectorize(HCAL_act)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 200 artists>"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAADH5JREFUeJzt3W+MZQdZx/Hvz6XwQomo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Zd0lq6FvvHNlqzdX1mQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.bar(np.arange(-10,10,0.1),np_HCAL_act(np.arange(-10,10,0.1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def d_HCAL_act(x):\n",
    "    num = np.multiply(a,np.exp(np.add(a,-x)))\n",
    "    den = np.square(np.add(1, np.exp(np.add(a,-x))))\n",
    "    return num/den"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "np_d_HCAL_act = np.vectorize(d_HCAL_act)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<BarContainer object of 300 artists>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAEXFJREFUeJzt3X+MZWddx/H3x26LESooOxjS7bJVl4RK+DkWEhSXWGTbJl0xIF1/gRbWPyhKIIZFtCUlJgWigFrAFZsCkTaVnxtYLIrFErHYQWppuyluSm3HNuzwU4FALXz9Y+7CdDoz98zMmbn3PvN+JTd7zznPnvs998z93Oc+555zU1VIktryQ6MuQJLUP8NdkhpkuEtSgwx3SWqQ4S5JDTLcJalBhrskNWhouCe5IsnxJLes0GZPkpuS3Jrkn/stUZK0Whl2ElOSZwLfAN5VVY9fYvkjgE8Be6vqriSPqqrjG1KtJKmTbcMaVNX1SXat0OTX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SfuA7/E5OyDxRZeFuSFwIdGWMuaTdprIkmYP2v/aFX92YJFm7o/JvokpiTPBf4CmAK+BtxUVc8ZLHsN8DvMH7l+eVV9dGSFrkKSdzP/8bOAO4HfPTFON+4GX1F7Mz+4TMWfjLikVUvyk8AHBpPbgPdMwnYkuQrYw/yVB78IXAJ8ELgG2AncBTy/qsb6YOUy27GHCXpNJPk54JPA54DvDWb/IfPj7pu2PyY63CVJS2t1WEaStjTDXZIaZLhLUoMMd0lqkOEuSQ0y3CWpQYa7JDXIcJekBv0/gVr/MEhP9BUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.bar(np.arange(-10,20,0.1),np_d_HCAL_act(np.arange(-10,20,0.1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "np_d_HCAL_act_32 = lambda x: np_d_HCAL_act(x).astype(np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tf_d_HCAL_act(x,name=None):\n",
    "    with tf.name_scope(name, \"d_HCAL_act\", [x]) as name:\n",
    "        y = tf.py_func(np_d_HCAL_act_32,\n",
    "                        [x],\n",
    "                        [tf.float32],\n",
    "                        name=name,\n",
    "                        stateful=False)\n",
    "        return y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def py_func(func, inp, Tout, stateful=True, name=None, grad=None):\n",
    "\n",
    "    # Need to generate a unique name to avoid duplicates:\n",
    "    rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))\n",
    "\n",
    "    tf.RegisterGradient(rnd_name)(grad)  # see _MySquareGrad for grad example\n",
    "    g = tf.get_default_graph()\n",
    "    with g.gradient_override_map({\"PyFunc\": rnd_name}):\n",
    "        return tf.py_func(func, inp, Tout, stateful=stateful, name=name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def HCAL_grad(op, grad):\n",
    "    x = op.inputs[0]\n",
    "\n",
    "    n_gr = tf_d_HCAL_act(x)\n",
    "    return grad * n_gr "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "np_HCAL_act_32 = lambda x: np_HCAL_act(x).astype(np.float32)\n",
    "\n",
    "def tf_HCAL_act(x, name=None):\n",
    "\n",
    "    with tf.name_scope(name, \"HCAL_act\", [x]) as name:\n",
    "        y = py_func(np_HCAL_act_32,\n",
    "                        [x],\n",
    "                        [tf.float32],\n",
    "                        name=name,\n",
    "                        grad=HCAL_grad)  # <-- here's the call to the gradient\n",
    "        return y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tf_HCAL_act(x):\n",
    "    a=6.12\n",
    "    arg=tf.cast(-x,tf.float32)\n",
    "    return tf.divide(a,tf.add(1.,tf.exp(tf.add(a,arg))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/hep/davide/miniconda3/envs/deepnet/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:107: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
      "Instructions for updating:\n",
      "Use `tf.global_variables_initializer` instead.\n",
      "[-10.  -9.  -8.  -7.  -6.  -5.  -4.  -3.  -2.  -1.   0.   1.   2.   3.\n",
      "   4.   5.   6.   7.   8.   9.] [6.1083620e-07 1.6604231e-06 4.5134962e-06 1.2268938e-05 3.3350316e-05\n",
      " 9.0654714e-05 2.4641879e-04 6.6978938e-04 1.8203339e-03 4.9456530e-03\n",
      " 1.3425037e-02 3.6355998e-02 9.7827286e-02 2.5881341e-01 6.5586865e-01\n",
      " 1.5055892e+00 2.8766201e+00 4.3257518e+00 5.3097801e+00 5.7947154e+00] [6.10836139e-07 1.66042264e-06 4.51349297e-06 1.22689135e-05\n",
      " 3.33501339e-05 9.06533678e-05 2.46408861e-04 6.69716042e-04\n",
      " 1.81979244e-03 4.94165625e-03 1.33955870e-02 3.61400247e-02\n",
      " 9.62635279e-02 2.47868240e-01 5.85580468e-01 1.13519740e+00\n",
      " 1.52450514e+00 1.26821423e+00 7.02955782e-01 3.07995707e-01]\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "\n",
    "    x = tf.constant(np.arange(-10,10,1.))\n",
    "    y = tf_HCAL_act(x)\n",
    "    tf.initialize_all_variables().run()\n",
    "\n",
    "    print(x.eval(), y.eval(), tf.gradients(y, [x])[0].eval())\n"
   ]
  },
  {
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}