KindXiaoming / pykan

Kolmogorov Arnold Networks
MIT License
13.64k stars 1.2k forks source link

Low R^2 Scores in KAN Model Regression #44

Open uozveren opened 2 months ago

uozveren commented 2 months ago

I am currently employing the KAN model for a basic engineering regression problem and have observed unexpectedly low R^2 scores, even for the simplest configurations. Given this, I suspect there might be an issue with the implementation in my script.

Would you be able to review my code to identify any potential errors or suggest improvements? I have attached the script for your reference. Any insights or recommendations you could provide would be greatly appreciated as I aim to optimize the model's performance.

@import pandas as pd import torch from kan import KAN from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, explained_variance_score, max_error import tkinter as tk from tkinter import filedialog

def load_data(): root = tk.Tk() root.withdraw() # GUI ekranını gizle filepath = filedialog.askopenfilename(title="Excel dosyası seçin", filetypes=(("Excel files", ".xlsx .xls"), ("All files", "."))) root.destroy() if not filepath: print("Dosya seçilmedi.") return None, None data = pd.read_excel(filepath) X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values return X, y

def train_and_evaluate(X, y): X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42) X_train = torch.tensor(X_train, dtype=torch.float32) X_test = torch.tensor(X_test, dtype=torch.float32) y_train = torch.tensor(y_train, dtype=torch.float32).view(-1, 1) y_test = torch.tensor(y_test, dtype=torch.float32).view(-1, 1)

grids = [5, 10, 20, 50, 100]
for grid in grids:
    model = KAN(width=[X_train.shape[1], 50, 100, 1], grid=grid, k=3, noise_scale=0.1, seed=0)
    model.train({'train_input': X_train, 'train_label': y_train}, opt='LBFGS', steps=50, lamb=0.01)

    y_train_pred = model.predict(X_train)
    y_test_pred = model.predict(X_test)

    print(f"Results for Grid Size: {grid}")
    print_metrics(y_train, y_train_pred, "Training")
    print_metrics(y_test, y_test_pred, "Testing")

def print_metrics(true_values, predicted_values, dataset_type): mse = mean_squared_error(true_values, predicted_values) mae = mean_absolute_error(true_values, predicted_values) r2 = r2_score(true_values, predicted_values) evs = explained_variance_score(true_values, predicted_values) max_err = max_error(true_values, predicted_values)

print(f"{dataset_type} - Mean Squared Error: {mse}")
print(f"{dataset_type} - Mean Absolute Error: {mae}")
print(f"{dataset_type} - R^2 Score: {r2}")
print(f"{dataset_type} - Explained Variance Score: {evs}")
print(f"{dataset_type} - Max Error: {max_err}")

def main(): X, y = load_data() if X is not None and y is not None: train_and_evaluate(X, y)

if name == "main": main()

KindXiaoming commented 2 months ago

Hi, might be good to run a baseline model (say linear regression, MLP) to see how that performs. And then we can have a sense of how hard this problem is, and have a sense of whether it's your implementation error or it's KAN's problem.