09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TensorFlow 1.x and 2.x

>>> simple_function

<function simple_function at 0xb26c3e510>

Note that with tf.function you need to annotate only one main function,

so all other functions called from there will be automatically and transparently

transformed into an optimized computational graph. For example, in the preceding

code, there is no need to annotate linear_layer. Think about tf.function as

marking code for Just In Time (JIT) compilation. Normally, you do not need to

see the code automatically generated by AutoGraph, but in case you are curious,

then you can investigate with the following code fragment:

# internal look at the auto-generated code

print(tf.autograph.to_code(simple_nn.python_function, experimental_

optional_features=None))

This code fragment will print the following automatically generated piece of code:

from __future__ import print_function

def tf__simple_nn(x):

do_return = False

retval_ = None

do_return = True

retval_ = ag__.converted_call('relu', tf.nn, ag__.ConversionOptions

(recursive=True, verbose=0, strip_decorators=(ag__.convert, ag__.do_

not_convert, ag__.converted_call), force_conversion=False, optional_

features=(), internal_convert_user_code=True), (linear_layer(x),), {})

return retval_

tf__simple_nn.autograph_info__ = {}

Let's see an example of the difference in speed between code annotated with the

tf.function() decorator and the same code with no annotation. Here we use

a layer LSTMCell() which will be discussed in Chapter 8, Recurrent Neural Networks,

but for now consider it as a black box doing some form of learning:

import tensorflow as tf

import timeit

cell = tf.keras.layers.LSTMCell(100)

@tf.function

def fn(input, state):

return cell(input, state)

input = tf.zeros([100, 100])

[ 62 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!