{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "import numpy as np\n", "from keras.models import Model\n", "from keras.layers import Input\n", "from keras.layers.core import Dense, Activation\n", "from keras import backend as K" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def format_decimal(arr, places=6):\n", " return [round(x * 10**places) / 10**places for x in arr]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Activation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[core.Activation.0] test 1 (tanh)**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in: [0, 0.2, 0.5, -0.1, 1, 2]\n", "in shape: (6,)\n", "out shape: (2,)\n", "out: [0.999999, -0.206966]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2)(layer_0)\n", "layer_2 = Activation('tanh')(layer_1)\n", "model = Model(input=layer_0, output=layer_2)\n", "\n", "W = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n", "b = np.array([0.5, 0.7])\n", "model.set_weights([W, b])\n", "\n", "data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n", "data_in_shape = (6,)\n", "print('in:', data_in)\n", "print('in shape:', data_in_shape)\n", "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n", "result = model.predict(np.array([arr_in]))\n", "arr_out = result[0]\n", "print('out shape:', arr_out.shape)\n", "data_out = format_decimal(arr_out.ravel().tolist())\n", "print('out:', data_out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[core.Activation.1] test 2 (hardSigmoid)**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in: [0, 0.2, 0.5, -0.1, 1, 2]\n", "in shape: (6,)\n", "out shape: (2,)\n", "out: [1.0, 0.458]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2)(layer_0)\n", "layer_2 = Activation('hard_sigmoid')(layer_1)\n", "model = Model(input=layer_0, output=layer_2)\n", "\n", "W = np.array([0.1, 0.4, 0.5, 0.1, 1, -2, 0, 0.3, 0.2, 0.1, 3, 0]).reshape((6, 2))\n", "b = np.array([0.5, 0.7])\n", "model.set_weights([W, b])\n", "\n", "data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n", "data_in_shape = (6,)\n", "print('in:', data_in)\n", "print('in shape:', data_in_shape)\n", "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n", "result = model.predict(np.array([arr_in]))\n", "arr_out = result[0]\n", "print('out shape:', arr_out.shape)\n", "data_out = format_decimal(arr_out.ravel().tolist())\n", "print('out:', data_out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.2" } }, "nbformat": 4, "nbformat_minor": 0 }