|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Basic introduction to TensorFlow's Eager API\n", |
| 8 | + "\n", |
| 9 | + "A simple introduction to get started with TensorFlow's Eager API.\n", |
| 10 | + "\n", |
| 11 | + "- Author: Aymeric Damien\n", |
| 12 | + "- Project: https://github.com/aymericdamien/TensorFlow-Examples/" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "### What is TensorFlow's Eager API ?\n", |
| 20 | + "\n", |
| 21 | + "*Eager execution is an imperative, define-by-run interface where operations are\n", |
| 22 | + "executed immediately as they are called from Python. This makes it easier to\n", |
| 23 | + "get started with TensorFlow, and can make research and development more\n", |
| 24 | + "intuitive. A vast majority of the TensorFlow API remains the same whether eager\n", |
| 25 | + "execution is enabled or not. As a result, the exact same code that constructs\n", |
| 26 | + "TensorFlow graphs (e.g. using the layers API) can be executed imperatively\n", |
| 27 | + "by using eager execution. Conversely, most models written with Eager enabled\n", |
| 28 | + "can be converted to a graph that can be further optimized and/or extracted\n", |
| 29 | + "for deployment in production without changing code. - Rajat Monga*\n", |
| 30 | + "\n", |
| 31 | + "More info: https://research.googleblog.com/2017/10/eager-execution-imperative-define-by.html" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 1, |
| 37 | + "metadata": { |
| 38 | + "collapsed": true |
| 39 | + }, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "from __future__ import absolute_import, division, print_function\n", |
| 43 | + "\n", |
| 44 | + "import numpy as np\n", |
| 45 | + "import tensorflow as tf\n", |
| 46 | + "import tensorflow.contrib.eager as tfe" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 2, |
| 52 | + "metadata": { |
| 53 | + "collapsed": false |
| 54 | + }, |
| 55 | + "outputs": [ |
| 56 | + { |
| 57 | + "name": "stdout", |
| 58 | + "output_type": "stream", |
| 59 | + "text": [ |
| 60 | + "Setting Eager mode...\n" |
| 61 | + ] |
| 62 | + } |
| 63 | + ], |
| 64 | + "source": [ |
| 65 | + "# Set Eager API\n", |
| 66 | + "print(\"Setting Eager mode...\")\n", |
| 67 | + "tfe.enable_eager_execution()" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 3, |
| 73 | + "metadata": { |
| 74 | + "collapsed": false |
| 75 | + }, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "name": "stdout", |
| 79 | + "output_type": "stream", |
| 80 | + "text": [ |
| 81 | + "Define constant tensors\n", |
| 82 | + "a = 2\n", |
| 83 | + "b = 3\n" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "# Define constant tensors\n", |
| 89 | + "print(\"Define constant tensors\")\n", |
| 90 | + "a = tf.constant(2)\n", |
| 91 | + "print(\"a = %i\" % a)\n", |
| 92 | + "b = tf.constant(3)\n", |
| 93 | + "print(\"b = %i\" % b)" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": 4, |
| 99 | + "metadata": { |
| 100 | + "collapsed": false |
| 101 | + }, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "name": "stdout", |
| 105 | + "output_type": "stream", |
| 106 | + "text": [ |
| 107 | + "Running operations, without tf.Session\n", |
| 108 | + "a + b = 5\n", |
| 109 | + "a * b = 6\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "# Run the operation without the need for tf.Session\n", |
| 115 | + "print(\"Running operations, without tf.Session\")\n", |
| 116 | + "c = a + b\n", |
| 117 | + "print(\"a + b = %i\" % c)\n", |
| 118 | + "d = a * b\n", |
| 119 | + "print(\"a * b = %i\" % d)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 5, |
| 125 | + "metadata": { |
| 126 | + "collapsed": false |
| 127 | + }, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stdout", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "Mixing operations with Tensors and Numpy Arrays\n", |
| 134 | + "Tensor:\n", |
| 135 | + " a = tf.Tensor(\n", |
| 136 | + "[[2. 1.]\n", |
| 137 | + " [1. 0.]], shape=(2, 2), dtype=float32)\n", |
| 138 | + "NumpyArray:\n", |
| 139 | + " b = [[3. 0.]\n", |
| 140 | + " [5. 1.]]\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "source": [ |
| 145 | + "# Full compatibility with Numpy\n", |
| 146 | + "print(\"Mixing operations with Tensors and Numpy Arrays\")\n", |
| 147 | + "\n", |
| 148 | + "# Define constant tensors\n", |
| 149 | + "a = tf.constant([[2., 1.],\n", |
| 150 | + " [1., 0.]], dtype=tf.float32)\n", |
| 151 | + "print(\"Tensor:\\n a = %s\" % a)\n", |
| 152 | + "b = np.array([[3., 0.],\n", |
| 153 | + " [5., 1.]], dtype=np.float32)\n", |
| 154 | + "print(\"NumpyArray:\\n b = %s\" % b)" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 6, |
| 160 | + "metadata": { |
| 161 | + "collapsed": false |
| 162 | + }, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdout", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + "Running operations, without tf.Session\n", |
| 169 | + "a + b = tf.Tensor(\n", |
| 170 | + "[[5. 1.]\n", |
| 171 | + " [6. 1.]], shape=(2, 2), dtype=float32)\n", |
| 172 | + "a * b = tf.Tensor(\n", |
| 173 | + "[[11. 1.]\n", |
| 174 | + " [ 3. 0.]], shape=(2, 2), dtype=float32)\n" |
| 175 | + ] |
| 176 | + } |
| 177 | + ], |
| 178 | + "source": [ |
| 179 | + "# Run the operation without the need for tf.Session\n", |
| 180 | + "print(\"Running operations, without tf.Session\")\n", |
| 181 | + "\n", |
| 182 | + "c = a + b\n", |
| 183 | + "print(\"a + b = %s\" % c)\n", |
| 184 | + "\n", |
| 185 | + "d = tf.matmul(a, b)\n", |
| 186 | + "print(\"a * b = %s\" % d)" |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": 7, |
| 192 | + "metadata": { |
| 193 | + "collapsed": false |
| 194 | + }, |
| 195 | + "outputs": [ |
| 196 | + { |
| 197 | + "name": "stdout", |
| 198 | + "output_type": "stream", |
| 199 | + "text": [ |
| 200 | + "Iterate through Tensor 'a':\n", |
| 201 | + "tf.Tensor(2.0, shape=(), dtype=float32)\n", |
| 202 | + "tf.Tensor(1.0, shape=(), dtype=float32)\n", |
| 203 | + "tf.Tensor(1.0, shape=(), dtype=float32)\n", |
| 204 | + "tf.Tensor(0.0, shape=(), dtype=float32)\n" |
| 205 | + ] |
| 206 | + } |
| 207 | + ], |
| 208 | + "source": [ |
| 209 | + "print(\"Iterate through Tensor 'a':\")\n", |
| 210 | + "for i in range(a.shape[0]):\n", |
| 211 | + " for j in range(a.shape[1]):\n", |
| 212 | + " print(a[i][j])" |
| 213 | + ] |
| 214 | + } |
| 215 | + ], |
| 216 | + "metadata": { |
| 217 | + "anaconda-cloud": {}, |
| 218 | + "kernelspec": { |
| 219 | + "display_name": "Python [default]", |
| 220 | + "language": "python", |
| 221 | + "name": "python2" |
| 222 | + }, |
| 223 | + "language_info": { |
| 224 | + "codemirror_mode": { |
| 225 | + "name": "ipython", |
| 226 | + "version": 2 |
| 227 | + }, |
| 228 | + "file_extension": ".py", |
| 229 | + "mimetype": "text/x-python", |
| 230 | + "name": "python", |
| 231 | + "nbconvert_exporter": "python", |
| 232 | + "pygments_lexer": "ipython2", |
| 233 | + "version": "2.7.12" |
| 234 | + } |
| 235 | + }, |
| 236 | + "nbformat": 4, |
| 237 | + "nbformat_minor": 1 |
| 238 | +} |
0 commit comments