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

Let's have a look into the non-linear network topology:

Figure 3: An example of a non-linear topology

We will see multiple examples of using the Functional API in subsequent chapters

of this book.

Model subclassing

Model subclassing offers the highest flexibility and it is generally used when

you need to define your own layer. In other words, it is useful when you are in

the business of building your own special lego brick instead of composing more

standard and well-known bricks. There is indeed a higher cost in terms of complexity

and therefore subclassing should be used only when really needed. In the majority

of situations, Sequential and Functional APIs are more appropriate, but you can still

use model subclassing if you prefer to think in an object-oriented manner, as the

typical Python/NumPy developer does.

So, in order to create a custom layer, we can subclass tf.keras.layers.Layer and

implement the following methods:

• __init__: Optionally used to define all the sublayers to be used by this layer.

This is the constructor where you can declare your model.

• build: Used to create the weights of the layer. You can add weights with

add_weight().

• call: Used to define the forward pass. This is where your layer is called and

chained in functional style.

• Optionally, a layer can be serialized by using get_config() and deserialized

using from_config().

[ 66 ]

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

Saved successfully!

Ooh no, something went wrong!