"vscode:/vscode.git/clone" did not exist on "5feb6bc02207f0af650fe299c5ba2cc98bb2a0e1"
Unverified Commit bd1a74ab authored by Mark Daoust's avatar Mark Daoust Committed by GitHub
Browse files

Merge pull request #7 from lamberta/mark-autograph

Format, language updates
parents 2c91b186 3deea5d4
...@@ -3,15 +3,16 @@ ...@@ -3,15 +3,16 @@
"nbformat_minor": 0, "nbformat_minor": 0,
"metadata": { "metadata": {
"colab": { "colab": {
"name": "AutoGraph Guide.ipynb", "name": "autograph.ipynb",
"version": "0.3.2", "version": "0.3.2",
"views": {},
"default_view": {},
"provenance": [], "provenance": [],
"private_outputs": true, "private_outputs": true,
"collapsed_sections": [ "collapsed_sections": [
"Jxv6goXm7oGF" "Jxv6goXm7oGF"
], ],
"toc_visible": true, "toc_visible": true
"include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
"name": "python3", "name": "python3",
...@@ -19,16 +20,6 @@ ...@@ -19,16 +20,6 @@
} }
}, },
"cells": [ "cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/MarkDaoust/models/blob/autopgraph-guide/samples/core/guide/autograph_control_flow.ipynb)"
]
},
{ {
"metadata": { "metadata": {
"id": "Jxv6goXm7oGF", "id": "Jxv6goXm7oGF",
...@@ -45,11 +36,16 @@ ...@@ -45,11 +36,16 @@
"metadata": { "metadata": {
"id": "llMNufAK7nfK", "id": "llMNufAK7nfK",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "#@title Licensed under the Apache License, Version 2.0 (the \"License\"); { display-mode: \"form\" }\n",
"# you may not use this file except in compliance with the License.\n", "# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n", "# You may obtain a copy of the License at\n",
"#\n", "#\n",
...@@ -64,6 +60,16 @@ ...@@ -64,6 +60,16 @@
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{
"metadata": {
"id": "8Byow2J6LaPl",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# AutoGraph: Easy control flow for graphs "
]
},
{ {
"metadata": { "metadata": {
"id": "kGXS3UWBBNoc", "id": "kGXS3UWBBNoc",
...@@ -71,13 +77,17 @@ ...@@ -71,13 +77,17 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"# AutoGraph: Easy control flow for graphs \n", "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
"\n", " <td>\n",
"<table class=\"tfo-notebook-buttons\" align=\"left\"><td>\n", " <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/autograph\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/models/blob/master/samples/core/guide/autograph_control_flow.ipynb\">\n", " </td>\n",
" <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /><span>Run in Google Colab</span></a> \n", " <td>\n",
"</td><td>\n", " <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
"<a target=\"_blank\" href=\"https://github.com/tensorflow/models/blob/master/samples/core/guide/autograph_control_flow.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /><span>View source on GitHub</span></a></td></table>" " </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/models/blob/master/samples/core/guide/autograph.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
"</table>"
] ]
}, },
{ {
...@@ -87,7 +97,7 @@ ...@@ -87,7 +97,7 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"[AutoGraph](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/README.md) helps you write complicated graph code using just plain Python -- behind the scenes, AutoGraph automatically transforms your code into the equivalent TF graph code. We support a large chunk of the Python language, which is growing. [Please see this document for what we currently support, and what we're working on](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/LIMITATIONS.md)." "[AutoGraph](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/) helps you write complicated graph code using normal Python. Behind the scenes, AutoGraph automatically transforms your code into the equivalent [TensorFlow graph code](https://www.tensorflow.org/guide/graphs). AutoGraph already supports much of the Python language, and that coverage continues to grow. For a list of supported Python langauge features, see the [Autograph capabilities and limitations](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/autograph/LIMITATIONS.md)."
] ]
}, },
{ {
...@@ -97,24 +107,56 @@ ...@@ -97,24 +107,56 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"### Setup" "## Setup\n",
"\n",
"To use AutoGraph, install the latest version of TensorFlow:"
]
},
{
"metadata": {
"id": "RSez0n7Ptcvb",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
},
"cell_type": "code",
"source": [
"! pip install tf-nightly"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "qLp9VZfit9oR",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"Import TensorFlow, AutoGraph, and any supporting modules:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "mT7meGqrZTz9", "id": "mT7meGqrZTz9",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"! pip install tf-nightly\n",
"\n",
"from __future__ import division, print_function, absolute_import\n", "from __future__ import division, print_function, absolute_import\n",
"\n", "\n",
"import tensorflow as tf\n", "import tensorflow as tf\n",
"from tensorflow.contrib import autograph\n", "from tensorflow.contrib import autograph\n",
"tf.enable_eager_execution()\n",
"\n", "\n",
"import matplotlib.pyplot as plt" "import matplotlib.pyplot as plt"
], ],
...@@ -123,32 +165,56 @@ ...@@ -123,32 +165,56 @@
}, },
{ {
"metadata": { "metadata": {
"id": "ohbSnA79mcJV", "id": "Hh1PajmUJMNp",
"colab_type": "text" "colab_type": "text"
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## Automatically converting control flow\n", "We'll enable [eager execution](https://www.tensorflow.org/guide/eager) for demonstration purposes, but AutoGraph works in both eager and [graph execution](https://www.tensorflow.org/guide/graphs) environments:",
"\n", "",
"AutoGraph can convert a large chunk of the Python language into equivalent graph-construction code, and we're adding new supported language features all the time. In this section, we'll give you a taste of some of the functionality in AutoGraph.\n", "Important: The converted code will _work_ in eager mode. But it runs as an eager-python function. To run the resulting code in a graph use `tf.contrib.eager.defun`."
"AutoGraph will automatically convert most Python control flow statements into their correct graph equivalent. "
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "Ry0TlspBZVvf", "id": "ks_hiqcSJNvg",
"colab_type": "code",
"colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
},
"cell_type": "code",
"source": [
"tf.enable_eager_execution()"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "ohbSnA79mcJV",
"colab_type": "text" "colab_type": "text"
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"Here's a quick example of how it works. Autograph can convert functions like this:" "## Automatically convert Python control flow\n",
"\n",
"AutoGraph will convert much of the Python language into the equivalent TensorFlow graph building code. It converts a function like:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "aA3gOodCBkOw", "id": "aA3gOodCBkOw",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -169,14 +235,19 @@ ...@@ -169,14 +235,19 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"Into graph-building functions like this:" "To a function that uses graph building:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "_EMhGUjRZoKQ", "id": "_EMhGUjRZoKQ",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -192,14 +263,19 @@ ...@@ -192,14 +263,19 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"You can take code written for eager execution and run it in a `tf.Graph`. You get the same results, but with all the benfits of graphs:" "Code written for eager execution can run in a `tf.Graph` with the same results, but with the benfits of graph execution:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "I1RtBvoKBxq5", "id": "I1RtBvoKBxq5",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -222,7 +298,12 @@ ...@@ -222,7 +298,12 @@
"metadata": { "metadata": {
"id": "SGjSq0WQvwGs", "id": "SGjSq0WQvwGs",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -246,14 +327,19 @@ ...@@ -246,14 +327,19 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"We support common statements like `while`, `for`, `if`, `break`, `return` and more. You can even nest them as much as you like. Imagine trying to write the graph version of this code by hand:\n" "AutoGraph supports common Python statements like `while`, `for`, `if`, `break`, and `return`, with support for nesting. Compare this function with the complicated graph verson displayed in the following code blocks:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "toxKBOXbB1ro", "id": "toxKBOXbB1ro",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -281,7 +367,12 @@ ...@@ -281,7 +367,12 @@
"metadata": { "metadata": {
"id": "jlyQgxYsYSXr", "id": "jlyQgxYsYSXr",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -297,18 +388,21 @@ ...@@ -297,18 +388,21 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"Try replacing the `continue` in the above code with `break` -- AutoGraph supports that as well! \n",
"\n",
"## Decorator\n", "## Decorator\n",
"\n", "\n",
"If you don't need easy access to the original python function use the `convert` decorator:" "If you don't need easy access to the original Python function, use the `convert` decorator:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "BKhFNXDic4Mw", "id": "BKhFNXDic4Mw",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -322,58 +416,55 @@ ...@@ -322,58 +416,55 @@
" print('Buzz')\n", " print('Buzz')\n",
" else:\n", " else:\n",
" print(num)\n", " print(num)\n",
" return num" " return num\n",
], "\n",
"execution_count": 0, "\n",
"outputs": [] "with tf.Graph().as_default():\n",
},
{
"metadata": {
"id": "TUqkNkaadDgy",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"with tf.Graph().as_default(): \n",
" # The result works like a regular op: takes tensors in, returns tensors.\n", " # The result works like a regular op: takes tensors in, returns tensors.\n",
" # You can inspect the graph using tf.get_default_graph().as_graph_def()\n", " # You can inspect the graph using tf.get_default_graph().as_graph_def()\n",
" num = tf.placeholder(tf.int32)\n", " num = tf.placeholder(tf.int32)\n",
" result = fizzbuzz(num)\n", " result = fizzbuzz(num)\n",
" with tf.Session() as sess:\n", " with tf.Session() as sess:\n",
" for n in range(10,16):\n", " for n in range(10,16):\n",
" sess.run(result, feed_dict={num:n}) " " sess.run(result, feed_dict={num:n})"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{ {
"metadata": { "metadata": {
"id": "raExYNNZgibS", "id": "-pkEH6OecW7h",
"colab_type": "text" "colab_type": "text"
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## Examples" "## Examples\n",
"\n",
"Let's demonstrate some useful Python language features."
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "-pkEH6OecW7h", "id": "axoRAkWi0CQG",
"colab_type": "text" "colab_type": "text"
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"### Assert\n", "### Assert\n",
"\n", "\n",
"Let's try some other useful Python constructs, like `print` and `assert`. We automatically convert Python `assert` statements into the equivalent `tf.Assert` code. " "AutoGraph automatically converts the Python `assert` statement into the equivalent `tf.Assert` code:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "IAOgh62zCPZ4", "id": "IAOgh62zCPZ4",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -401,14 +492,19 @@ ...@@ -401,14 +492,19 @@
"source": [ "source": [
"### Print\n", "### Print\n",
"\n", "\n",
"You can also use plain Python `print` functions in in-graph" "Use the Python `print` function in-graph:"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "ySTsuxnqCTQi", "id": "ySTsuxnqCTQi",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -436,14 +532,19 @@ ...@@ -436,14 +532,19 @@
"source": [ "source": [
"### Lists\n", "### Lists\n",
"\n", "\n",
"Appending to lists in loops also works (we create tensor list ops for you behind the scenes)." "Append to lists in loops (tensor list ops are automatically created):"
] ]
}, },
{ {
"metadata": { "metadata": {
"id": "ABX070KwCczR", "id": "ABX070KwCczR",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -482,7 +583,12 @@ ...@@ -482,7 +583,12 @@
"metadata": { "metadata": {
"id": "4yyNOf-Twr6s", "id": "4yyNOf-Twr6s",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -517,7 +623,12 @@ ...@@ -517,7 +623,12 @@
"metadata": { "metadata": {
"id": "ucmZyQVL03bF", "id": "ucmZyQVL03bF",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -548,7 +659,12 @@ ...@@ -548,7 +659,12 @@
"metadata": { "metadata": {
"id": "1sjaFcL717Ig", "id": "1sjaFcL717Ig",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -572,16 +688,6 @@ ...@@ -572,16 +688,6 @@
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{
"metadata": {
"id": "hy99pRWpMcuN",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
""
]
},
{ {
"metadata": { "metadata": {
"id": "4LfnJjm0Bm0B", "id": "4LfnJjm0Bm0B",
...@@ -589,15 +695,15 @@ ...@@ -589,15 +695,15 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## Advanced example: A training loop in-graph\n", "## Advanced example: An in-graph training loop\n",
"\n", "\n",
"Writing control flow in AutoGraph is easy, so running a training loop in a TensorFlow graph should be easy as well! \n", "Since writing control flow in AutoGraph is easy, running a training loop in a TensorFlow graph should also be easy. \n",
"\n", "\n",
"<!--TODO(markdaoust) link to examples showing autograph **in** keras models when ready-->\n", "<!--TODO(markdaoust) link to examples showing autograph **in** keras models when ready-->\n",
"\n", "\n",
"Important: While this example wraps up a `tf.keras.Model` using autograph, `tf.contrib.autograph` is fully compatible with `tf.keras` and can be used in the definitions [custom of keras layers and models](http://tensorflow.org/guide/keras#build_advanced_models). The easiest way is to `@autograph.convert()` the `call` method.\n", "Important: While this example wraps a `tf.keras.Model` using AutoGraph, `tf.contrib.autograph` is compatible with `tf.keras` and can be used in [Keras custom layers and models](https://tensorflow.org/guide/keras#build_advanced_models). The easiest way is to `@autograph.convert()` the `call` method.\n",
"\n", "\n",
"Here, we show an example of training a simple Keras model on MNIST, where the entire training process -- loading batches, calculating gradients, updating parameters, calculating validation accuracy, and repeating until convergence -- is done in-graph." "This example shows how to train a simple Keras model on MNIST with the entire training processloading batches, calculating gradients, updating parameters, calculating validation accuracy, and repeating until convergence—is performed in-graph."
] ]
}, },
{ {
...@@ -614,7 +720,12 @@ ...@@ -614,7 +720,12 @@
"metadata": { "metadata": {
"id": "xqoxumv0ssQW", "id": "xqoxumv0ssQW",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -637,7 +748,12 @@ ...@@ -637,7 +748,12 @@
"metadata": { "metadata": {
"id": "Pe-erWQdBoC5", "id": "Pe-erWQdBoC5",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -702,7 +818,12 @@ ...@@ -702,7 +818,12 @@
"metadata": { "metadata": {
"id": "3xtg_MMhJETd", "id": "3xtg_MMhJETd",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
...@@ -765,7 +886,12 @@ ...@@ -765,7 +886,12 @@
"metadata": { "metadata": {
"id": "HYh6MSZyJOag", "id": "HYh6MSZyJOag",
"colab_type": "code", "colab_type": "code",
"colab": {} "colab": {
"autoexec": {
"startup": false,
"wait_interval": 0
}
}
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment