Commit e1be4e9d authored by Mark Daoust's avatar Mark Daoust
Browse files

Resolving review comments.

parent e3a6768a
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"collapsed_sections": [ "collapsed_sections": [
"Jxv6goXm7oGF" "Jxv6goXm7oGF"
], ],
"toc_visible": true,
"include_colab_link": true "include_colab_link": true
}, },
"kernelspec": { "kernelspec": {
...@@ -89,6 +90,16 @@ ...@@ -89,6 +90,16 @@
"[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/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)."
] ]
}, },
{
"metadata": {
"id": "n4EKOpw9mObL",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"### Setup"
]
},
{ {
"metadata": { "metadata": {
"id": "mT7meGqrZTz9", "id": "mT7meGqrZTz9",
...@@ -103,12 +114,26 @@ ...@@ -103,12 +114,26 @@
"\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"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{
"metadata": {
"id": "ohbSnA79mcJV",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Automatically converting control flow\n",
"\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",
"AutoGraph will automatically convert most Python control flow statements into their correct graph equivalent. "
]
},
{ {
"metadata": { "metadata": {
"id": "Ry0TlspBZVvf", "id": "Ry0TlspBZVvf",
...@@ -144,7 +169,7 @@ ...@@ -144,7 +169,7 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"Into graph-compatible functions like this:" "Into graph-building functions like this:"
] ]
}, },
{ {
...@@ -167,7 +192,7 @@ ...@@ -167,7 +192,7 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"You can take code written for eager execution and run it in graph mode. You get the same results, but with all the benfits of graphs:" "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:"
] ]
}, },
{ {
...@@ -178,7 +203,7 @@ ...@@ -178,7 +203,7 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"print('Original value: %2.2f' % g(9.0)) " "print('Eager results: %2.2f, %2.2f' % (g(tf.constant(9.0)), g(tf.constant(-9.0))))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -206,9 +231,10 @@ ...@@ -206,9 +231,10 @@
"with tf.Graph().as_default(): \n", "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",
" g_ops = tf_g(tf.constant(9.0))\n", " g_out1 = tf_g(tf.constant( 9.0))\n",
" g_out2 = tf_g(tf.constant(-9.0))\n",
" with tf.Session() as sess:\n", " with tf.Session() as sess:\n",
" print('Autograph value: %2.2f\\n' % sess.run(g_ops)) " " print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -220,12 +246,6 @@ ...@@ -220,12 +246,6 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## Automatically converting control flow\n",
"\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",
"AutoGraph will automatically convert most Python control flow statements into their correct graph equivalent. \n",
" \n",
"\n",
"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" "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"
] ]
}, },
...@@ -246,13 +266,13 @@ ...@@ -246,13 +266,13 @@
" s += c\n", " s += c\n",
" return s\n", " return s\n",
"\n", "\n",
"print('Original value: %d' % f([10,12,15,20]))\n", "print('Eager result: %d' % f(tf.constant([10,12,15,20])))\n",
"\n", "\n",
"tf_f = autograph.to_graph(f)\n", "tf_f = autograph.to_graph(f)\n",
"\n", "\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" print('Graph value: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())" " print('Graph result: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -318,20 +338,25 @@ ...@@ -318,20 +338,25 @@
"with tf.Graph().as_default(): \n", "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",
" input = tf.placeholder(tf.int32)\n", " num = tf.placeholder(tf.int32)\n",
" result = fizzbuzz(input)\n", " result = fizzbuzz(num)\n",
" with tf.Session() as sess:\n", " with tf.Session() as sess:\n",
" sess.run(result, feed_dict={input:10}) \n", " for n in range(10,16):\n",
" sess.run(result, feed_dict={input:11}) \n", " sess.run(result, feed_dict={num:n}) "
" sess.run(result, feed_dict={input:12}) \n",
" sess.run(result, feed_dict={input:13}) \n",
" sess.run(result, feed_dict={input:14}) \n",
" sess.run(result, feed_dict={input:15}) \n",
" "
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
}, },
{
"metadata": {
"id": "raExYNNZgibS",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"## Examples"
]
},
{ {
"metadata": { "metadata": {
"id": "-pkEH6OecW7h", "id": "-pkEH6OecW7h",
...@@ -352,18 +377,17 @@ ...@@ -352,18 +377,17 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"@autograph.convert()\n",
"def f(x):\n", "def f(x):\n",
" assert x != 0, 'Do not pass zero!'\n", " assert x != 0, 'Do not pass zero!'\n",
" return x * x\n", " return x * x\n",
"\n", "\n",
"tf_f = autograph.to_graph(f)\n",
"\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" try:\n", " try:\n",
" print(tf_f(tf.constant(0)).eval())\n", " print(tf_f(tf.constant(0)).eval())\n",
" except tf.errors.InvalidArgumentError as e:\n", " except tf.errors.InvalidArgumentError as e:\n",
" print('Got error message:\\n%s' % e.message)" " print('Got error message:\\n %s' % e.message)"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -412,7 +436,7 @@ ...@@ -412,7 +436,7 @@
"source": [ "source": [
"### Lists\n", "### Lists\n",
"\n", "\n",
"Appending to lists in loops also works (we create a `TensorArray` for you behind the scenes)" "Appending to lists in loops also works (we create tensor list ops for you behind the scenes)."
] ]
}, },
{ {
...@@ -423,6 +447,7 @@ ...@@ -423,6 +447,7 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"@autograph.convert()\n",
"def f(n):\n", "def f(n):\n",
" z = []\n", " z = []\n",
" # We ask you to tell us the element dtype of the list\n", " # We ask you to tell us the element dtype of the list\n",
...@@ -433,11 +458,11 @@ ...@@ -433,11 +458,11 @@
" # (this is just like np.stack)\n", " # (this is just like np.stack)\n",
" return autograph.stack(z) \n", " return autograph.stack(z) \n",
"\n", "\n",
"tf_f = autograph.to_graph(f)\n", "#tf_f = autograph.to_graph(f)\n",
"\n", "\n",
"with tf.Graph().as_default(): \n", "with tf.Graph().as_default(): \n",
" with tf.Session():\n", " with tf.Session():\n",
" print(tf_f(tf.constant(3)).eval())" " print(f(tf.constant(3)).eval())"
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
...@@ -515,7 +540,7 @@ ...@@ -515,7 +540,7 @@
}, },
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"### Break from loop" "### Break"
] ]
}, },
{ {
...@@ -765,6 +790,19 @@ ...@@ -765,6 +790,19 @@
], ],
"execution_count": 0, "execution_count": 0,
"outputs": [] "outputs": []
},
{
"metadata": {
"id": "ZpEIfs5jn6jw",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
} }
] ]
} }
\ No newline at end of file
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