TorchLean API

NN.API.Models.KAN

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:

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.

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

      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.

          • hidden : List

            Hidden KAN widths. Each entry creates one KAN layer followed by tanh.

          • outDim :

            Number of output coordinates/classes.

          • Edge basis family. The default is a compact triangular piecewise-linear basis.

          Instances For
            @[reducible, inline]

            Input shape (batch × inDim) for a KAN config.

            Instances For
              @[reducible, inline]

              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.

                    Instances For