Open In App

Keras Input Layer

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When deep learning models are built, the foundation step of the model preparation starts with the input layer. Keras Input Layer is essential for defining the shape and size of the input data the model with receive. In this article, we are going to learn more on Keras Input Layer, its purpose, usage and it’s role in model architecture.

What is Keras Input Layer?

The Input Layer in Keras is the starting point of any neural network. It is not a traditional layer that processes or transforms data. Instead, it serves as a specification of the kind of input the model expects, including the dimensions and type of data. Essentially, it defines how the network should receive the input data, setting the stage for the subsequent layers.

There are two ways to specify the input layer :

  1. Specifying input layer explicitly.
  2. Implicit specification by passing ‘input_shape’ dimension in the first layer of the neural network.

Purpose of the Keras Input Layer

The primary purpose of the Input Layer in Keras is to define the input’s shape (or dimensionality). This specification is crucial because the subsequent layers in the network need to know the shape of their input tensor to correctly configure themselves. The input layer does not alter data; it simply passes it on to the next layer.

Key Features of Keras Input Layer

  1. Input Shape Specification: The Input Layer specifies the shape of the input data, not including the batch size. For instance, for a 28×28 pixel image, the input shape would be (28, 28).
  2. Compatibility: Ensures that the first layer of the model is prepared to receive the correct form of data, facilitating smoother data processing through the network.
  3. Flexibility: It allows the model to handle input data of varying lengths, especially important in models processing sequences or time series.

Syntax of Keras Input Layer

The keras.Input function is used to instantiate a tensor object that is useful when building model that use the functional API.

keras.Input(
shape=None,
batch_size=None,
dtype=None,
sparse=None,
batch_shape=None,
name=None,
tensor=None,
)

Parameters of keras.Input

  1. shape: A tuple specifying the dimensions of the input data. When using this parameter, do not include the batch size. For instance, shape=(32, 32, 3) would be used for 32×32 RGB images.
  2. batch_size: This parameter can be used to define the batch size explicitly. If specified, it means the model will always expect batch input with the specified batch size. This is particularly useful for stateful models in recurrent networks.
  3. dtype: The data type expected by the input, as a string (e.g., ‘float32’, ‘int32’). By default, it is set to None, which means it defaults to the float32.
  4. sparse: A boolean specifying whether the input data will be a sparse tensor. If True, the resulting input will be a sparse tensor (tf.sparse.SparseTensor), often used when dealing with sparse data like word indices in text processing.
  5. batch_shape: An alternative to shape, this parameter includes the batch size as part of the shape. It is a tuple of integers, e.g., batch_shape=(10, 32) for a batch size of 10 and input features of 32.
  6. name: A string name for the layer. Useful when you need to specify a specific layer name for purposes such as model serialization or for referencing layers by name in situations involving complex models.
  7. tensor: An optional existing tensor to wrap into the Input layer. If set, the layer will not create a placeholder tensor but will use the given tensor, which is useful for creating models based on external inputs.

Example Usage of keras.Input

In this section, we have defined a CNN model with an input shape of (28, 28, 1) and a batch size of 3 using TensorFlow’s Keras API. It includes a convolutional layer with 16 filters, a max pooling layer, a flatten layer, and a dense layer with 10 units and a softmax activation function for classification. The shape of the input layer is printed, showing the specified shape and batch size.

Python3
from tensorflow import keras
from keras import models, layers
input_layer = keras.layers.Input(shape=(28, 28, 1),batch_size=3)
model = models.Sequential([
  layers.Conv2D(16, (3,3)),
  layers.MaxPooling2D(pool_size=2),
  layers.Flatten(),
  layers.Dense(10, activation='softmax'),
])
print("The shape of input layer: ",input_layer.shape)

Output:

The shape of input layer:  (3, 28, 28, 1)
  • The input layer in Keras is in charge of obtaining and transforming the input data, making it a crucial part of deep learning models.
  • The whole neural network architecture depends on the shape of the input data.
  • Thus to start with building an efficient neural network, Input layer is necessary.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads