TorchLean API

NN.API.Models.KAN

Kolmogorov-Arnold Network Helpers #

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:

@[reducible, inline]

Unbatched vector shape used by KAN edge bases.

Instances For
    @[reducible, inline]

    Matrix shape used by batched KAN models and basis tables.

    Instances For

      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
        @[reducible, inline]

        Shape of the edge-basis expansion for inDim scalar inputs.

        Instances For

          Configuration for triangular piecewise-linear KAN edge bases.

          The basis functions are hats centered at the integer knots 0, ..., gridSize - 1. The input is multiplied by inputScale before the hats are evaluated. For normalized data in [0, 1], setting inputScale = 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 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: [basis_0(x_0), ..., basis_0(x_n), basis_1(x_0), ...].

            Each basis value is relu(1 - |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.

                • seedBase :

                  Base seed used for learned edge coefficients and biases.

                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
                      def NN.API.nn.models.kanLayer (inDim outDim : ) (edge : KANEdgeFamily) (seedW seedB : := 0) :

                      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
                        def NN.API.nn.models.kanGo (edge : KANEdgeFamily) (inDim : ) (hidden : List ) (outDim seed : ) :

                        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