Kolmogorov-Arnold Networks #
KAN layers replace each scalar edge by a small trainable one-dimensional function. TorchLean keeps
that structure visible: an edge family first expands every scalar input into basis features, and the
KAN layer learns one coefficient per (output, input, basis) edge.
The first built-in family uses triangular piecewise-linear hats. Users can add another family by
constructing KANEdgeFamily: provide a basis dimension and a TorchLean model that maps
Vec inDim to Vec (inDim * basisDim).
References:
- Z. Liu et al., "KAN: Kolmogorov-Arnold Networks", arXiv:2404.19756.
- C. de Boor, "A Practical Guide to Splines", Springer, 1978/2001.
Backend-compatible KAN edge family.
An edge family turns each scalar input coordinate into basisDim features. A KAN layer then applies
a learned linear map to all expanded features. The basis is a TorchLean model fragment, not an
arbitrary Lean callback, so the resulting KAN can run in eager, compiled, CPU, and CUDA training
paths supported by the underlying operations.
- name : String
Short label shown in model summaries and training metadata.
- basisDim : ℕ
Number of basis features produced per scalar input coordinate.
- basis (inDim : ℕ) : Sequential (Spec.Shape.dim inDim Spec.Shape.scalar) (Spec.Shape.dim (inDim * self.basisDim) Spec.Shape.scalar)
Basis expansion for an unbatched vector of length
inDim.
Instances For
Configuration for triangular piecewise-linear KAN edge bases.
The basis functions are hats centered at the integer knots $0,\ldots,\mathrm{gridSize}-1$. The input is
multiplied by inputScale before the hats are evaluated. For normalized data in $[0,1]$, setting
$\mathrm{inputScale}=\mathrm{gridSize}-1$ spreads the grid across the full interval.
- gridSize : ℕ
Number of knots, hence the number of basis functions per scalar coordinate.
- inputScale : ℕ
Scale applied before basis evaluation; use $\mathrm{gridSize}-1$ for normalized $[0,1]$ inputs.
Instances For
Instances For
Expand x : Vec inDim to all triangular basis features.
The output is flattened row-major from a (gridSize × inDim) table:
$[\operatorname{basis}_0(x_0),\ldots,\operatorname{basis}_0(x_n), \operatorname{basis}_1(x_0),\ldots]$.
Each basis value is $\operatorname{ReLU}(1-|\mathrm{inputScale}\,x_i-k|)$, expressed directly in the ordinary TorchLean op language rather than through an opaque spline evaluator.
Instances For
Turn piecewise-linear triangular bases into a general KAN edge family.
Instances For
Configuration for a KAN over batched row vectors.
- batch : ℕ
Leading minibatch dimension.
- inDim : ℕ
Number of scalar input coordinates.
- outDim : ℕ
Number of output coordinates/classes.
- edge : KANEdgeFamily
Edge basis family. The default is a compact triangular piecewise-linear basis.
Instances For
Input shape (batch × inDim) for a KAN config.
Instances For
Output shape (batch × outDim) for a KAN config.
Instances For
One unbatched KAN layer.
The layer first applies the selected edge basis to every input coordinate, then learns coefficients
with an ordinary linear map from the expanded features to outDim.
Instances For
Recursive unbatched KAN stack. Hidden layers use tanh; the final layer is linear in bases.
Instances For
Build a batched KAN model.
Task semantics are deliberately not baked into the model name: use Trainer.new with
task := .regression, .classification, .crossEntropy, or .custom ... with the same KAN
constructor.