Production-grade Kolmogorov-Arnold Networks โ TensorFlow + PyTorch + ONNX.
Project description
๐ kanx
Production-grade Kolmogorov-Arnold Networks โ TensorFlow + PyTorch + ONNX
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:
- Open an issue first for any non-trivial change.
- Run
bash scripts/test.shโ it must stay green. - Add a test for any new behaviour (we're at 94% coverage; let's keep climbing).
- Update
roadmap.mdwhen 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d67632e88110ca74841ff7f3a227c06266401cd5ae69040b1665ffb23348f3d
|
|
| MD5 |
00879faa7b46cd237755ec772b6be33c
|
|
| BLAKE2b-256 |
9e077599245f5f367f2c4534b372c404e34d6e6c37acf47cc5dcaaa5295871b1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8635e5bc8366d697359f2d5c91b7a93d0b79bc7fb7e5d84f711d26f9d3864e5
|
|
| MD5 |
6ec1473f9a403e0dc649e6dca1b2b10f
|
|
| BLAKE2b-256 |
977251b1eedde0294f42356fa2c9483b69c0d177175943a1e4495383a80dafc9
|