{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This work comes from [LearningTensorFlow.com](http://learningtensorflow.com/), developed by [dataPipeline](http://datapipeline.co.au/), with whom the copyright remains. \n", "For more tutorials and extended exercises, please see the website." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What does TensorFlow do?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TensorFlow is a way of representing computation without actually performing it until asked. In this sense, it is a form of lazy computing, and it allows for some great improvements to the running of code:\n", "\n", "- Faster computation of complex variables\n", "- Distributed computation across multiple systems, including GPUs.\n", "- Reduced redundency in some computations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let’s have a look at this in action. First, a very basic python script:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40\n" ] } ], "source": [ "x = 35\n", "y = x + 5\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This script basically just says “create a variable x with value 35, set the value of a new variable y to that plus 5, which is currently 40, and print it out”. The value 40 will print out when you run this program. If you aren’t familiar with python, create a new text file called `basic_script.py`, and copy that code in. Save it on your computer and run it with:\n", "\n", "```bash\n", "python basic_script.py\n", "```\n", "\n", "Note that the path (i.e. `basic_script.py`) must reference the file, so if it is in the Code folder, you use:\n", "\n", "```\n", "python Code/basic_script.py\n", "```\n", "\n", "Also, make sure you have activated the Anaconda virtual environment. On Linux, this will make your prompt look something like:\n", "\n", "```\n", "(tensorenv)username@computername:~$\n", "```\n", "\n", "If that is working, let’s convert it to a TensorFlow equivalent." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import tensorflow as tf\n", "\n", "x = tf.constant(35, name='x')\n", "y = tf.Variable(x + 5, name='y')\n", "\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After running this, you’ll get quite a funny output, something like ``````. This is clearly not the value 40.\n", "\n", "The reason why, is that our program actually does something quite different to the previous one. The code here does the following:\n", "\n", "- Import the tensorflow module and call it `tf`\n", "- Create a constant value called x, and give it the numerical value 35\n", "- Create a Variable called y, and define it as being the equation x + 5\n", "- Print out the equation object for y\n", "\n", "The subtle difference is that y isn’t given “the current value of x + 5” as in our previous program. Instead, it is effectively an equation that means “when this variable is computed, take the value of x (as it is then) and add 5 to it”. The computation of the value of y is never actually performed in the above program.\n", "\n", "Let’s fix that:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40\n" ] } ], "source": [ "import tensorflow as tf\n", "\n", "x = tf.constant(35, name='x')\n", "y = tf.Variable(x + 5, name='y')\n", "\n", "model = tf.initialize_all_variables()\n", "\n", "with tf.Session() as session:\n", " session.run(model)\n", " print(session.run(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have removed the `print(y)` statement, and instead we have code that creates a session, and actually computes the value of y. This is quite a bit of boilerplate, but it works like this:\n", "\n", "1. Import the tensorflow module and call it `tf`\n", "2. Create a constant value called x, and give it the numerical value 35\n", "3. Create a Variable called y, and define it as being the equation x + 5\n", "4. Initialize the variables with `initialize_all_variables` (we will go into more detail on this)\n", "5. Create a session for computing the values\n", "6. Run the model created in 4\n", "7. Run just the variable y and print out its current value\n", "\n", "The step 4 above is where some magic happens. In this step, a graph is created of the dependencies between the variables. In this case, the variable y depends on the variable x, and that value is transformed by adding 5 to it. Keep in mind that this value isn’t computed until step 7, as up until then, only equations and relations are computed." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" }, "toc": { "toc_cell": false, "toc_number_sections": false, "toc_section_display": "none", "toc_threshold": "8", "toc_window_display": true }, "toc_position": { "height": "86px", "left": "1331.45px", "right": "20px", "top": "120px", "width": "164px" } }, "nbformat": 4, "nbformat_minor": 0 }