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://github.com/Mattral/KANX/

reference from my previous repo: โ†’ 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.1.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.1-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kanx-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 78904637b9830a30106bb64c0717ab4d3b8ff25905d076580c28c8bb2e37326e
MD5 e7dcf6db83f7f22006e71139b892c620
BLAKE2b-256 0b0314936ca3b8d0c7e49c73e97854a9ec63f9426fdf8a1ea77586b4df646215

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kanx-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b98186ac7df3170bf88eb6c1e0d2d0da0d16e6f910c92eb5fe614944324d2e1
MD5 055e21a99f50e4c6969a153f64408017
BLAKE2b-256 188cee80885c6c86b78cd4d58a12621de26f476f7327c1a13ffdaa4da4d5497a

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