pdfcoffee
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 ]
- Page 47 and 48: Neural Network Foundations with Ten
- Page 49 and 50: Neural Network Foundations with Ten
- Page 51 and 52: Neural Network Foundations with Ten
- Page 53 and 54: Neural Network Foundations with Ten
- Page 55 and 56: Neural Network Foundations with Ten
- Page 57 and 58: Neural Network Foundations with Ten
- Page 59 and 60: Neural Network Foundations with Ten
- Page 61 and 62: Neural Network Foundations with Ten
- Page 63 and 64: Neural Network Foundations with Ten
- Page 65 and 66: Neural Network Foundations with Ten
- Page 67 and 68: Neural Network Foundations with Ten
- Page 69 and 70: Neural Network Foundations with Ten
- Page 71 and 72: Neural Network Foundations with Ten
- Page 73 and 74: Neural Network Foundations with Ten
- Page 75 and 76: Neural Network Foundations with Ten
- Page 77 and 78: Neural Network Foundations with Ten
- Page 79 and 80: Neural Network Foundations with Ten
- Page 81 and 82: Neural Network Foundations with Ten
- Page 83 and 84: Neural Network Foundations with Ten
- Page 86 and 87: TensorFlow 1.x and 2.xThe intent of
- Page 88 and 89: An example to start withWe'll consi
- Page 90 and 91: Chapter 23. Placeholders: Placehold
- Page 92 and 93: • To create random values from a
- Page 94 and 95: To know the value, we need to creat
- Page 96 and 97: Chapter 2Both PyTorch and TensorFlo
- Page 100 and 101: Chapter 2For now, there's no need t
- Page 102 and 103: Chapter 2Let's see an example of a
- Page 104 and 105: Chapter 2If you want to save a mode
- Page 106 and 107: Chapter 2supervised=True)train_data
- Page 108 and 109: Chapter 2There, tf.feature_column.n
- Page 110 and 111: Chapter 2print (dz_dx)print (dy_dx)
- Page 112 and 113: Chapter 2In our toy example we use
- Page 114 and 115: Chapter 2For multi-machine training
- Page 116 and 117: Chapter 25. Use tf.layers modules t
- Page 118 and 119: Chapter 2Keras or tf.keras?Another
- Page 120: • tf.data can be used to load mod
- Page 123 and 124: RegressionLet us imagine a simpler
- Page 125 and 126: RegressionTake a look at the last t
- Page 127 and 128: Regression3. Now, we calculate the
- Page 129 and 130: RegressionIn the next section we wi
- Page 131 and 132: Regression2. Now, we define the fea
- Page 133 and 134: Regression2. Download the dataset:(
- Page 135 and 136: RegressionThe following is the Tens
- Page 137 and 138: RegressionIn regression the aim is
- Page 139 and 140: RegressionThe Estimator outputs the
- Page 141 and 142: RegressionThe following is the grap
- Page 143 and 144: RegressionReferencesHere are some g
- Page 145 and 146: Convolutional Neural NetworksIn thi
- Page 147 and 148: Convolutional Neural NetworksIn thi
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 ]