KindXiaoming / pykan

Kolmogorov Arnold Networks
MIT License
14.61k stars 1.34k forks source link

What Happens if Inputvalues for making predictions with the trained model are Not in Grid Range ? #385

Closed Cxrxnnx closed 1 month ago

Cxrxnnx commented 1 month ago

The Grid Range in the classification example (regression) is [-3,3] for x1 and [-3,3] for x2. I thougt Spline(x) is only defined in [-1,1] (because G=3 and k=3). Now I want to make prediction with the trained Model. When my Input is e.g. x1 = -5 and x2=5 I get an Output even though my Inputvalues are Not in Range [-1,1] Spline(x) is only defined in [-1,1] .. so in this case Spline is automatically set to 0?

seyidcemkarakas commented 1 month ago

Hi @Cxrxnnx Can you share your codes ?

I have got that issue maybe we can make brainestorming together => https://github.com/KindXiaoming/pykan/issues/360

Cxrxnnx commented 1 month ago
from kan import KAN
import matplotlib.pyplot as plt
from sklearn.datasets import make_moons
import torch
import numpy as np

dataset = {}
train_input, train_label = make_moons(n_samples=1000, shuffle=True, noise=0.1, random_state=None)
test_input, test_label = make_moons(n_samples=1000, shuffle=True, noise=0.1, random_state=None)

dataset['train_input'] = torch.from_numpy(train_input.astype(np.float32))
dataset['test_input'] = torch.from_numpy(test_input.astype(np.float32))
dataset['train_label'] = torch.from_numpy(train_label[:,None])
dataset['test_label'] = torch.from_numpy(test_label[:,None])

model = KAN(width=[2,1], grid=3, k=3)
model.train(dataset, opt="LBFGS", steps=5, metrics=(train_acc, test_acc));

model.act_fun[0].grid
image

So I thought this means for calculation Spline(x) the Values for x1 have to be in range [-1,1] (because G= 3 and k=3) and the values for x2 too.

I wanted to check what happens when I use values which are not in this range. So I tried x1=-10 and x2=10

input_values = torch.from_numpy(np.array([[-10, 10]]).astype(np.float32))
prediction = torch.round(model(input_values)).detach().cpu().numpy()[0,0] 
prediction

and got this:

image
KindXiaoming commented 1 month ago

in model.fit, update_grid should automatically handle this issue. grid_range is for case when you don't want to update grid and want to specify the the grid_range manually.

Cxrxnnx commented 1 month ago

Thank you for your answer! But when I trained the Model and then make a forward pass through the traind, final Model with a new Input Vector and this Inputs are Not in the range of the Input Grid Range from my final, traind Model (because I thought while forward pass there are no Parameters that get updated). so in Model.fit the Range gets automaticly but what about Model(x) ?

KindXiaoming commented 1 month ago

Yeah unforunately model(x) isn't adjusting grids, but I guess you could call model.update_grid(x) to update grids