TensorFlow

https://blog.tensorflow.org/2019/09/tensorflow-20-is-now-available.html

Install TensorFlow 2

Version Python version Compiler Build tools
tensorflow-2.2.0 3.5-3.8 GCC 7.3.1 Bazel 2.0.0
tensorflow-2.1.0 2.7, 3.5-3.7 GCC 7.3.1 Bazel 0.27.1
tensorflow-2.0.0 2.7, 3.3-3.7 GCC 7.3.1 Bazel 0.26.1
1
2
3
sudo pacman -U cuda-10.1.243-2-x86_64.pkg.tar.xz
sudo pacman -U cudnn-7.6.5.32-2-x86_64.pkg.tar.xz
pip install tensorflow-gpu

Data input pipelines

Keras

Sequential API

When to use a Sequential model

A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.

A Sequential model is not appropriate when:

  • Your model has multiple inputs or multiple outputs
  • Any of your layers has multiple inputs or multiple outputs
  • You need to do layer sharing
  • You want non-linear topology (e.g. a residual connection, a multi-branch model)

The Sequential constructor accepts a name argument, just like any layer or model in Keras. This is useful to annotate TensorBoard graphs with semantically meaningful names.

Specifying the input shape in advance

it can be very useful when building a Sequential model incrementally to be able to display the summary of the model so far, including the current output shape. In this case, you should start your model by passing an Input object to your model, so that it knows its input shape from the start.

1
2
3
model = keras.Sequential()
model.add(keras.Input(shape=(4,)))
model.add(layers.Dense(2, activation="relu"))

A simple alternative is to just pass an input_shape argument to your first layer.

1
2
model = keras.Sequential()
model.add(layers.Dense(2, activation="relu", input_shape=(4,)))

In general, it’s a recommended best practice to always specify the input shape of a Sequential model in advance if you know what it is.

A common debugging workflow: add() + summary()

When building a new Sequential architecture, it’s useful to incrementally stack layers with add() and frequently print model summaries.

Feature extraction with a Sequential model

quickly creating a model that extracts the outputs of all intermediate layers in a Sequential model:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
initial_model = keras.Sequential(
    [
        keras.Input(shape=(250, 250, 3)),
        layers.Conv2D(32, 5, strides=2, activation="relu"),
        layers.Conv2D(32, 3, activation="relu"),
        # layers.Conv2D(32, 3, activation="relu", name="my_intermediate_layer"),
        layers.Conv2D(32, 3, activation="relu"),
    ]
)
feature_extractor = keras.Model(
    inputs=initial_model.inputs,
    outputs=[layer.output for layer in initial_model.layers],
    # only extract features from one layer:
    # outputs=initial_model.get_layer(name="my_intermediate_layer").output,
)

# Call feature extractor on test input.
x = tf.ones((1, 250, 250, 3))
features = feature_extractor(x)

Transfer learning with a Sequential model

Keras functional API

train_and_evaluate

Write custom layers and models

Save and serialize models

Customizing what happens in fit

Writing a training loop from scratch

Keras Recurrent Neural Networks

Masking and padding

Write custom callbacks

Transfer learning

Estimators

Save a model

Reference

  1. [TensorFlow 2.0 is now available!]([https://blog.tensorflow.org/2019/09/tensorflow-20-is-now-available.html#:~:text=Today%2C%20we’re%20delighted%20to,TensorFlow%202.0%20is%20now%20available!&text=TensorFlow%202.0%20provides%20a%20comprehensive,build%20scalable%20ML%2Dpowered%20applications.](https://blog.tensorflow.org/2019/09/tensorflow-20-is-now-available.html#:~:text=Today%2C we’re delighted to,TensorFlow 2.0 is now available!&text=TensorFlow 2.0 provides a comprehensive,build scalable ML-powered applications.))
  2. Getting started with Tensorflow 2.0 Tutorial