A Tensorflow implementation of KANs
Project description
tfkan
Here is a tensorflow implementation of KAN (Kolmogorov-Arnold Networks (KANs))
How to use
Install from PyPI directly:
pip install tfkan
or clone the repository and run in the terminal:
cd tfkan && pip install .
then you can use tfkan packages:
from tfkan import layers
from tfkan.layers import DenseKAN, Conv2DKAN
Features
The modules in layers are similar to those in tf.keras.layers, so you can use these layers as independent modules to assemble the model (in a TensorFlow style)
from tfkan.layers import DenseKAN
# create model using KAN
model = tf.keras.models.Sequential([
DenseKAN(4),
DenseKAN(1)
])
model.build(input_shape=(None, 10))
When calling model.summary() you can see the model structure and its trainable parameters.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_kan (DenseKAN) (None, 4) 484
dense_kan_1 (DenseKAN) (None, 1) 85
=================================================================
Total params: 569 (2.22 KB)
Trainable params: 401 (1.57 KB)
Non-trainable params: 168 (672.00 Byte)
_________________________________________________________________
When getting started quickly, we can define the optimizer and loss function used for training through model.compile() and model.fit(), or you can use tf.GradientTape() to more finely control the model training behavior and parameter update logic. All model behaviors are the same in Tensorflow2, and you can truly use KAN as an independent module.
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
loss='mse', metrics=['mae'])
history = model.fit(x_train, y_train, epochs=100, batch_size=64)
Layers
The layers currently implemented include:
-
DenseKAN- The basic dense layer, corresponding to
tf.keras.layers.Dense()(ornn.Linear()in torch) in MLP. - Implement the computational logic described in the KANs paper.
- grid update method is available but it will not be automatically used.
- The basic dense layer, corresponding to
-
ConvolutionKAN- THe basic convolution layer, provide
Conv1DKAN,Conv2DKANandConv3DKANfor classical 1D, 2D and 3D convolution operations. - The implementation logic is similar to
ConvinTensorflow, expanding convolution operations into matrix multiplication, and then replacing MLP dense kernel withDenseKANkernel.
- THe basic convolution layer, provide
About Grid Update
The grid adaptive update is an important feature mentioned in KANs paper. In this tensorflow implementation of KANs, each KAN layer has two method used to implement this feature:
self.update_grid_from_samples(...)- adaptively update the spline grid points using input samples given.
- this can help the spline grid adapt to the numerical range of the input, avoiding the occurrence of input features outside the support set of the grid (resulting in outputs that are equal to 0, as spline functions outside the support set are not defined)
self.extend_grid_from_samples(...)- extend the spline grid for given
gird_size - this can help to make the spline activation output more smooth, thereby enhancing the model approximation ability.
- extend the spline grid for given
You can call it in custom training logic or use Tensorflow Callbacks
- In custom training logic
def train_model(...):
# training logic
...
# call update_grid_from_samples
for layer in model.layers:
if hasattr(layer, 'update_grid_from_samples'):
layer.update_grid_from_samples(x)
x = layer(x)
- or use Tensorflow
Callbacks
# define update grid callback
class UpdateGridCallback(tf.keras.callbacks.Callback):
def on_epoch_begin(self, epoch, logs=None):
"""
update grid before new epoch begins
"""
global x_train, batch_size
x_batch = x_train[:batch_size]
if epoch > 0:
for layer in self.model.layers:
if hasattr(layer, 'update_grid_from_samples'):
layer.update_grid_from_samples(x_batch)
x_batch = layer(x_batch)
then add it into model.fit()
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 tfkan-0.1.0.tar.gz.
File metadata
- Download URL: tfkan-0.1.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b2f3c961c6f7123c74995b7de6f060f5f87a556ab47cd8276f98e534bd326f9
|
|
| MD5 |
fb153aa829d622c5d75c6dee52ab70a2
|
|
| BLAKE2b-256 |
804c7427b1cfad4ad4a1178d66d05c383390e20982a2a98f289c982fee38d256
|
File details
Details for the file tfkan-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tfkan-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2c4d32012598b1b6708bdb128de6f88f4c2f452f4980203a052f6f943017c48
|
|
| MD5 |
8a847d225a591afeb8001c4d459899fa
|
|
| BLAKE2b-256 |
36460030f614bda142046b79b79a8c42bf3dd45e798429f8dbc661d00f39cdba
|