How keras.Input is different from keras.layers.InputLayer

From the course video what I took is the very first layer which accepts raw inputs from dataset is called Input Layer, then why we are using tf.keras.Input() instead of tf.keras.layers.InputLayer()

Why not use this,

tf.keras.layers.InputLayer(input_shape=(400, )),
tf.keras.layers.Dense(25, name="L1", activation="relu"),

but this,

tf.keras.layers.Input(shape=(400, )),
tf.keras.layers.Dense(25, name="L1", activation="relu"),

Infact in the code given by tensorflow developers on the InputLayer documentation page, they are using InputLayer instead of Input with sequential model.

fyi, i have read this article already tensorflow - Keras: difference of InputLayer and Input - Stack Overflow

There is more than one way to specify the size of the input.

Makes sense, thanks for clarifying

Might be interesting to take a look at the code on github to get a deeper understanding. They seem to be used interchangeably, and to a certain degree they are, but they are not exactly the same thing.

First, InputLayer

class InputLayer(base_layer.Layer):
    """Layer to be used as an entry point into a Network (a graph of layers).

InputLayer is-a Layer in Object-Oriented speak. It extends the base class, meaning it has the ability to carry Layer state through inherited attributes and act as a Layer using inherited behavior. Down in the body of the init function is where one of the attributes gets instantiated…

      input_tensor = keras_tensor.keras_tensor_from_type_spec(type_spec)

So an instance of InputLayer is-a Layer and has-a input_tensor

Next, Input.

def Input(
    """`Input()` is used to instantiate a Keras tensor.

Notice that Input is not a class. It is a function. It has neither Layer-like state nor Layer-like behavior of its own. What it does do can be found down near the end of the function…

    input_layer = InputLayer(**input_layer_config)

    # Return tensor including `_keras_history`.
    # Note that in this case train_output and test_output are the same pointer.
    outputs = input_layer._inbound_nodes[0].outputs
    if isinstance(outputs, list) and len(outputs) == 1:
        return outputs[0]
        return outputs

It calls the constructor for InputLayer, and returns its outputs. From this you can see why usage of the two looks similar. InputLayer instantiates the object instance directly, using its init(). Input isn’t itself a class; you don’t instantiate an Input object. Instead, Input instantiates an InputLayer indirectly and returns the InputLayer object’s output attribute, which is what you need to start wiring together the model. Hope this helps.

It makes more sense now. so basically the Input depends on InputLayer and it returns the unchanged tensor from the X_train to next layer (Dense, CNN, Embedding …)

Also I found a short-cut to have input_shape in the first Dense and CNN layers which automatically implements the InputLayer for us.

Quoting from the Dense layer documentation

When a popular kwarg input_shape is passed, then keras will create an input layer to insert before the current layer. This can be treated equivalent to explicitly defining an InputLayer .

1 Like