A Keras model can used as a Tensorflow function on a Tensor, through the functional API, as described here.
So we can do:
from keras.layers import InputLayer a = tf.placeholder(dtype=tf.float32, shape=(None, 784)) model = Sequential() model.add(InputLayer(input_tensor=a, input_shape=(None, 784))) model.add(Dense(32, activation='relu')) model.add(Dense(10, activation='softmax')) output = model.output
Which is a tensor:
<tf.Tensor 'dense_24/Softmax:0' shape=(?, 10) dtype=float32>
But, this also works without any InputLayer
:
a = tf.placeholder(dtype=tf.float32, shape=(None, 784)) model = Sequential() model.add(Dense(32, activation='relu', input_shape=(784,))) model.add(Dense(10, activation='softmax')) output = model(a)
works, and output
has the same shape as before:
<tf.Tensor 'sequential_9/dense_22/Softmax:0' shape=(?, 10) dtype=float32>
I assume the first form permits:
inputs
and outputs
as attributes of the model (of the same names), so we can reuse them elsewhere. For example with other TF ops._keras_history
as stated in the source code).But this is not something we cannot do with the second form, so, is there a special usage of the InputLayer
(and Input
a fortiori) (except for multiple inputs)?
Moreover, the InputLayer
is tricky because it's using input_shape
differently from other keras layers: we specify the batch size (None
here), which is not usually the case...
Input function. Input() is used to instantiate a Keras tensor. A Keras tensor is a symbolic tensor-like object, which we augment with certain attributes that allow us to build a Keras model just by knowing the inputs and outputs of the model. shape: A shape tuple (integers), not including the batch size.
It is generally recommend to use the Keras Functional model via Input , (which creates an InputLayer ) without directly using InputLayer . When using InputLayer with the Keras Sequential model, it can be skipped by moving the input_shape parameter to the first layer after the InputLayer .
Input shape (list of integers, does not include the samples axis) which is required when using this layer as the first layer in a model. batch_input_shape. Shapes, including the batch size. For instance, batch_input_shape=c(10, 32) indicates that the expected input will be batches of 10 32-dimensional vectors.
A Sequential model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor. Schematically, the following Sequential model: # Define Sequential model with 3 layers. model = keras.
It would seem that InputLayer
has some uses:
First, it allows you to give pure tensorflow tensors as is, without specifying their shape. E.g. you could have written
model.add(InputLayer(input_tensor=a))
This is nice for several obvious reasons, among others less duplication.
Second, they allow you to write non-sequential networks with a single input, e.g.
input / \ / \ / \ conv1 conv2 | |
Without InputLayer
you would need to explicitly feed conv1
and conv2
the same tensor, or create an arbitrary identity layer on top of the model. Neither is quite pleasing.
Finally, they remove the arbitrary distinction between "layers that are also inputs" and "normal layers". If you use InputLayer
you can write code where there is a clear distinction between what layer is the input and what layer does something. This improves code readability and makes refactoring much easier. For example, replacing the first layer becomes just as easy as replacing any other layer, you don't need to think about input_shape
.
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