字幕表 動画を再生する
[MUSIC PLAYING]
SPEAKER: In this video, I'll show you
how you can use Autograph to write complex, high performance
TensorFlow code using normal Python.
Autograph is available in the new TF2 function API,
makes it easy to run TensorFlow computations in a way
that's efficient and portable.
When you annotate a Python function with tf.function,
Autograph will automatically convert its Python code
to TensorFlow graph code.
The code is then compiled into a graph
and executed when you call the function.
Let's look at an example.
This simple function calculates the square of a scalar input
if it's positive.
In TensorFlow 2.0, you don't have to use tf.cond anymore.
You can just write a normal if statement,
and Autograph will generate a tf.cond operation
so that the entire computation runs as a graph.
This is the generated code that Autograph writes for you.
Notice that we're writing true and false functions that
would normally be fed into a tf.cond statement.
Instead of writing these, you can simply
use Python if statements.
Let's take a look at a more complicated example.
This is a bare bones RNN cell.
Note that it contains a data dependent for loop,
and it also contains a data independent if statement.
Autograph will only run the data dependent loop in the graph
and leave the data independent if statement untouched.
Simply adding a tf.function as a decorator
still lets you call the function directly and get results
immediately.
But the function runs in graph mode.
It prints results.
And we can also time it.
Now, if we remove the tf.function decorator, which
I've preemptively done here, and run the function in eager mode,
we get the same results out.
However, it's going to be a little bit slower because we
won't have coalesced the entire function into a single tf.graph
op.
We can time both options with tf.function in Autograph
and without.
You'll note that using tf.function, which
requires only a single function decorator,
is significantly faster than the eager mode version
without tf.function.
[MUSIC PLAYING]