{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"
To use TensorFlow, we need to import the library. We imported it and optionally gave it the name \"tf\", so the modules can be accessed by __tf.module-name__:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Building a Graph\n", "\n", "As we said before, TensorFlow works as a graph computational model. Let's create our first graph.\n", "\n", "To create our first graph we will utilize __source operations__, which do not need any information input. These source operations or __source ops__ will pass their information to other operations which will execute computations.\n", "\n", "To create two source operations which will output numbers we will define two constants:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = tf.constant([2])\n", "b = tf.constant([3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After that, let's make an operation over these variables. The function __tf.add()__ adds two elements (you could also use `c = a + b`). " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = tf.add(a,b)\n", "#c = a + b is also a way to define the sum of the terms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then TensorFlow needs to initialize a session to run our code. Sessions are, in a way, a context for creating a graph inside TensorFlow. Let's define our session:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "session = tf.Session()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's run the session to get the result from the previous defined 'c' operation:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5]\n" ] } ], "source": [ "result = session.run(c)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Close the session to release resources:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "session.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "To avoid having to close sessions every time, we can define them in a __with__ block, so after running the __with__ block the session will close automatically:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5]\n" ] } ], "source": [ "with tf.Session() as session:\n", " result = session.run(c)\n", " print(result)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Even this silly example of adding 2 constants to reach a simple result defines the basis of TensorFlow. Define your edge (In this case our constants), include nodes (operations, like _tf.add_), and start a session to build a graph.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### What is the meaning of Tensor?\n", "\n", "
Dimension | \n", "Physical Representation | \n", "Mathematical Object | \n", "In Code | \n", "
Zero | \n", "Point | \n", "Scalar (Single Number) | \n", "[ 1 ] | \n", "
One | \n", "Line | \n", "Vector (Series of Numbers) | \n", "[ 1,2,3,4,... ] | \n", "
Two | \n", "Surface | \n", "Matrix (Table of Numbers) | \n", "[ [1,2,3,4,...], [1,2,3,4,...], [1,2,3,4,...],... ] | \n", "
Three | \n", "Volume | \n", "Tensor (Cube of Numbers) | \n", "[ [[1,2,...], [1,2,...], [1,2,...],...], [[1,2,...], [1,2,...], [1,2,...],...], [[1,2,...], [1,2,...], [1,2,...] ,...]... ] | \n", "