KindXiaoming / pykan

Kolmogorov Arnold Networks
MIT License
14.95k stars 1.38k forks source link

CUDA device error #110

Open qihouji opened 5 months ago

qihouji commented 5 months ago

The statement "model (dataset ['train input '])" in the "hellokan. ipynb" encountered an error while running: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

qihouji commented 5 months ago

Try to modify it this way:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = model.to(device) dataset['train_input'] = dataset['train_input'].to(device) model(dataset['train_input'])

But it's useless

SimoSbara commented 5 months ago

This problem is referenced in those two issues #75 #98.

SEUCGX commented 5 months ago

I chose to make some modifications in the source code for my own algorithm. Specifically, I added “to(device)" after the following statement in kan.py. Maybe, this can help you.

self.biases = nn.ModuleList(self.biases).to(device) self.act_fun = nn.ModuleList(self.act_fun).to(device) self.symbolic_fun = nn.ModuleList(self.symbolic_fun).to(device)

When the data set is larger, GPU has a significant advantage, although it is still very slow compared to MLP.

Buanma commented 5 months ago

This will be a solution: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = KAN(width=[2,5,1], grid=5, k=3, seed=0,device=device) dataset = create_dataset(f, n_var=2,device=device) model.train(dataset, opt="LBFGS", steps=20, lamb=0.01, lamb_entropy=10.,device=device)

qihouji commented 5 months ago

This will be a solution: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = KAN(width=[2,5,1], grid=5, k=3, seed=0,device=device) dataset = create_dataset(f, n_var=2,device=device) model.train(dataset, opt="LBFGS", steps=20, lamb=0.01, lamb_entropy=10.,device=device)

It works! thanks

Simba1999 commented 5 months ago

I chose to make some modifications in the source code for my own algorithm. Specifically, I added “to(device)" after the following statement in kan.py. Maybe, this can help you.

self.biases = nn.ModuleList(self.biases).to(device) self.act_fun = nn.ModuleList(self.act_fun).to(device) self.symbolic_fun = nn.ModuleList(self.symbolic_fun).to(device)

When the data set is larger, GPU has a significant advantage, although it is still very slow compared to MLP.

I try many ways, this works, thanks