-
Notifications
You must be signed in to change notification settings - Fork 45.6k
Add autograph keras layers example #4839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
samples/core/guide/autograph.ipynb
Outdated
"source": [ | ||
"## Interoperation with `tf.Keras`\n", | ||
"\n", | ||
"Now that you've seen the basics, let's build some real model components with autograph.\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
autograph => AutoGraph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
samples/core/guide/autograph.ipynb
Outdated
"\n", | ||
"Now that you've seen the basics, let's build some real model components with autograph.\n", | ||
"\n", | ||
"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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit:
It's easy to use autograph
with tf.keras
, but 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest keeping the observation about batching as a separate callout, e.g. in a separate paragraph with "Tip: remember that the inputs are usually batched examples, ...". The guidance is otherwise not strongly related to the keras interop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit:
Done.
not strongly related to the keras interop.
Moved up to the of examples.
samples/core/guide/autograph.ipynb
Outdated
"\n", | ||
"### Stateless functions\n", | ||
"\n", | ||
"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`." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 using tf.keras.layers.Lambda
:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
"source": [ | ||
"### Custom Layers and Models\n", | ||
"\n", | ||
"<!--TODO(markdaoust) link to full examples or these referenced models.-->\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO now or later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I can show that this performs as the paper: https://colab.research.google.com/drive/14YkY78YjBhqyKn9pQ50rC36hWkuWHFb7#scrollTo=nzst0_CH2uXV
samples/core/guide/autograph.ipynb
Outdated
"\n", | ||
"<!--TODO(markdaoust) link to full examples or these referenced models.-->\n", | ||
"\n", | ||
"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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The easiest way to use AutoGraph with Keras layers and models is to @autograph.convert()
the call
method. See the TensorFlow Keras guide for details to build these classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
samples/core/guide/autograph.ipynb
Outdated
"<!--TODO(markdaoust) link to examples showing autograph **in** keras models when ready-->\n", | ||
"\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", | ||
"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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a DevSite callout, options are:
Note:, Caution:, Warning:, Key Point:, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!
I was sure "important" was one of the allowed ones, maybe that was in g3doc....
Fixed/reworded.
samples/core/guide/autograph.ipynb
Outdated
@@ -377,7 +379,7 @@ | |||
" result = fizzbuzz(num)\n", | |||
" with tf.Session() as sess:\n", | |||
" for n in range(10,16):\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I feel that we should avoid examples that place control flow outside sess.run - in a way, AutoGraph was made so that one doesn't have to use that, which is a bit of an antipattern. For the point above, that converted code works like a regular op, I think we can use anything, like for instance taking the output and passing it through some other op, so we don't strictly need the fizzbuzz example for that. LMK what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point.
Here I'm trying to show that FizzBuzz is working, without getting into too-complex of an example.
This would be a step in the right direction:
@autograph.convert()
def fizzbuzz(num):
if num % 3 == 0 and num % 5 == 0:
return 'FizzBuzz'
elif num % 3 == 0:
return 'Fizz'
elif num % 5 == 0:
return 'Buzz'
else:
return tf.as_string(num)
@autograph.convert()
def fizzbuzz_loop(start, stop):
for n in range(start, stop):
print(fizzbuzz(n))
return tf.constant(stop)
with tf.Graph().as_default():
# The result works like a regular op: takes tensors in, returns tensors.
# You can inspect the graph using tf.get_default_graph().as_graph_def()
with tf.Session() as sess:
sess.run(fizzbuzz_loop(10,16))
But print
is currently not working...
samples/core/guide/autograph.ipynb
Outdated
" return x * x\n", | ||
"def inverse(x):\n", | ||
" assert x != 0.0, 'Do not pass zero!'\n", | ||
" return 1.0/x\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: for expressions, the Google style guide recommends placing a space around operators, e.g. "1.0 / x"; I think it would be useful to be consistent with it in this notebook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 we just don't have an integrated notebook linter (yet?).
Done.
samples/core/guide/autograph.ipynb
Outdated
"\n", | ||
"with tf.Graph().as_default(): \n", | ||
" with tf.Session():\n", | ||
" try:\n", | ||
" print(f(tf.constant(0)).eval())\n", | ||
" print(inverse(tf.constant(0.0)).eval())\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you use sess.run(inverse(...)) instead of .eval() here and elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
samples/core/guide/autograph.ipynb
Outdated
"\n", | ||
"Now that you've seen the basics, let's build some real model components with autograph.\n", | ||
"\n", | ||
"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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest keeping the observation about batching as a separate callout, e.g. in a separate paragraph with "Tip: remember that the inputs are usually batched examples, ...". The guidance is otherwise not strongly related to the keras interop.
" \n", | ||
" @autograph.convert()\n", | ||
" def call(self, inputs):\n", | ||
" training = tf.cast(K.learning_phase(), dtype=bool) \n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, we're looking into modifying the default behavior to avoid the need for these explicit casts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, good to know.
samples/core/guide/autograph.ipynb
Outdated
"<!--TODO(markdaoust) link to examples showing autograph **in** keras models when ready-->\n", | ||
"\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", | ||
"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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another way this could be framed is that AutoGraph is compatible with Keras: AutoGraph code can be used inside Keras models and layers as well; Keras models can also be used in AutoGrahp code, as this example shows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reworded, wdyt?
Colab link to this PR:
https://colab.research.google.com/github/markdaoust/models/blob/autograph/samples/core/guide/autograph.ipynb