Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tf.layers classes instead of functions

Tags:

tensorflow

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

like image 917
Milad Avatar asked Apr 25 '18 18:04

Milad


People also ask

How do you define layers in TensorFlow?

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.

How do I create a custom layer in TensorFlow?

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.

What does tf keras layers do?

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).

When should you create a custom layer versus a custom model?

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.


1 Answers

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:

  • MNIST in the tensorflow/models repository
  • The programmer's guide
  • Other eager execution samples (where the exact same model definition works for both graph execution and eager execution).

Not all existing TensorFlow examples have been moved to this style, but they slowly will.

Hope that helps.

like image 124
ash Avatar answered Sep 27 '22 20:09

ash