team-daniel / KAN

Implementation on how to use Kolmogorov-Arnold Networks (KANs) for classification and regression tasks.
https://daniel-bethell.co.uk/posts/kan
177 stars 20 forks source link

Error running code #6

Closed diego-uva closed 11 hours ago

diego-uva commented 1 month ago

Hello. Thank you very much for sharing your KAN neural networks example.

Running the following cell of your regression example notebook

results = model.train(calhous_dataset, opt="LBFGS", device=device, metrics=(train_mse, test_mse), loss_fn=torch.nn.MSELoss(), steps=25, lamb=0.01, lamb_entropy=2., save_fig=True, img_folder=image_folder)

I have encountered this error:

{ "name": "TypeError", "message": "Module.train() got an unexpected keyword argument 'opt'", "stack": "--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 13 10 mse = torch.nn.functional.mse_loss(predictions, calhous_dataset['test_label']) 11 return mse ---> 13 results = model.train(calhous_dataset, opt=\"LBFGS\", device=device, metrics=(train_mse, test_mse), 14 loss_fn=torch.nn.MSELoss(), steps=25, lamb=0.01, lamb_entropy=2., save_fig=True, img_folder=image_folder)

TypeError: Module.train() got an unexpected keyword argument 'opt'" }

Best regards.

team-daniel commented 1 month ago

Hi Diego,

It seems that the PyKAN package has recently been updated. As you can see below, .train() has been replaced with .fit().

image

Here is an example from the authors of PyKAN:

image

Try replacing this and let me know if this works. If not, I will quickly throw a fix together :)

Best, Dan

diego-uva commented 1 month ago

Thank you very much Dan.

Now the line

results = model.fit(calhous_dataset, opt="LBFGS", device=device, metrics=(train_mse, test_mse),
                      loss_fn=torch.nn.MSELoss(), steps=25, lamb=0.01, lamb_entropy=2., save_fig=True, img_folder=image_folder)

gives this error:


    "name": "_LinAlgError",
    "message": "torch.linalg.lstsq: (Batch element 6): The least squares solution could not be computed because the input matrix does not have full rank (error code: 9).",
    "stack": "---------------------------------------------------------------------------
_LinAlgError                              Traceback (most recent call last)
Cell In[5], line 16
     11     return mse
     13 #results = model.fit(calhous_dataset, opt=\"LBFGS\", device=device, metrics=(train_mse, test_mse),
     14 #                      loss_fn=torch.nn.MSELoss(), steps=25, lamb=0.01, lamb_entropy=2., save_fig=True, img_folder=image_folder)
---> 16 results = model.fit(calhous_dataset, opt=\"LBFGS\", device=device, metrics=(train_mse, test_mse),
     17                       loss_fn=torch.nn.MSELoss(), steps=25, lamb=0.01, lamb_entropy=2., save_fig=True, img_folder=image_folder)

File c:\\Users\\hp\\.conda\\envs\\kan\\lib\\site-packages\\kan\\MultKAN.py:807, in MultKAN.fit(self, dataset, opt, steps, log, lamb, lamb_l1, lamb_entropy, lamb_coef, lamb_coefdiff, update_grid, grid_update_num, loss_fn, lr, start_grid_update_step, stop_grid_update_step, batch, small_mag_threshold, small_reg_factor, metrics, save_fig, in_vars, out_vars, beta, save_fig_freq, img_folder, device, singularity_avoiding, y_th, reg_metric)
    804 test_id = np.random.choice(dataset['test_input'].shape[0], batch_size_test, replace=False)
    806 if _ % grid_update_freq == 0 and _ < stop_grid_update_step and update_grid and _ >= start_grid_update_step:
--> 807     self.update_grid_from_samples(dataset['train_input'][train_id].to(device))
    809 if opt == \"LBFGS\":
    810     optimizer.step(closure)

File c:\\Users\\hp\\.conda\\envs\\kan\\lib\\site-packages\\kan\\MultKAN.py:289, in MultKAN.update_grid_from_samples(self, x)
    287 for l in range(self.depth):
    288     self.forward(x)
--> 289     self.act_fun[l].update_grid_from_samples(self.acts[l])

File c:\\Users\\hp\\.conda\\envs\\kan\\lib\\site-packages\\kan\\KANLayer.py:232, in KANLayer.update_grid_from_samples(self, x)
    230 grid = self.grid_eps * grid_uniform + (1 - self.grid_eps) * grid_adaptive
    231 self.grid.data = extend_grid(grid, k_extend=self.k)
--> 232 self.coef.data = curve2coef(x_pos, y_eval, self.grid, self.k, device=self.device)

File c:\\Users\\hp\\.conda\\envs\\kan\\lib\\site-packages\\kan\\spline.py:167, in curve2coef(x_eval, y_eval, grid, k, device)
    165 # coef shape: (in_dim, outdim, G+k) 
    166 y_eval = y_eval.permute(1,2,0).unsqueeze(dim=3) # y_eval: (in_dim, out_dim, batch, 1)
--> 167 coef = torch.linalg.lstsq(mat.to(device), y_eval.to(device),
    168                           driver='gelsy' if device == 'cpu' else 'gels').solution[:,:,:,0]
    169 return coef.to(device)

_LinAlgError: torch.linalg.lstsq: (Batch element 6): The least squares solution could not be computed because the input matrix does not have full rank (error code: 9)."

I will try to build a simpler dataset to see if I am able to get the example you provide to work.

Thanks again. Diego.