{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Layering Nested Operations\n", "\n", "We start by loading the necessary libraries and resetting the computational graph." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import tensorflow as tf\n", "import os\n", "from tensorflow.python.framework import ops\n", "ops.reset_default_graph()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a graph session" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sess = tf.Session()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create the Tensors, Constants, and Placeholders\n", "\n", "We start by creating an array to feed in to a placeholder (note the agreements on the dimensions). We then declare some graph constants to use in the operations." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create data to feed in\n", "my_array = np.array([[1., 3., 5., 7., 9.],\n", " [-2., 0., 2., 4., 6.],\n", " [-6., -3., 0., 3., 6.]])\n", "# Duplicate the array for having two inputs\n", "x_vals = np.array([my_array, my_array + 1])\n", "# Declare the placeholder\n", "x_data = tf.placeholder(tf.float32, shape=(3, 5))\n", "# Declare constants for operations\n", "m1 = tf.constant([[1.],[0.],[-1.],[2.],[4.]])\n", "m2 = tf.constant([[2.]])\n", "a1 = tf.constant([[10.]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Declare Operations\n", "\n", "We start with matrix multiplication (A[3x5] * m1[5x1]) = prod1[3x1]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 1st Operation Layer = Multiplication\n", "prod1 = tf.matmul(x_data, m1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second operation is multiplication of prod1[3x1] by m2[1x1], which results in prod2[3x1]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 2nd Operation Layer = Multiplication\n", "prod2 = tf.matmul(prod1, m2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The third operation is matrix addition of prod2[3x1] to a1[1x1], This makes use of TensorFlow's broadcasting." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 3rd Operation Layer = Addition\n", "add1 = tf.add(prod2, a1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evaluate and Print Output" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 102.]\n", " [ 66.]\n", " [ 58.]]\n", "[[ 114.]\n", " [ 78.]\n", " [ 70.]]\n" ] } ], "source": [ "for x_val in x_vals:\n", " print(sess.run(add1, feed_dict={x_data: x_val}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create and Format Tensorboard outputs for viewing" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "merged = tf.summary.merge_all(key='summaries')\n", "\n", "if not os.path.exists('tensorboard_logs/'):\n", " os.makedirs('tensorboard_logs/')\n", "\n", "my_writer = tf.summary.FileWriter('tensorboard_logs/', sess.graph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![layering_nested_operations](https://github.com/nfmcclure/tensorflow_cookbook/raw/master/02_TensorFlow_Way/images/02_Multiple_Operations.png)" ] }, { "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.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }