Skip to content

Commit e1be4e9

Browse files
committed
Resolving review comments.
1 parent e3a6768 commit e1be4e9

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

samples/core/guide/autograph_control_flow.ipynb

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"collapsed_sections": [
1111
"Jxv6goXm7oGF"
1212
],
13+
"toc_visible": true,
1314
"include_colab_link": true
1415
},
1516
"kernelspec": {
@@ -89,6 +90,16 @@
8990
"[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)."
9091
]
9192
},
93+
{
94+
"metadata": {
95+
"id": "n4EKOpw9mObL",
96+
"colab_type": "text"
97+
},
98+
"cell_type": "markdown",
99+
"source": [
100+
"### Setup"
101+
]
102+
},
92103
{
93104
"metadata": {
94105
"id": "mT7meGqrZTz9",
@@ -103,12 +114,26 @@
103114
"\n",
104115
"import tensorflow as tf\n",
105116
"from tensorflow.contrib import autograph\n",
117+
"tf.enable_eager_execution()\n",
106118
"\n",
107119
"import matplotlib.pyplot as plt"
108120
],
109121
"execution_count": 0,
110122
"outputs": []
111123
},
124+
{
125+
"metadata": {
126+
"id": "ohbSnA79mcJV",
127+
"colab_type": "text"
128+
},
129+
"cell_type": "markdown",
130+
"source": [
131+
"## Automatically converting control flow\n",
132+
"\n",
133+
"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",
134+
"AutoGraph will automatically convert most Python control flow statements into their correct graph equivalent. "
135+
]
136+
},
112137
{
113138
"metadata": {
114139
"id": "Ry0TlspBZVvf",
@@ -144,7 +169,7 @@
144169
},
145170
"cell_type": "markdown",
146171
"source": [
147-
"Into graph-compatible functions like this:"
172+
"Into graph-building functions like this:"
148173
]
149174
},
150175
{
@@ -167,7 +192,7 @@
167192
},
168193
"cell_type": "markdown",
169194
"source": [
170-
"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:"
195+
"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:"
171196
]
172197
},
173198
{
@@ -178,7 +203,7 @@
178203
},
179204
"cell_type": "code",
180205
"source": [
181-
"print('Original value: %2.2f' % g(9.0)) "
206+
"print('Eager results: %2.2f, %2.2f' % (g(tf.constant(9.0)), g(tf.constant(-9.0))))"
182207
],
183208
"execution_count": 0,
184209
"outputs": []
@@ -206,9 +231,10 @@
206231
"with tf.Graph().as_default(): \n",
207232
" # The result works like a regular op: takes tensors in, returns tensors.\n",
208233
" # You can inspect the graph using tf.get_default_graph().as_graph_def()\n",
209-
" g_ops = tf_g(tf.constant(9.0))\n",
234+
" g_out1 = tf_g(tf.constant( 9.0))\n",
235+
" g_out2 = tf_g(tf.constant(-9.0))\n",
210236
" with tf.Session() as sess:\n",
211-
" print('Autograph value: %2.2f\\n' % sess.run(g_ops)) "
237+
" print('Graph results: %2.2f, %2.2f\\n' % (sess.run(g_out1), sess.run(g_out2)))"
212238
],
213239
"execution_count": 0,
214240
"outputs": []
@@ -220,12 +246,6 @@
220246
},
221247
"cell_type": "markdown",
222248
"source": [
223-
"## Automatically converting control flow\n",
224-
"\n",
225-
"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",
226-
"AutoGraph will automatically convert most Python control flow statements into their correct graph equivalent. \n",
227-
" \n",
228-
"\n",
229249
"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"
230250
]
231251
},
@@ -246,13 +266,13 @@
246266
" s += c\n",
247267
" return s\n",
248268
"\n",
249-
"print('Original value: %d' % f([10,12,15,20]))\n",
269+
"print('Eager result: %d' % f(tf.constant([10,12,15,20])))\n",
250270
"\n",
251271
"tf_f = autograph.to_graph(f)\n",
252272
"\n",
253273
"with tf.Graph().as_default(): \n",
254274
" with tf.Session():\n",
255-
" print('Graph value: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())"
275+
" print('Graph result: %d\\n\\n' % tf_f(tf.constant([10,12,15,20])).eval())"
256276
],
257277
"execution_count": 0,
258278
"outputs": []
@@ -318,20 +338,25 @@
318338
"with tf.Graph().as_default(): \n",
319339
" # The result works like a regular op: takes tensors in, returns tensors.\n",
320340
" # You can inspect the graph using tf.get_default_graph().as_graph_def()\n",
321-
" input = tf.placeholder(tf.int32)\n",
322-
" result = fizzbuzz(input)\n",
341+
" num = tf.placeholder(tf.int32)\n",
342+
" result = fizzbuzz(num)\n",
323343
" with tf.Session() as sess:\n",
324-
" sess.run(result, feed_dict={input:10}) \n",
325-
" sess.run(result, feed_dict={input:11}) \n",
326-
" sess.run(result, feed_dict={input:12}) \n",
327-
" sess.run(result, feed_dict={input:13}) \n",
328-
" sess.run(result, feed_dict={input:14}) \n",
329-
" sess.run(result, feed_dict={input:15}) \n",
330-
" "
344+
" for n in range(10,16):\n",
345+
" sess.run(result, feed_dict={num:n}) "
331346
],
332347
"execution_count": 0,
333348
"outputs": []
334349
},
350+
{
351+
"metadata": {
352+
"id": "raExYNNZgibS",
353+
"colab_type": "text"
354+
},
355+
"cell_type": "markdown",
356+
"source": [
357+
"## Examples"
358+
]
359+
},
335360
{
336361
"metadata": {
337362
"id": "-pkEH6OecW7h",
@@ -352,18 +377,17 @@
352377
},
353378
"cell_type": "code",
354379
"source": [
380+
"@autograph.convert()\n",
355381
"def f(x):\n",
356382
" assert x != 0, 'Do not pass zero!'\n",
357383
" return x * x\n",
358384
"\n",
359-
"tf_f = autograph.to_graph(f)\n",
360-
"\n",
361385
"with tf.Graph().as_default(): \n",
362386
" with tf.Session():\n",
363387
" try:\n",
364388
" print(tf_f(tf.constant(0)).eval())\n",
365389
" except tf.errors.InvalidArgumentError as e:\n",
366-
" print('Got error message:\\n%s' % e.message)"
390+
" print('Got error message:\\n %s' % e.message)"
367391
],
368392
"execution_count": 0,
369393
"outputs": []
@@ -412,7 +436,7 @@
412436
"source": [
413437
"### Lists\n",
414438
"\n",
415-
"Appending to lists in loops also works (we create a `TensorArray` for you behind the scenes)"
439+
"Appending to lists in loops also works (we create tensor list ops for you behind the scenes)."
416440
]
417441
},
418442
{
@@ -423,6 +447,7 @@
423447
},
424448
"cell_type": "code",
425449
"source": [
450+
"@autograph.convert()\n",
426451
"def f(n):\n",
427452
" z = []\n",
428453
" # We ask you to tell us the element dtype of the list\n",
@@ -433,11 +458,11 @@
433458
" # (this is just like np.stack)\n",
434459
" return autograph.stack(z) \n",
435460
"\n",
436-
"tf_f = autograph.to_graph(f)\n",
461+
"#tf_f = autograph.to_graph(f)\n",
437462
"\n",
438463
"with tf.Graph().as_default(): \n",
439464
" with tf.Session():\n",
440-
" print(tf_f(tf.constant(3)).eval())"
465+
" print(f(tf.constant(3)).eval())"
441466
],
442467
"execution_count": 0,
443468
"outputs": []
@@ -515,7 +540,7 @@
515540
},
516541
"cell_type": "markdown",
517542
"source": [
518-
"### Break from loop"
543+
"### Break"
519544
]
520545
},
521546
{
@@ -765,6 +790,19 @@
765790
],
766791
"execution_count": 0,
767792
"outputs": []
793+
},
794+
{
795+
"metadata": {
796+
"id": "ZpEIfs5jn6jw",
797+
"colab_type": "code",
798+
"colab": {}
799+
},
800+
"cell_type": "code",
801+
"source": [
802+
""
803+
],
804+
"execution_count": 0,
805+
"outputs": []
768806
}
769807
]
770808
}

0 commit comments

Comments
 (0)