Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between tf.keras.layers versus tf.layers?

What is the difference between tf.keras.layers versus tf.layers?
E.g. both of them have Conv2d, do they provide different outputs?
Is there any benefits if you mix them (something like a tf.keras.layers.Conv2d in one hidden layer and in the next, tf.layers.max_pooling2d)?

like image 375
Gabriel_Koch Avatar asked Jun 28 '18 18:06

Gabriel_Koch


People also ask

What is the difference between Keras and tf Keras?

TensorFlow is an open-sourced end-to-end platform, a library for multiple machine learning tasks, while Keras is a high-level neural network library that runs on top of TensorFlow. Both provide high-level APIs used for easily building and training models, but Keras is more user-friendly because it's built-in Python.

What does tf Keras layers do?

Used in the notebooks. 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).

What is a Keras layer?

Layers are the basic building blocks of neural networks in Keras. A layer consists of a tensor-in tensor-out computation function (the layer's call method) and some state, held in TensorFlow variables (the layer's weights).

What is tf Keras layers dense?

Dense layer is the regular deeply connected neural network layer. It is most common and frequently used layer. Dense layer does the below operation on the input and return the output. output = activation(dot(input, kernel) + bias)


3 Answers

Since TensorFlow 1.12, tf.layers are merely wrappers around tf.keras.layers.

A few examples:

Convolutional tf.layers just inherit from the convolutional tf.keras.layers, see source code here:

@tf_export('layers.Conv2D')
class Conv2D(keras_layers.Conv2D, base.Layer):

The same is true for all core tf.layers, e.g.:

@tf_export('layers.Dense')
class Dense(keras_layers.Dense, base.Layer):

With the integration of Keras into TensorFlow, it would make little sense to maintain several different layer implementations. tf.keras is becoming the de-facto high-level API for TensorFlow, therefore tf.layers are now just wrappers around tf.keras.layers.

like image 61
Alex Avatar answered Oct 08 '22 15:10

Alex


tf.keras.layers.Conv2d is a tensorflow-keras layer while tf.layers.max_pooling2d is a tensorflow 'native layer'

You cannot use a native layer directly within a Keras model, as it will be missing certain attributes required by the Keras API.

However, it is possible to use native layer if wrapped within a tensorflow-keras Lambda layer. A link to the documentation for this is below.

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda

like image 21
isaacsultan Avatar answered Oct 08 '22 15:10

isaacsultan


tf.layers module is Tensorflow attempt at creating a Keras like API whereas tf.keras.layers is a compatibility wrapper. In fact, most of the implementation refers back to tf.layers, for example the tf.keras.layers.Dense inherits the core implementation:

@tf_export('keras.layers.Dense')
class Dense(tf_core_layers.Dense, Layer):
  # ...

Because the tf.keras compatibility module is checked into the Tensorflow repo separately, it might lack behind what Keras actually offers. I would use Keras directly or tf.layers but not necessarily mix them.

like image 34
nuric Avatar answered Oct 08 '22 17:10

nuric