Implementing KAN from Scratch and Testing Against micrograd

A developer implemented KAN from scratch and tested it against micrograd, revealing engineering challenges.
This article covers a Reddit developer's from-scratch implementation of a full KAN network tested against the micrograd autodiff library. KAN uses learnable one-dimensional functions (typically B-spline parameterized) on connections instead of MLP scalar weights and fixed activations, fundamentally changing forward and gradient propagation. From-scratch implementation requires solving B-spline basis construction, forward tensor alignment, and edge-wise gradient chain derivation. micrograd was chosen because its scalar-level computation graph is fully transparent, enabling precise verification of function parameter gradients. This reproduction-plus-comparison approach exposes gaps between mathematical formulation and engineering implementation and offers a learnable path for understanding new architectures.
In an era where deep learning frameworks are highly encapsulated, writing a neural network from scratch has long been the best way to understand its internal mechanics. Recently, a developer in the Reddit community shared their practice: fully implementing a complete KAN network from scratch and testing it against the lightweight automatic differentiation library micrograd.
Since its introduction, KAN (Kolmogorov-Arnold Network) has attracted much attention for its design based on the Kolmogorov-Arnold representation theorem. It replaces the fixed activation functions in traditional MLPs with learnable one-dimensional functions, so that weights are no longer simple scalars but functions distributed on each connection. What kind of implementation complexity and performance does this structural difference bring? This developer's experiment provides a rare window for observation.

The Core Design Logic of KAN Networks
To understand the significance of this implementation, we first need to return to the basic architecture of KAN. Traditional multilayer perceptrons use fixed nonlinear activation functions at each node, and connection weights are scalars; whereas KAN places learnable functions on the connections (edges), and each function is usually parameterized by B-spline basis functions. This means that the forward propagation of the network is no longer just matrix multiplication plus activation, but requires computing the value of the function edge by edge.
The direct consequence of this design is a change in the computation graph and the way gradients propagate. When implementing from scratch, the developer must handle the construction of B-spline basis functions, the updating of control points, and the gradient chain for each function parameter during backpropagation. Compared with directly calling PyTorch or TensorFlow, manually deriving these steps exposes the real engineering difficulties of KAN.
The Kolmogorov-Arnold representation theorem is the theoretical cornerstone of KAN. The theorem states that any multivariate continuous function can be represented as a composition of a finite number of univariate continuous functions and addition operations, specifically in the form f(x_1, ..., x_n) = Σ_{q=1}^{2n+1} Φ_q(Σ_{p=1}^n φ_{q,p}(x_p)), where φ and Φ are both univariate functions. Traditional MLPs, inspired by the universal approximation theorem, approximate arbitrary functions with fixed activation functions plus linear weights; whereas KAN directly uses this theorem as an architectural blueprint and parameterizes univariate functions as learnable spline functions, thereby theoretically achieving stronger function representation efficiency. This design also gives KAN a certain degree of interpretability: the shape of the function on each connection can be visualized, helping to understand how input features are transformed.
Key Steps in Implementing from Scratch
To build a usable KAN network from scratch, the developer needs to complete at least several core modules. The first is the basis function system—KAN usually uses B-splines to fit the function on each edge, which requires correctly setting the grid range, knot vector, and basis function degree. If the basis functions are written incorrectly, the expressive power of the entire network will be distorted.
The second is assembling forward propagation. The output of each KAN layer is a combination of inputs transformed by a batch of one-dimensional functions, and the computation order and dimension alignment are more error-prone than in MLPs. The developer needs to verify tensor shapes layer by layer to ensure that the function inputs and outputs on each edge correspond one-to-one.
Backpropagation is another hurdle. Although automatic differentiation frameworks can handle most gradient computations, implementing from scratch means either manually writing the chain rule or relying on a minimalist automatic differentiation engine such as micrograd. The advantage of micrograd is that its computation graph is completely transparent, and the gradient of every scalar operation can be traced, which is exactly suitable for verifying the correctness of function parameter gradients in KAN.
B-spline is the core tool for parameterizing one-dimensional functions in KAN. It is formed by a linear combination of a set of control points (coefficients) and basis functions defined on a knot vector, and has excellent properties such as local support and continuous differentiability. In KAN, the function on each edge is usually represented as f(x) = Σ_i c_i B_i(x), where B_i is a B-spline basis function of degree k, and c_i are learnable control points. Implementation requires correctly setting the grid range, the knot vector (usually uniform or extended uniform knots), and the basis function degree (commonly 3, i.e., cubic B-splines). The higher the degree, the smoother the function, but the computational cost also increases accordingly; the grid resolution affects the fitting accuracy of the function. If the basis functions are constructed incorrectly, such as a non-monotonic knot vector or a mismatch between the number of control points and the number of basis functions, the function representation will be distorted and even gradients may become abnormal.
Comparative Testing Against micrograd
micrograd is a tiny automatic differentiation engine implemented by Andrej Karpathy, known for scalar-level backpropagation. Choosing micrograd as the comparison target is a clear approach: use a minimalist framework whose gradient source anyone can understand to verify whether gradient computation in the KAN implementation is accurate.
The tests may cover several levels. The first is consistency of forward output—under the same parameters, the forward results of the KAN implementation should match the reference results computed scalar by scalar. The second is comparison of gradient values, which is the place where errors are most easily hidden in from-scratch implementations: the partial derivatives of B-spline basis functions with respect to control points, and the accumulation of backpropagation between layers, all need to align with the computation graph results of micrograd. The third is end-to-end training performance, observing whether the network can converge normally after gradient verification passes.
The value of this kind of comparative testing is not about which is faster, but about exposing the gap between KAN's mathematical formulation and its engineering implementation. A small oversight in symbolic derivation may be silently compensated for in a high-level framework, but it will immediately become visible in scalar-level gradient comparisons.
The core of micrograd is a scalar-level automatic differentiation engine that computes gradients by building a computation graph (DAG) and performing reverse topological sorting. Each scalar operation (such as addition, multiplication, tanh) generates a node that records its operation type and inputs/outputs, and during backpropagation the chain rule is applied in reverse order along the graph. This design makes the gradient computation process completely transparent, and the gradient of any intermediate variable can be checked directly, making it very suitable for debugging gradient errors in custom networks. Unlike tensor-level frameworks such as PyTorch, micrograd does not involve vectorized optimization, so it runs more slowly, but precisely because of this, it becomes an ideal reference for verifying mathematical correctness. In a KAN implementation, the developer can decompose the updates of B-spline basis functions and control points into scalar operations and use micrograd to verify gradients point by point, ensuring that the partial derivative of each control point is consistent with the analytical derivation.
What This Means for Learners
Implementing KAN from scratch and comparing it against micrograd is a practical path to understanding this new architecture today. KAN papers focus on representation capability and interpretability, but when it comes to code, the engineering details of B-splines, the organization of the computation graph, and the precise propagation of gradients are the parts that truly determine whether an implementation succeeds.
For developers who want to deeply understand neural networks, this method of "reproduction plus comparison" is also worth borrowing: first use a minimalist engine to verify mathematical correctness, then migrate to production frameworks to optimize performance. Although this Reddit share did not disclose complete benchmark data, it points in a direction—the popularization of new architectures often requires more practical samples that disassemble them from the ground up.
At present, the specific code structure and test metrics of this implementation have not yet been expanded in the source material. Readers who are interested can follow the original post to obtain complete implementation details.
One-line Summary
A developer implemented a KAN network from scratch and tested it against micrograd, revealing engineering implementation difficulties.
Paragraph Summary
This article introduces the practice of a Reddit community developer who implemented a complete KAN network from scratch and tested it against the micrograd automatic differentiation library. KAN is based on the Kolmogorov-Arnold representation theorem and places learnable one-dimensional functions (usually parameterized by B-splines) on connections, replacing the scalar weights and fixed activation functions in MLPs, which fundamentally changes forward propagation and gradient propagation. Implementing from scratch requires overcoming core difficulties such as constructing B-spline basis functions, aligning forward tensors, and deriving edge-by-edge gradient chains. micrograd was chosen as the comparison because its scalar-level computation graph is completely transparent and can precisely verify the correctness of function parameter gradients in KAN. This method of reproduction plus comparison not only exposes the gap between mathematical formulation and engineering implementation, but also provides a learnable path for understanding new architectures.
Related articles

ChatGPT Plus Subscription Pitfall Guide: Go/Plus/Pro Tier Comparison and Quick Activation
A complete ChatGPT Plus subscription guide based on a Bilibili tutorial: from registering an account, comparing Go/Plus/Pro tiers, to the practical steps for quickly completing your subscription.

GPT-6.2 Sol/Astra Released! A Step-by-Step ChatGPT Plus Subscription Guide and Tier Comparison
GPT-6.2 Sol/Astra is out—here's a ChatGPT Plus subscription guide: register an account, compare Go/Plus/Pro tiers, and subscribe in a minute.

Comparing Three Channels for Subscribing to GPT Plus in China: Is Website Top-Up the Easiest?
How to subscribe to GPT Plus in China? This article compares website top-up, credit card, and mobile top-up channels—process, cost, and risk—with steps and tips to help you choose wisely.