Skip to main content

Python library designed to integrate Kolmogorov Arnold Networks with recurrent mechanisms.

Project description

TimeKAN

TimeKAN is a Pytorch implementation of integrating Kolmogorov-Arnold Networks (KAN) for temporal data with recurrent neural network architectures (Currently LSTM and GRU). It is still in an experimental stage, the implementation suffer from exploding gradients and vanishing gradients problems but with careful training it can perform well specifically on non-linear/complex temporal data.

Inspired by:

image

In tKANLSTM, KAN layers replace the output gate, computing $o_t = \sigma(\text{KAN}(W_x x_t + W_h h_{t-1}))$. In tKANGRU, they form the candidate hidden state, $\tilde{h}t = \tanh(\text{KAN}(W_x x_t + W_h (r_t \odot h{t-1})))$. The layer basis functions can be Fourier series, Chebyshev polynomials, or splines.

Here's how it can perform on Rossler system signal:

image

The table below compares TimeKAN (using tKANLSTM and spline as basic functions) and a standard bidirectional LSTM on three chaotic datasets available in timekan.utils.datasets. Metrics include Mean Absolute Error (MAE) and training time (seconds) until convergence.

Dataset Model MAE Training Time (s)
Mackey-Glass LSTM 0.0893 0.3346
TimeKAN 0.0822 9.8755
Lorenz LSTM 0.9410 1.1331
TimeKAN 0.7485 7.9437
Rössler LSTM 0.3332 1.3951
TimeKAN 0.2657 12.4172

Installation

Install TimeKAN via pip:

pip install timekan

Alternatively, clone the repository and install locally:

git clone https://github.com/SamerMakni/timekan.git
cd timekan
pip install .

Requirements: Python >= 3.9, PyTorch >= 2.4.0

Usage

Full documentation can be found here.

Here’s a simple example training a TKANLSTM on Mackey-Glass data:

import torch
import torch.nn as nn
from timekan.models.tkan_lstm import tKANLSTM
from timekan.utils.datasets import mackey_glass

class TKANLSTM(nn.Module):
    def __init__(self, input_dim, hidden_dim):
        super().__init__()
        self.tkan = tKANLSTM(
            input_dim=input_dim,
            hidden_dim=hidden_dim,
            return_sequences=False,
            bidirectional=True,
            kan_type='fourier',
            sub_kan_configs={'gridsize': 50, 'addbias': True}
        )
        self.regressor = nn.Linear(hidden_dim * 2, 1)

    def forward(self, x):
        features = self.tkan(x)
        return self.regressor(features).squeeze(-1)

x_train, y_train, x_test, y_test = mackey_glass()

model = TKANLSTM(input_dim=1, hidden_dim=16)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

for epoch in range(10):
    model.train()
    optimizer.zero_grad()
    outputs = model(x_train)
    loss = criterion(outputs, y_train)
    loss.backward()
    optimizer.step()
    print(f"Epoch {epoch + 1}/10, Training MSE: {loss.item():.4f}")

model.eval()
with torch.no_grad():
    test_outputs = model(x_test)
    test_mse = criterion(test_outputs, y_test).item()
    print(f"Test MSE: {test_mse:.4f}")

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

timekan-0.1.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

timekan-0.1.2-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file timekan-0.1.2.tar.gz.

File metadata

  • Download URL: timekan-0.1.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for timekan-0.1.2.tar.gz
Algorithm Hash digest
SHA256 572de73c4928b871dd9a3e6d0ecde10bcf65d7db847114a4ab1bb72032f3180c
MD5 cbba7474f4f00a6e0241700ff6991f64
BLAKE2b-256 f74223feb41f1d462b2a4eafc2c474a053cd633c791a51c6c5ea91da1dcf73b1

See more details on using hashes here.

File details

Details for the file timekan-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: timekan-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.10

File hashes

Hashes for timekan-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ee036844fe02f4145f2aea117b2f8b8a37f8fd09c9218bd9ad49402eee27f39
MD5 6e3778952288807d836728271c100ade
BLAKE2b-256 00af58aafbf60025c1f5e8a307a60a24cf0038a29ab2f36edbd15539615c69c7

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