Skip to main content

Production-grade Kolmogorov-Arnold Networks โ€” TensorFlow + PyTorch + ONNX.

Project description

๐Ÿš€ kanx

Production-grade Kolmogorov-Arnold Networks โ€” TensorFlow + PyTorch + ONNX

CI PyPI Python License Coverage

KAN vs MLP benchmark

265ร— lower test MSE than an MLP with 5ร— fewer parameters. One library. Two backends. Real ONNX export. Docker + Kubernetes ready.


โšก The 10-line magic moment

=== "๐Ÿ”ท TensorFlow (default)"

```python
from kanx import KAN, train, load_config
import numpy as np

X = np.random.uniform(-1, 1, (512, 2)).astype("float32")
y = np.sin(np.pi * X[:, :1])

model, _ = train(load_config("configs/default.yaml"), X, y)
model.predict(X[:3])
```

=== "๐Ÿ”ถ PyTorch"

```python
from kanx.torch import KAN, Trainer
import torch

model = KAN([2, 64, 1])
X = torch.randn(512, 2); y = torch.sin(torch.pi * X[:, :1])

Trainer(model).fit(X, y, epochs=30, lr=1e-2)
model.predict([[0.5, 0.2]])
```

=== "๐ŸŒ REST API"

```bash
docker run --rm -p 8000:8000 ghcr.io/mattral/kanx:latest
curl -X POST http://localhost:8000/api/predict \
     -H 'content-type: application/json' \
     -d '{"x": [[0.1, -0.2], [0.5, 0.7]]}'
```

=== "๐Ÿ”„ ONNX export"

```python
from kanx.torch import KAN, export_onnx
import torch

model = KAN([2, 64, 1])
export_onnx(model, "kan.onnx", sample_input=torch.zeros(1, 2))
# โ†’ run anywhere with ONNX Runtime, TensorRT, OpenVINO, CoreML, โ€ฆ
```

๐Ÿง  Why kanx?

Research-y KAN repos kanx
Two backends (TF + Torch) โŒ โœ…
Vectorized B-spline โš ๏ธ Python loops โœ… pure einsum
ONNX export โŒ โœ… TF & Torch
FastAPI service โŒ โœ… hot-swap, 5 endpoints
Docker + K8s + HPA โŒ โœ…
CI/CD + release pipeline โŒ โœ… PyPI + GHCR + Pages
Property-based tests โŒ โœ… Hypothesis
Numerical-contract tests โŒ โœ… partition of unity
Test coverage ~30% 94%
Performance budgets โŒ โœ… regression alarms

๐Ÿ“Š Benchmarks

Synthetic 2-D regression target y = sin(ฯ€ยทxโ‚) + cos(2ฯ€ยทxโ‚‚), 30 epochs, Adam(lr=1e-2), batch=128, CPU.

Model Params Train (s) Infer 4k (ms) Test MSE
KAN[2,16,1] 432 4.18 35.71 6.4 ร— 10โปโต
KAN[2,32,1] 864 5.31 28.50 1.7 ร— 10โปโต
MLP[2,64,64,1] 4 417 2.04 6.88 4.5 ร— 10โปยณ

Reproduce with bash scripts/benchmark.sh.


๐Ÿ“ฆ Install

pip install kanx              # core (TensorFlow)
pip install "kanx[torch]"     # +PyTorch backend
pip install "kanx[api]"       # +FastAPI service
pip install "kanx[all]"       # everything

Or development install:

git clone https://github.com/Mattral/Kolmogorov-Arnold-Networks
cd Kolmogorov-Arnold-Networks
pip install -e ".[dev,api,torch]"
bash scripts/test.sh          # 95 tests, 94% coverage

๐Ÿงช What's in the box

kanx/
โ”œโ”€โ”€ src/kanx/             # library
โ”‚   โ”œโ”€โ”€ layers.py         # TF KANLinear + B-spline primitives
โ”‚   โ”œโ”€โ”€ model.py          # TF KAN sequential
โ”‚   โ”œโ”€โ”€ train.py          # train() + optim/loss factories
โ”‚   โ”œโ”€โ”€ inference.py      # predict + save/load
โ”‚   โ”œโ”€โ”€ onnx_export.py    # TF โ†’ ONNX (via tf2onnx)
โ”‚   โ”œโ”€โ”€ config.py         # YAML config + strict validator
โ”‚   โ”œโ”€โ”€ utils.py          # logging, seeding
โ”‚   โ”œโ”€โ”€ __main__.py       # CLI: `python -m kanx`
โ”‚   โ””โ”€โ”€ torch/            # โšก PyTorch backend (parallel surface)
โ”‚       โ”œโ”€โ”€ layers.py     # Torch KANLinear + B-spline
โ”‚       โ”œโ”€โ”€ model.py      # Torch KAN + save/load
โ”‚       โ”œโ”€โ”€ trainer.py    # Trainer.fit(...)
โ”‚       โ””โ”€โ”€ onnx_export.py # Torch โ†’ ONNX
โ”œโ”€โ”€ api/                  # FastAPI service
โ”œโ”€โ”€ tests/                # 95 tests โ€” unit + property + perf + E2E
โ”œโ”€โ”€ benchmarks/           # KAN vs MLP harness
โ”œโ”€โ”€ examples/             # quickstart, function_fit, MNIST
โ”œโ”€โ”€ configs/              # YAML training configs
โ”œโ”€โ”€ notebooks/            # Colab-ready notebooks
โ”œโ”€โ”€ k8s/                  # Deployment + Service + Ingress + HPA + PVC
โ”œโ”€โ”€ docs/                 # MkDocs Material site
โ””โ”€โ”€ .github/workflows/    # CI + release (PyPI + GHCR + Pages)

๐ŸŒ REST API

uvicorn api.app:app --port 8000           # or docker compose up
Method Path Purpose
GET /api/health Liveness + model load source
GET /api/info Version + TF version + model summary
POST /api/predict Inference (single or batch)
POST /api/load Hot-swap checkpoint
POST /api/reset Re-init from KANX_CONFIG

All endpoints validate at the boundary: wrong feature count โ†’ 400, oversized batch โ†’ 413, missing checkpoint โ†’ 404. The startup contract loads KANX_CHECKPOINT if it exists, otherwise falls back to a fresh model built from KANX_CONFIG.


๐Ÿณ Docker / โ˜ธ๏ธ Kubernetes

docker run --rm -p 8000:8000 ghcr.io/mattral/kanx:latest
kubectl apply -f k8s/        # Deployment + Service + Ingress + HPA + PVC

Manifests ship with rolling updates, readiness/liveness probes on /api/health, an HPA (2 โ†” 10 replicas, CPU-target 70%) and a PVC for the model registry.


๐Ÿ› ๏ธ CLI

python -m kanx info                                          # versions
python -m kanx train --config configs/default.yaml           # train
python -m kanx predict --checkpoint model.keras --input X.json

๐Ÿ“š Documentation

Full docs are published as a MkDocs Material site:

โ†’ https://mattral.github.io/Kolmogorov-Arnold-Networks/

Or read the source files under documentations/.


๐Ÿงฌ Research positioning

KANs were introduced by Liu et al. (arXiv:2404.19756). This repo is not a reproduction of the paper โ€” it's an opinionated engineering layer that takes the core idea (learnable spline activations on edges, SiLU residual) and packages it for real-world MLOps:

  • Two backends so you can drop kanx into either TF or Torch stacks.
  • ONNX so you can deploy anywhere.
  • A REST API + Docker so you can serve.
  • A test suite that catches bugs before users do.

๐Ÿค Contributing

PRs welcome. Please:

  1. Open an issue first for any non-trivial change.
  2. Run bash scripts/test.sh โ€” it must stay green.
  3. Add a test for any new behaviour (we're at 94% coverage; let's keep climbing).
  4. Update roadmap.md when shipping a feature.

๐Ÿ“œ License

Apache 2.0. Use it. Ship it. Tell us when you do.

Built with care so KANs are actually usable, not just academically interesting.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kanx-0.1.0.tar.gz (38.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kanx-0.1.0-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file kanx-0.1.0.tar.gz.

File metadata

  • Download URL: kanx-0.1.0.tar.gz
  • Upload date:
  • Size: 38.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for kanx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1d67632e88110ca74841ff7f3a227c06266401cd5ae69040b1665ffb23348f3d
MD5 00879faa7b46cd237755ec772b6be33c
BLAKE2b-256 9e077599245f5f367f2c4534b372c404e34d6e6c37acf47cc5dcaaa5295871b1

See more details on using hashes here.

File details

Details for the file kanx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kanx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for kanx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8635e5bc8366d697359f2d5c91b7a93d0b79bc7fb7e5d84f711d26f9d3864e5
MD5 6ec1473f9a403e0dc649e6dca1b2b10f
BLAKE2b-256 977251b1eedde0294f42356fa2c9483b69c0d177175943a1e4495383a80dafc9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page