Skip to content

Keras - Layers

Layers are the fundamental building blocks of Keras models (tf.keras.Model). A layer is essentially a data processing module that takes input tensors, performs some computation (often involving learnable weights), and returns output tensors. The architecture of a neural network is defined by how these layers are connected.

Most Keras layers share some common concepts and configuration arguments:

  • Input Shape (input_shape): For the very first layer in a Sequential model, or when defining an Input layer in the Functional API, you need to specify the shape of the input data excluding the batch dimension. For example, for MNIST images (28x28 pixels, 1 channel), input_shape=(28, 28, 1) (using channels-last format). For flattened MNIST (784 features), input_shape=(784,).
  • Units / Filters: Many layers (like Dense, Conv2D, LSTM) have a primary argument specifying the dimensionality of the output space (number of neurons/units or number of convolution filters).
  • Activation Function (activation): Specifies the non-linear function to apply after the main computation of the layer. Can be specified by string name (e.g., 'relu', 'sigmoid', 'softmax') or as a function object from tf.keras.activations.
  • Weight Initializers (kernel_initializer, bias_initializer): Determine how the layer’s weights (kernel) and biases are initialized before training starts. Good initialization can help convergence. Common choices include 'glorot_uniform' (default for Dense, Conv), 'he_normal', 'zeros', 'ones'. Provided by tf.keras.initializers.
  • Regularizers (kernel_regularizer, bias_regularizer, activity_regularizer): Apply penalties to weights, biases, or layer activations during training to prevent overfitting. Examples: tf.keras.regularizers.l1(0.01), tf.keras.regularizers.l2(0.001).
  • Constraints (kernel_constraint, bias_constraint): Enforce constraints on weights during optimization (e.g., non-negativity using tf.keras.constraints.NonNeg()). Less commonly used than regularizers.
  • Learnable Parameters: Most layers have internal variables (weights and biases) that are updated during training via backpropagation to minimize the loss function. Layers like Dropout or Flatten typically have no learnable parameters.

Example: Creating a Dense layer with common parameters:

import tensorflow as tf
dense_layer = tf.keras.layers.Dense(
units=64, # 64 neurons in this layer
activation='relu', # Apply ReLU activation function
kernel_initializer='he_normal', # Use He normal initialization for weights
kernel_regularizer=tf.keras.regularizers.l2(0.01), # Apply L2 regularization
input_shape=(784,) # Specify input shape (if it's the first layer)
)

tf.keras.layers provides a wide variety of layer types. Here are some key categories:

  • Dense: Regular fully-connected layer. Each input neuron is connected to each output neuron. The core building block of MLPs. output = activation(dot(input, kernel) + bias).
  • Activation: Applies an activation function as a separate layer.
  • Dropout: Randomly sets a fraction (rate) of input units to 0 at each update during training time. A powerful technique to prevent overfitting.
  • Flatten: Flattens the input tensor into a single dimension (excluding the batch dimension). Often used to transition from convolutional/pooling layers to Dense layers.
  • Reshape: Reshapes the input tensor to a specified target shape.
  • Permute: Permutes the dimensions of the input according to a given pattern.
  • RepeatVector: Repeats the input n times.
  • Lambda: Wraps an arbitrary expression or function as a Layer object.
  • InputLayer / Input: Used to instantiate an input tensor (primarily for the Functional API).

Designed for grid-like data (e.g., images).

  • Conv1D, Conv2D, Conv3D: Perform 1D, 2D, or 3D convolution, respectively. Key parameters include filters (number of output filters), kernel_size (size of the convolution window), strides, padding ('valid' or 'same').
  • SeparableConv2D: Depthwise separable 2D convolution. More efficient than standard Conv2D, used in models like MobileNet.
  • DepthwiseConv2D: Depthwise 2D convolution.
  • Conv2DTranspose (Deconvolution): Performs an operation roughly inverse to convolution, often used in generative models or segmentation for upsampling.
  • UpSampling2D: Upsamples rows and columns by repeating them.

Reduce the spatial dimensions of feature maps, providing invariance and reducing computation.

  • MaxPooling1D, MaxPooling2D, MaxPooling3D: Take the maximum value over a window.
  • AveragePooling1D, AveragePooling2D, AveragePooling3D: Take the average value over a window.
  • GlobalMaxPooling2D, GlobalAveragePooling2D: Take the max or average over the entire spatial dimensions, resulting in a single value per feature map. Often used before a final Dense layer instead of Flatten.

Designed for sequential data.

  • SimpleRNN: Basic fully-connected RNN where the output is fed back to the input.
  • GRU (Gated Recurrent Unit): More sophisticated RNN with gating mechanisms to control information flow, often performing similarly to LSTMs with fewer parameters.
  • LSTM (Long Short-Term Memory): Advanced RNN with input, forget, and output gates, capable of learning long-range dependencies.
  • Bidirectional: A wrapper layer that applies an RNN (like LSTM or GRU) in both forward and backward directions on the input sequence and concatenates the outputs. Often improves performance by providing context from both past and future steps.

Help stabilize training and improve convergence.

  • BatchNormalization: Normalizes activations across the current batch. Very common in CNNs, applied after convolution/dense layers and before activation.
  • LayerNormalization: Normalizes activations across the features dimension. Often used in RNNs and Transformers.

Perform data preprocessing directly within the model, simplifying deployment as the preprocessing becomes part of the saved model.

  • TextVectorization: Converts raw text into sequences of integer indices or multi-hot vectors.
  • Normalization: Feature-wise normalization (calculates mean/variance from data).
  • Resizing, Rescaling, CenterCrop: Image transformations.
  • CategoryEncoding: Turns integer categorical features into one-hot, multi-hot, or TF-IDF vectors.

Implement attention mechanisms, allowing models to focus on relevant parts of the input sequence. Core components of Transformer models.

  • Attention, AdditiveAttention (Bahdanau-style): Basic attention mechanisms.
  • MultiHeadAttention: Performs attention in parallel across different ‘heads’. A key layer in Transformers.

The art of deep learning involves selecting appropriate layers and arranging them into an effective architecture for your specific task. Understanding the purpose and parameters of common layers is the first step towards building powerful Keras models.