Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 8 commits into from
Jul 19, 2018
Merged

Conversation

MarkDaoust
Copy link
Member

@MarkDaoust MarkDaoust commented Jul 19, 2018

"source": [
"## Interoperation with `tf.Keras`\n",
"\n",
"Now that you've seen the basics, let's build some real model components with autograph.\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autograph => AutoGraph

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"\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",
Copy link
Member

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.

Copy link

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.

Copy link
Member Author

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.

"\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`."
Copy link
Member

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:

Copy link
Member Author

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO now or later?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"\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",
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"<!--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",
Copy link
Member

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

Copy link
Member Author

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.

@@ -377,7 +379,7 @@
" result = fizzbuzz(num)\n",
" with tf.Session() as sess:\n",
" for n in range(10,16):\n",
Copy link

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.

Copy link
Member Author

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...

" return x * x\n",
"def inverse(x):\n",
" assert x != 0.0, 'Do not pass zero!'\n",
" return 1.0/x\n",
Copy link

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.

Copy link
Member Author

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.

"\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",
Copy link

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"\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",
Copy link

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",
Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, good to know.

"<!--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",
Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworded, wdyt?

@MarkDaoust MarkDaoust merged commit 15b3336 into tensorflow:master Jul 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants