Path: blob/master/site/en-snapshot/tutorials/customization/custom_layers.ipynb
25118 views
Copyright 2018 The TensorFlow Authors.
Custom layers
We recommend using tf.keras
as a high-level API for building neural networks. That said, most TensorFlow APIs are usable with eager execution.
Layers: common sets of useful operations
Most of the time when writing code for machine learning models you want to operate at a higher level of abstraction than individual operations and manipulation of individual variables.
Many machine learning models are expressible as the composition and stacking of relatively simple layers, and TensorFlow provides both a set of many common layers as well as easy ways for you to write your own application-specific layers either from scratch or as the composition of existing layers.
TensorFlow includes the full Keras API in the tf.keras package, and the Keras layers are very useful when building your own models.
The full list of pre-existing layers can be seen in the documentation. It includes Dense (a fully-connected layer), Conv2D, LSTM, BatchNormalization, Dropout, and many others.
Implementing custom layers
The best way to implement your own layer is extending the tf.keras.Layer class and implementing:
__init__
, where you can do all input-independent initializationbuild
, where you know the shapes of the input tensors and can do the rest of the initializationcall
, where you do the forward computation
Note that you don't have to wait until build
is called to create your variables, you can also create them in __init__
. However, the advantage of creating them in build
is that it enables late variable creation based on the shape of the inputs the layer will operate on. On the other hand, creating variables in __init__
would mean that shapes required to create the variables will need to be explicitly specified.
Overall code is easier to read and maintain if it uses standard layers whenever possible, as other readers will be familiar with the behavior of standard layers. If you want to use a layer which is not present in tf.keras.layers
, consider filing a github issue or, even better, sending us a pull request!
Models: Composing layers
Many interesting layer-like things in machine learning models are implemented by composing existing layers. For example, each residual block in a resnet is a composition of convolutions, batch normalizations, and a shortcut. Layers can be nested inside other layers.
Typically you inherit from keras.Model
when you need the model methods like: Model.fit
,Model.evaluate
, and Model.save
(see Custom Keras layers and models for details).
One other feature provided by keras.Model
(instead of keras.layers.Layer
) is that in addition to tracking variables, a keras.Model
also tracks its internal layers, making them easier to inspect.
For example here is a ResNet block:
Much of the time, however, models which compose many layers simply call one layer after the other. This can be done in very little code using tf.keras.Sequential
:
Next steps
Now you can go back to the previous notebook and adapt the linear regression example to use layers and models to be better structured.