You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: samples/core/guide/autograph.ipynb
+27-27
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,8 @@
13
13
"toc_visible": true
14
14
},
15
15
"kernelspec": {
16
-
"name": "python3",
17
-
"display_name": "Python 3"
16
+
"name": "python2",
17
+
"display_name": "Python 2"
18
18
}
19
19
},
20
20
"cells": [
@@ -191,7 +191,11 @@
191
191
"source": [
192
192
"## Automatically convert Python control flow\n",
193
193
"\n",
194
-
"AutoGraph will convert much of the Python language into the equivalent TensorFlow graph building code. It converts a function like:"
194
+
"AutoGraph will convert much of the Python language into the equivalent TensorFlow graph building code. \n",
195
+
"\n",
196
+
"Note: In real applications batching is essential for performance. The best code to convert to AutoGraph is code where the control flow is decided at the _batch_ level. If making decisions at the individual _example_ level, you must index and batch the examples to maintain performance while applying the control flow logic. \n",
"Let's demonstrate some useful Python language features."
399
+
"Let's demonstrate some useful Python language features.\n"
397
400
]
398
401
},
399
402
{
@@ -419,14 +422,13 @@
419
422
"@autograph.convert()\n",
420
423
"def inverse(x):\n",
421
424
" assert x != 0.0, 'Do not pass zero!'\n",
422
-
" return 1.0/x\n",
425
+
" return 1.0 / x\n",
423
426
"\n",
424
-
"with tf.Graph().as_default(): \n",
425
-
" with tf.Session():\n",
426
-
" try:\n",
427
-
" print(inverse(tf.constant(0.0)).eval())\n",
428
-
" except tf.errors.InvalidArgumentError as e:\n",
429
-
" print('Got error message:\\n %s' % e.message)"
427
+
"with tf.Graph().as_default(), tf.Session() as sess:\n",
428
+
" try:\n",
429
+
" print(sess.run(inverse(tf.constant(0.0))))\n",
430
+
" except tf.errors.InvalidArgumentError as e:\n",
431
+
" print('Got error message:\\n %s' % e.message)"
430
432
],
431
433
"execution_count": 0,
432
434
"outputs": []
@@ -459,9 +461,8 @@
459
461
" i += 1\n",
460
462
" return n\n",
461
463
"\n",
462
-
"with tf.Graph().as_default():\n",
463
-
" with tf.Session():\n",
464
-
" count(tf.constant(5)).eval()"
464
+
"with tf.Graph().as_default(), tf.Session() as sess:\n",
465
+
" sess.run(count(tf.constant(5)))"
465
466
],
466
467
"execution_count": 0,
467
468
"outputs": []
@@ -499,9 +500,8 @@
499
500
" return autograph.stack(z) \n",
500
501
"\n",
501
502
"\n",
502
-
"with tf.Graph().as_default(): \n",
503
-
" with tf.Session():\n",
504
-
" print(arange(tf.constant(10)).eval())"
503
+
"with tf.Graph().as_default(), tf.Session() as sess:\n",
504
+
" sess.run(arange(tf.constant(10)))"
505
505
],
506
506
"execution_count": 0,
507
507
"outputs": []
@@ -655,14 +655,14 @@
655
655
"source": [
656
656
"## Interoperation with `tf.Keras`\n",
657
657
"\n",
658
-
"Now that you've seen the basics, let's build some real model components with autograph.\n",
658
+
"Now that you've seen the basics, let's build some model components with autograph.\n",
659
659
"\n",
660
-
"It's relatively simple to integrate `autograph` with `tf.keras`. But remember that batchng is essential for performance. So the best candidate code for conversion to autograph is code where the control flow is decided at the _batch_ level. If decisions are made at the individual _example_ level you will still need to index and batch your examples to maintain performance while appling the control flow logic. \n",
660
+
"It's relatively simple to integrate `autograph` with `tf.keras`. \n",
661
661
"\n",
662
662
"\n",
663
663
"### Stateless functions\n",
664
664
"\n",
665
-
"For stateless functions like `collatz`, below, the easiest way to include them in a keras model is to wrap them up as a layer uisng `tf.keras.layers.Lambda`."
665
+
"For stateless functions, like `collatz` shown below, the easiest way to include them in a keras model is to wrap them up as a layer uisng `tf.keras.layers.Lambda`."
666
666
]
667
667
},
668
668
{
@@ -711,7 +711,7 @@
711
711
"\n",
712
712
"<!--TODO(markdaoust) link to full examples or these referenced models.-->\n",
713
713
"\n",
714
-
"The easiest way to use autograph is keras layers and models is to `@autograph.convert()` the `call` method. See the [keras guide](https://tensorflow.org/guide/keras#build_advanced_models) for details on how to build on these classes. \n",
714
+
"The easiest way to use AutoGraph with Keras layers and models is to `@autograph.convert()` the `call` method. See the [TensorFlow Keras guide](https://tensorflow.org/guide/keras#build_advanced_models) for details on how to build on these classes. \n",
715
715
"\n",
716
716
"Here is a simple example of the [stocastic network depth](https://arxiv.org/abs/1603.09382) technique :"
717
717
]
@@ -866,9 +866,9 @@
866
866
"source": [
867
867
"## Advanced example: An in-graph training loop\n",
868
868
"\n",
869
-
"Since writing control flow in AutoGraph is easy, running a training loop in a TensorFlow graph should also be easy. \n",
869
+
"The previous section showed that AutoGraph can be used inside Keras layers and models. Keras models can also be used in AutoGraph code.\n",
870
870
"\n",
871
-
"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).\n",
871
+
"Since writing control flow in AutoGraphis easy, running a training loop in a TensorFlow graph should also be easy. \n",
872
872
"\n",
873
873
"This example shows how to train a simple Keras model on MNIST with the entire training process—loading batches, calculating gradients, updating parameters, calculating validation accuracy, and repeating until convergence—is performed in-graph."
0 commit comments