{ "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\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": [ "### Dense" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**[core.Dense.0] test 1**" ] }, { "cell_type": "code", "execution_count": 3, "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: [7.3, -0.21]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='linear')(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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.Dense.1] test 2 (with sigmoid activation)**" ] }, { "cell_type": "code", "execution_count": 4, "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.999325, 0.447692]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='sigmoid')(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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.Dense.2] test 3 (with softplus activation and no bias)**" ] }, { "cell_type": "code", "execution_count": 5, "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: [6.801113, 0.338274]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='softplus', bias=False)(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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", "model.set_weights([W])\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.Dense.3] [GPU] test 1**" ] }, { "cell_type": "code", "execution_count": 3, "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: [7.3, -0.21]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='linear')(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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.Dense.4] [GPU] test 2 (with sigmoid activation)**" ] }, { "cell_type": "code", "execution_count": 4, "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.999325, 0.447692]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='sigmoid')(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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.Dense.5] [GPU] test 3 (with softplus activation and no bias)**" ] }, { "cell_type": "code", "execution_count": 5, "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: [6.801113, 0.338274]\n" ] } ], "source": [ "layer_0 = Input(shape=(6,))\n", "layer_1 = Dense(2, activation='softplus', bias=False)(layer_0)\n", "model = Model(input=layer_0, output=layer_1)\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", "model.set_weights([W])\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 }