Dense Layer
March 25, 2026 · View on GitHub
Overview & Motivation
A dense (fully-connected) layer is the most fundamental building block of a neural network. It maps an input vector to an output vector through a learnable affine transformation followed by a non-linear activation:
Every input neuron is connected to every output neuron — hence "fully connected." The layer's parameters are the weight matrix and bias vector ; training adjusts these to minimize the loss.
In this library, input size, output size, and parameter count are all compile-time constants, enabling stack allocation and dimension checking with zero runtime overhead.
Mathematical Theory
Forward Pass
where , , and is the activation function.
Backward Pass
Given the gradient of the loss with respect to the output :
-
Pre-activation gradient:
-
Weight gradient:
-
Bias gradient:
-
Input gradient (propagated to the previous layer):
Parameter Count
For a layer with 128 inputs and 64 outputs: parameters.
Complexity Analysis
| Operation | Time | Space |
|---|---|---|
| Forward () | output + cached | |
| Backward (, , ) | weight gradient | |
| Total parameters | — |
The matrix-vector product dominates both passes. For embedded networks (e.g. ), a single forward pass takes ~512 multiply-accumulate operations.
Step-by-Step Walkthrough
Layer: 3 inputs → 2 outputs, ReLU activation.
Forward:
| Step | Computation | Result |
|---|---|---|
Backward with :
| Step | Computation | Result |
|---|---|---|
| row 1: all zeros; row 2: | ||
| — | ||
Pitfalls & Edge Cases
- Dimension mismatch. In a multi-layer network, the output size of layer must equal the input size of layer . This library enforces this at compile time.
- Weight initialization. Zero-initialized weights cause all neurons to compute the same thing (symmetry problem). Use He initialization for ReLU: .
- Gradient accumulation. When processing mini-batches, accumulate across samples before updating — do not update per-sample.
- Fixed-point overflow in . The dot product of terms can exceed Q15/Q31 range. Use a wider accumulator (Q31 for Q15 data) or scale weights.
- Large layers exhaust stack. A 256×256 weight matrix of
floatconsumes 256 KB. Size layers to fit the target's stack budget.
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| Convolutional layer | Weight sharing across spatial positions; parameters per filter instead of |
| Recurrent layer | Shares weights across time steps; adds a hidden state feedback connection |
| Batch normalization layer | Normalizes activations to zero mean and unit variance; accelerates training |
| Dropout layer | Randomly zeros activations during training; regularization effect |
| Sparse layer | Only a subset of connections exist; reduces parameter count and computation |
Applications
- Hidden layers — One or more dense layers form the core of feed-forward networks for regression and classification.
- Output layer — A final dense layer maps to the target dimensionality (1 for regression, for -class classification).
- Embedding projection — Dense layers project high-dimensional sparse inputs to low-dimensional dense representations.
- Controller networks — In neural network-based control, small dense layers map state vectors to actuator commands.
Connections to Other Algorithms
graph TD
Layer["Dense Layer"]
Act["Activation Functions"]
Model["Model"]
Opt["Optimizer"]
LR["Linear Regression"]
Act --> Layer
Layer --> Model
Model --> Opt
Layer -.->|"no activation, MSE loss"| LR
| Component | Relationship |
|---|---|
| Activation Functions | Applied element-wise after the affine transformation |
| Model | Chains multiple dense layers into a network |
| Optimizer | Updates and using the computed gradients |
| Linear Regression | A dense layer with identity activation and MSE loss is equivalent to linear regression |
References & Further Reading
- Goodfellow, I., Bengio, Y., and Courville, A., Deep Learning, MIT Press, 2016 — Chapter 6.
- He, K. et al., "Delving deep into rectifiers", ICCV, 2015 — He weight initialization.
- Glorot, X. and Bengio, Y., "Understanding the difficulty of training deep feedforward neural networks", AISTATS, 2010 — Xavier initialization.