From d10bd87f2ca949db295e741f7075d116e6c12f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Mon, 25 Mar 2019 10:01:20 +0800 Subject: [PATCH] Update tf.function doc: mention that `while` is captured, and explain how python numerical values are handled --- tensorflow/python/eager/def_function.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/eager/def_function.py b/tensorflow/python/eager/def_function.py index b1cc124bce6..dfbc5a96966 100644 --- a/tensorflow/python/eager/def_function.py +++ b/tensorflow/python/eager/def_function.py @@ -762,7 +762,8 @@ def function(func=None, assert (h().numpy() == f(x, y).numpy()).all() # Data-dependent control flow is also captured in the graph. Supported - # control flow statements include `if`, `for`, `break`, `continue`, `return`. + # control flow statements include `if`, `for`, `while`, `break`, `continue`, + # `return`. @tf.function def g(x): if tf.reduce_sum(x) > 0: @@ -784,7 +785,13 @@ def function(func=None, ``` Note that unlike other TensorFlow operations, we don't convert python - numerical inputs to tensors. + numerical inputs to tensors. Moreover, a new graph is generated for each + distinct python numerical value, for example calling `g(2)` and `g(3)` will + generate two new graphs (while only one is generated if you call + `g(tf.constant(2))` and `g(tf.constant(3))`). Therefore, python numerical + inputs should be restricted to arguments that will have few distinct values, + such as hyperparameters like the number of layers in a neural network. This + allows TensorFlow to optimize each variant of the neural network. _Referencing `tf.Variable`s_