Keras
Sequential model
Input layer
Deep learning
Python

Keras Sequential model input layer

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Keras is a high-level deep learning API written in Python, which is capable of running on top of popular machine learning libraries such as TensorFlow. One of the most popular models in Keras is the `Sequential` model, which is a linear stack of layers. This article focuses on understanding the input layer in the Keras `Sequential` model, including its configuration and functionality.

Understanding the Keras Sequential Model

The Keras `Sequential` model is simple and ideal for situations where a model can be structured layer by layer in a stack formation. It is particularly suitable for most feedforward neural networks. The primary advantage of the `Sequential` model is its ease of use, which allows developers to quickly prototype models without in-depth architectural considerations.

Input Layer: The Foundation of a Neural Network

The input layer is fundamental to a neural network model, as it specifies the dimension of the input data. In Keras, the input layer is not explicitly defined, as it is automatically inferred based on the input shape you provide to the first actual layer in the network. However, it can also be defined using the `keras.layers.InputLayer`.

Defining Input Shape

Method 1: Specifying the Input Shape in the First Layer

The most straightforward method to define the input shape is by specifying it in the first layer of the `Sequential` model. This is generally done using the `input_shape` argument:

  • Batch Size: When defining the input shape, the batch size is not included. If desired, the `input_shape` can be specified as `(None, 100)`, where `None` acts as a placeholder for the batch size, which can vary.
  • Multiple Input Features: In cases of multi-dimensional inputs such as images (e.g., with dimensions height, width, channels), the input shape would reflect this, e.g., `input_shape=(32, 32, 3)` for a 32x32 RGB image.
  • Handling Variable Sequence Lengths: For sequences where the input length can vary (common in NLP tasks), you can use `None` to specify variable dimensions, for example, `input_shape=(None, 128)` for variable-length sequences.
  • The first layer specifies an `input_shape` of 784, typically for models handling unrolled 28x28 pixel images (common in the MNIST dataset).
  • Subsequent layers do not require an `input_shape` because they automatically infer their input dimension from the previous layer.

Course illustration
Course illustration

All Rights Reserved.