pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 2state = [tf.zeros([100, 100])] * 2# warmupcell(input, state)fn(input, state)graph_time = timeit.timeit(lambda: cell(input, state), number=100)auto_graph_time = timeit.timeit(lambda: fn(input, state), number=100)print('graph_time:', graph_time)print('auto_graph_time:', auto_graph_time)When the code fragment is executed, you see a reduction in time of one order ofmagnitude if tf.function() is used:graph_time: 0.4504085020016646auto_graph_time: 0.07892408400221029In short, you can decorate Python functions and methods with tf.function, whichconverts them to the equivalent of a static graph, with all the optimization that comeswith it.Keras APIs – three programming modelsTensorFlow 1.x provides a lower-level API. You build models by first creating agraph of ops, which you then compile and execute. tf.keras offers a higher APIlevel, with three different programming models: Sequential API, Functional API, andModel Subclassing. Learning models are created as easily as "putting LEGO ® brickstogether," where each "lego brick" is a specific Keras.layer. Let's see when it is bestto use Sequential, Functional, and Subclassing, and note that you can mix-and-matchthe three styles according to your specific needs.Sequential APIThe Sequential API is a very elegant, intuitive, and concise model that is appropriatein 90% of cases. In the previous chapter, we covered an example of using theSequential API when we discussed the MNIST code, so let's create the brick with:tf.keras.utils.plot_model(model, to_file="model.png")[ 63 ]

TensorFlow 1.x and 2.xFigure 2: An example of a Sequential modelFunctional APIThe Functional API is useful when you want to build a model with more complex(non-linear) topologies, including multiple inputs, multiple outputs, residualconnections with non-sequential flows, and shared and reusable layers. Each layeris callable (with a tensor in input), and each layer returns a tensor as an output.Let's look at an example where we have two separate inputs, two separate logisticregressions as outputs, and one shared module in the middle.[ 64 ]

Chapter 2

state = [tf.zeros([100, 100])] * 2

# warmup

cell(input, state)

fn(input, state)

graph_time = timeit.timeit(lambda: cell(input, state), number=100)

auto_graph_time = timeit.timeit(lambda: fn(input, state), number=100)

print('graph_time:', graph_time)

print('auto_graph_time:', auto_graph_time)

When the code fragment is executed, you see a reduction in time of one order of

magnitude if tf.function() is used:

graph_time: 0.4504085020016646

auto_graph_time: 0.07892408400221029

In short, you can decorate Python functions and methods with tf.function, which

converts them to the equivalent of a static graph, with all the optimization that comes

with it.

Keras APIs – three programming models

TensorFlow 1.x provides a lower-level API. You build models by first creating a

graph of ops, which you then compile and execute. tf.keras offers a higher API

level, with three different programming models: Sequential API, Functional API, and

Model Subclassing. Learning models are created as easily as "putting LEGO ® bricks

together," where each "lego brick" is a specific Keras.layer. Let's see when it is best

to use Sequential, Functional, and Subclassing, and note that you can mix-and-match

the three styles according to your specific needs.

Sequential API

The Sequential API is a very elegant, intuitive, and concise model that is appropriate

in 90% of cases. In the previous chapter, we covered an example of using the

Sequential API when we discussed the MNIST code, so let's create the brick with:

tf.keras.utils.plot_model(model, to_file="model.png")

[ 63 ]

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

Saved successfully!

Ooh no, something went wrong!