Keras
Dense layer
neural networks
machine learning
deep learning

Keras Dense layer's input is not flattened

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Keras is a popular high-level neural network API written in Python, offering fast prototyping and easy modification. One of its core components is the Dense layer, which is widely used in both simple and complex models. This article delves into why the input to a Keras Dense layer is not automatically flattened, explores its implications, and explains how the necessary input structure is achieved.

Dense Layer: An Overview

A Dense layer, or fully connected layer, is a neural network layer where each neuron receives input from all neurons in the previous layer. This is mathematically represented as:

z=Wx+bz = W \cdot x + b where:

  • WW is the weight matrix,
  • xx is the input vector,
  • bb is the bias vector,
  • zz is the resultant vector.

The dense layer applies a linear transformation to the input vector, followed by an optional activation function.

Keras Dense Layer and Input Shapes

In Keras, the Dense layer requires the input to be 2D (a matrix), where the first dimension is the batch size, and the second dimension is the number of features. However, for certain types of input data (such as images), this isn't the natural shape. Images, for example, are typically represented in 3D (height, width, channels), necessitating transformation before they can be fed into a Dense layer.

Why Inputs Aren't Automatically Flattened

Flexibility: Keras values transparency and explicit actions over assumptions. Automatically flattening inputs may lead to confusion about what transformations are applied to the data, reducing flexibility for users who desire control over data processing.

Optimization: Automatic flattening would remove opportunities for utilizing optimizations or specific strategies for pre-processing data, such as performing certain operations in the convolutional layers prior to a dense layer.

Practical Implications

  1. Image Data: If you work with image data, the convolutional and pooling layers can maintain the 3D shape (width, height, channels). Before feeding into a Dense layer, this data must be reshaped to 2D. The `Flatten` layer in Keras accomplishes this:
  • Input Definition: The input layer accepts a 3D shape.
  • Flatten Layer Use: Explicitly flattens from a 3D tensor to a 2D matrix before passing to a Dense layer.
  • Model Compilation: Normalizes workflow by explicitly defining each shape transformation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.