It seems that tf.Layer modules come in two flavours: functions and classes. I normally use the functions directly (e.g, tf.layers.dense) but I'd like to know how to use classes directly (tf.layers.Dense). I've started experimenting with the new eager execution mode in tensorflow and I think using classes are going to be useful there as well but I haven't seen good examples in the documentation. Is there any part of TF documentation that shows how these are used?
I guess it would make sense to use them in a class where these layers are instantiated in the __init__
and then they're linked in the __call__
method when the inputs and dimensions are known?
Are these tf.layer classes related to tf.keras.Model
? Is there an equivalent wrapper class for using tf.layers
?
Update: for eager execution there's tfe.Network
that must be inherited. There's an example here
Defining models and layers in TensorFlow. Most models are made of layers. Layers are functions with a known mathematical structure that can be reused and have trainable variables. In TensorFlow, most high-level implementations of layers and models, such as Keras or Sonnet, are built on the same foundational class: tf.
Implementing custom layers Layer class and implementing: __init__ , where you can do all input-independent initialization. build , where you know the shapes of the input tensors and can do the rest of the initialization. call , where you do the forward computation.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the call() method, and a state (weight variables).
If you are building a new model architecture using existing keras/tf layers then build a custom model. If you are implementing your own custom tensor operations with in a layer, then build a custom layer.
tf.layers
and tf.keras.layer
classes are generally interchangeable and in fact at head (and thus by the next release - 1.9), the former actually inherits from the latter.
TensorFlow is moving towards consolidating on tf.keras
APIs for constructing models as that makes state ownership more explicit (e.g., parameters are "owned" by the Layer
object, as opposed to the functional style where all model parameters are put in a "collection" associated with the complete graph). This style works well for both eager execution and graph construction (support for eager execution is improving with every release). I'd recommend using tf.keras.layers
and tf.keras.Model
.
Some examples that you may find useful:
tensorflow/models
repositoryNot all existing TensorFlow examples have been moved to this style, but they slowly will.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With