Research
Batch Matrix-form Equations and Implementation of Multilayer Perceptrons
Batch Matrix-form Equations and Implementation of Multilayer Perceptrons Overview Research area: Deep learning fundamentals — explicit mathematical specification and reference implementation of multil

- arXiv
- 2511.11918
- Published
- 2025-11-14
- Authors
- Wieger Wesselink, Bram Grooten, Huub van de Wetering, Qiao Xiao, Decebal Constantin Mocanu
AI summary
Batch Matrix-form Equations and Implementation of Multilayer PerceptronsOverview
Research area: Deep learning fundamentals — explicit mathematical specification and reference implementation of multilayer perceptrons (MLPs), with an emphasis on sparse neural networks.
Technical level: Advanced. The paper assumes comfort with matrix calculus, gradient/Jacobian definitions, backpropagation derivations, and reads code in NumPy-style Python and C++.
Scope: The paper provides complete, validated, batch matrix-form forward and backward equations for MLP layers (including batch normalization and softmax), plus uniform reference implementations across NumPy, PyTorch, JAX, TensorFlow, and a sparse-optimized C++ backend.
What This Paper Is About
Modern deep learning frameworks hide MLP algorithms behind automatic differentiation, so the precise batch matrix-form structure of forward and backward passes is rarely documented explicitly — and, according to the authors, is absent for advanced layers such as batch normalization and softmax. This paper fills that gap by deriving complete batch matrix-form backpropagation equations for MLPs, validating them symbolically, and turning them into readable, uniform reference implementations. A sparse neural network case study then shows what becomes possible once the computation is made explicit.
Key Contributions
- Complete derivation of batch matrix-form backpropagation for MLPs, covering standard layers plus advanced layers such as batch normalization and softmax.
- Symbolic validation of all gradient equations using SymPy, which the authors argue goes beyond numerical gradient checking because it localizes errors in formulas rather than only signaling that an error exists.
- Uniform reference implementations across NumPy, PyTorch, JAX, and TensorFlow, plus a high-performance C++ backend, all built on the same small set of matrix primitives defined in Table 1 of the paper.
- A demonstration that explicit formulations enable efficient sparse computation, showing how visible computational structure supports systematic optimization, sparsity exploration, and specialized architectures.
Main Findings
-
Derivations are validated symbolically, not just numerically: Every gradient equation is checked with SymPy, giving mathematical assurance and precise error localization in formulas.
-
A small set of matrix primitives suffices: Table 1 defines the operations needed (transposition, matrix multiplication, Hadamard product, diag/Diag, elements_sum, column/row repeat and sums, column/row max and mean, element-wise functions, log_sigmoid), and Table 3 maps them to NumPy, PyTorch, JAX, TensorFlow, and C++ with Eigen. This yields implementations that are uniform, maintainable, and extensible.
-
Explicit equations reveal sparse bottlenecks: Fine-grained profiling identifies the critical sparse operations as
A = SB(sparse-dense, feedforward),A = S^T B(sparse-dense, backward pass), andS = AB^T(dense-dense, backward pass). -
AB^Tdominates backpropagation in sparse linear layers: Because only a small subset of the dense product's values are retained for a sparseS, explicitly forming and storingAB^Tis expensive and negates sparsity's memory advantages. In experiments such as training on CIFAR-10 at 95% sparsity, this operation consumed over half of the backpropagation time. -
Eigen and MKL expression-tree issues surfaced: Dense products were sometimes not forwarded by Eigen's expression trees to MKL, resulting in slower execution, and some MKL sparse kernels showed poor efficiency for large matrices — bottlenecks that explicit matrix-form equations make straightforward to detect.
-
Sparse settings favor Nerva over PyTorch masking: Dense MLPs implemented with Nerva perform on par with PyTorch, but PyTorch's masking strategy maintains nearly constant runtime across sparsity levels, whereas Nerva's truly sparse implementation accelerates as sparsity increases.
-
At 99% sparsity on CIFAR-10, Nerva is about four times faster and uses dramatically less memory, enabling training of networks too large for dense frameworks.
-
The softmax derivation is worked as a full example: The paper derives the Jacobian of softmax in matrix form, arriving at
Diag(y) - y^T y, and shows how equations map to code. -
Layer design is object-oriented and uniform: Each layer exposes
feedforward,backpropagate, andoptimize; the MLP iterates layers forward, then inreversed(self.layers)order, passingY, DY = layer.X, layer.DXbackward. Optimizers use the composite design pattern so different parameters can use different optimizers (the listing showsMomentum(0.9)andNesterov(0.9)combined viaCompositeOptimizer). -
DropConnect-style dropout is specified in matrix form:
Y = X(W ⊙ R)^T + 1_N · b, with non-zero entries ofRset to1/(1−p)to compensate for the average shrinkage of weight magnitudes by a factor1−p. -
Batch normalization is rewritten in matrix form: Centering uses
R = X − (1_N · 1_N^T / N) · X, variance usesΣ = diag(R^T R)^T / N, standardization usesZ = (1_N · Σ^(−1/2)) ⊙ R, and the scale/shift step isY = (1_N · γ) ⊙ Z + 1_N · β.
Methodology in Plain English
The authors take the position that MLP algorithms should be written out explicitly rather than delegated to automatic differentiation.
-
Fix notational conventions first. Data is stored in row layout, so each row of an input matrix
X ∈ ℝ^(N×D)is one example. Gradients, Jacobians, and a shorthandDY = ∇_Y L(Y,T)for the loss gradient are defined up front. Broadcasting is deliberately avoided in favor of explicit matrix calculus, because SymPy validation needs explicit dimensions. -
Reduce everything to a small operation vocabulary. Table 1 lists the matrix operations the MLPs need, each with a Nerva API name (
zeros,hadamard,columns_sum,inv_sqrt,log_sigmoid, and so on) that maps one-to-one to code across backends. -
Derive equations layer by layer. Forward and backward equations are written for linear layers, activations, batch normalization, dropout, and softmax. The softmax gradient derivation is shown step by step, with the number of each applied rule placed above the equal sign.
-
Validate the equations symbolically. Rather than only checking numerical gradients, the authors use SymPy so an incorrect formula can be pinpointed rather than merely flagged.
-
Implement the same equations everywhere. The Python backends (NumPy, PyTorch, JAX, TensorFlow) emphasize clarity and readability for education and experimentation; the C++ backend (Eigen, Intel MKL) targets high performance and truly sparse networks. Because both come from the same specifications, correctness follows from the validated equations.
-
Use sparsity as the stress test. Sparse networks are the case where explicit equations pay off most, so the authors profile each equation as one or more matrix operations, time sub-steps precisely, and try several approaches to compute
AB^Tdirectly in sparse form in the Nerva libraries.
Why This Matters
Impact on research. By making the computational structure of MLPs explicit and validated, the paper gives researchers a transparent baseline for analysis, modification, and optimization rather than a black box. It also creates a bridge between mathematics and code: because each equation maps to named primitives, implementations across frameworks become comparable and errors become traceable.
Real-world applications:
- Sparse neural network training — the demonstrated setting where explicit equations enable speedups and memory savings that dense frameworks with masking cannot match.
- Education — the readable Python backends and fully derived equations support teaching how backpropagation actually works.
- Custom or specialized architectures — researchers who need to modify layers, gradients, or optimizers directly rather than working around autograd limitations.
- Resource-constrained deployment and scaling — the ability to train networks too large for dense frameworks matters when memory is the binding constraint.
Industry relevance. Teams building on PyTorch, JAX, or TensorFlow may not need to reimplement backpropagation, but teams working with sparse models, custom hardware kernels, or non-standard gradients benefit from explicit, profiled formulations. The finding that one operation (AB^T) consumed over half of sparse backpropagation time is exactly the kind of targeted insight that guides kernel engineering, and the uniform primitive layer lowers the cost of porting implementations across frameworks.
Future Directions
- Faster sparse computation of
AB^T. The paper reports experimenting with several approaches in the Nerva libraries, but this product remains the dominant bottleneck in sparse backpropagation and the clearest target for further work. - Better sparse kernel efficiency in existing libraries. The authors note that some Intel MKL sparse kernels were inefficient for large matrices and that Eigen expression trees sometimes failed to forward dense products to MKL — gaps that could be addressed at the library level.
- Extending the primitive-based implementation approach to further frameworks and layer types. The design is described as readily extensible, and the appendices already cover layer equations, matrix operations, activations, losses, initialization, optimizers, and learning rate schedulers.
- Broadening the sparse case study. The reported sparse comparison centers on CIFAR-10 at 95% and 99% sparsity; a wider range of datasets, architectures, and sparsity patterns would test how general the advantages are. The paper does not report results beyond those settings.
Target Audience
This paper is best suited to graduate students and researchers in machine learning who want an explicit, derivation-level understanding of MLP training; to educators building courses around transparent backpropagation implementations; and to engineers and systems researchers working on sparse neural networks, custom kernels, or cross-framework reference implementations. Readers who only use high-level frameworks without needing to inspect gradients will find the mathematical detail heavier than necessary, while those already comfortable with matrix calculus and C++ will get the most from it.
Authors’ abstract
Multilayer perceptrons (MLPs) remain fundamental to modern deep learning, yet their algorithmic details are rarely presented in complete, explicit \emph{batch matrix-form}. Rather, most references express gradients per sample or rely on automatic differentiation. Although automatic differentiation can achieve equally high computational efficiency, the usage of batch matrix-form makes the computational structure explicit, which is essential for transparent, systematic analysis, and optimization in settings such as sparse neural networks. This paper fills that gap by providing a mathematically rigorous and implementation-ready specification of MLPs in batch matrix-form. We derive forward and backward equations for all standard and advanced layers, including batch normalization and softmax, and validate all equations using the symbolic mathematics library SymPy. From these specifications, we construct uniform reference implementations in NumPy, PyTorch, JAX, TensorFlow, and a high-performance C++ backend optimized for sparse operations. Our main contributions are: (1) a complete derivation of batch matrix-form backpropagation for MLPs, (2) symbolic validation of all gradient equations, (3) uniform Python and C++ reference implementations grounded in a small set of matrix primitives, and (4) demonstration of how explicit formulations enable efficient sparse computation. Together, these results establish a validated, extensible foundation for understanding, teaching, and researching neural network algorithms.