{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![book](https://raw.githubusercontent.com/ageron/tensorflow-safari-course/master/images/intro_to_tf_course.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Try not to peek at the solutions when you go through the exercises. ;-)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let's make sure this notebook works well in both Python 2 and Python 3:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import absolute_import, division, print_function, unicode_literals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# TensorFlow basics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "tf.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Construction Phase" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ ">>> a = tf.constant(3)\n", ">>> b = tf.constant(5)\n", ">>> s = a + b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tf.get_default_graph()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ ">>> graph = tf.Graph()\n", ">>> with graph.as_default():\n", "... a = tf.constant(3)\n", "... b = tf.constant(5)\n", "... s = a + b\n", "..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Execution Phase" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ ">>> with tf.Session(graph=graph) as sess:\n", "... result = s.eval()\n", "...\n", ">>> result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ ">>> with tf.Session(graph=graph) as sess:\n", "... result = sess.run(s)\n", "...\n", ">>> result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ ">>> with tf.Session(graph=graph) as sess:\n", "... result = sess.run([a,b,s])\n", "...\n", ">>> result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![Exercise](https://c1.staticflickr.com/9/8101/8553474140_c50cf08708_b.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.1) Create a simple graph that calculates $ c = \\exp(\\sqrt 8 + 3) $.\n", "\n", "**Tip**: TensorFlow's API documentation is available at:\n", "https://www.tensorflow.org/versions/master/api_docs/python/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.2) Now create a `Session()` and evaluate the operation that gives you the result of the equation above:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.3) Create a graph that evaluates and prints both $ b = \\sqrt 8 $ and $ c = \\exp(\\sqrt 8 + 3) $. Try to implement this in a way that only evaluates $ \\sqrt 8 $ once." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.4) The following code is needed to display TensorFlow graphs in Jupyter. Just run this cell then visualize your graph by calling `show_graph(`_your graph_`)`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from IPython.display import display, HTML\n", "\n", "def strip_consts(graph_def, max_const_size=32):\n", " \"\"\"Strip large constant values from graph_def.\"\"\"\n", " strip_def = tf.GraphDef()\n", " for n0 in graph_def.node:\n", " n = strip_def.node.add() \n", " n.MergeFrom(n0)\n", " if n.op == 'Const':\n", " tensor = n.attr['value'].tensor\n", " size = len(tensor.tensor_content)\n", " if size > max_const_size:\n", " tensor.tensor_content = b\"\"%size\n", " return strip_def\n", "\n", "def show_graph(graph_def=None, max_const_size=32):\n", " \"\"\"Visualize TensorFlow graph.\"\"\"\n", " graph_def = graph_def or tf.get_default_graph()\n", " if hasattr(graph_def, 'as_graph_def'):\n", " graph_def = graph_def.as_graph_def()\n", " strip_def = strip_consts(graph_def, max_const_size=max_const_size)\n", " code = \"\"\"\n", " \n", " \n", "
\n", " \n", "
\n", " \"\"\".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))\n", "\n", " iframe = \"\"\"\n", " \n", " \"\"\".format(code.replace('\"', '"'))\n", " display(HTML(iframe))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try not to peek at the solution below before you have done the exercise! :)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![thinking](https://upload.wikimedia.org/wikipedia/commons/0/06/Filos_segundo_logo_%28flipped%29.jpg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1 - Solution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = tf.Graph()\n", "with graph.as_default():\n", " c = tf.exp(tf.add(tf.sqrt(tf.constant(8.)), tf.constant(3.)))\n", " # or simply...\n", " c = tf.exp(tf.sqrt(8.) + 3.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tf.Session(graph=graph):\n", " c_val = c.eval()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c_val" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = tf.Graph()\n", "with graph.as_default():\n", " b = tf.sqrt(8.)\n", " c = tf.exp(b + 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with tf.Session(graph=graph) as sess:\n", " b_val, c_val = sess.run([b, c])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b_val" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c_val" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Important**: the following implementation gives the right result, but it runs the graph twice, once to evaluate `b`, and once to evaluate `c`. Since `c` depends on `b`, it means that `b` will be evaluated twice. Not what we wanted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# WRONG!\n", "with tf.Session(graph=graph):\n", " b_val = b.eval() # evaluates b\n", " c_val = c.eval() # evaluates c, which means evaluating b again!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b_val" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "c_val" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_graph(graph)" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }