JARS29 / KAN_MLP_MI-BCI

A comparison between KAN and MLP for Motor Imagery Classification
1 stars 0 forks source link

ValueError: Cannot take a larger sample than population when 'replace=False' #1

Open Yagnik12599 opened 5 months ago

Yagnik12599 commented 5 months ago

I am trying to apply the same idea that you did but using a different dataset but running into the following error:

`ValueError Traceback (most recent call last) in <cell line: 1>() 31 32 ---> 33 results_kan = kan_model.train(dataset, opt="LBFGS", steps=steps, loss_fn=loss_fn, batch=batch, lr=lr) 34 35 epoch_end_time_KAN = time.time()

/usr/local/lib/python3.10/dist-packages/kan/KAN.py in train(self, dataset, opt, steps, log, lamb, lamb_l1, lamb_entropy, lamb_coef, lamb_coefdiff, update_grid, grid_update_num, loss_fn, lr, stop_grid_update_step, batch, small_mag_threshold, small_reg_factor, metrics, sglr_avoid, save_fig, in_vars, out_vars, beta, save_fig_freq, img_folder, device) 893 894 train_id = np.random.choice(dataset['train_input'].shape[0], batch_size, replace=False) --> 895 test_id = np.random.choice(dataset['test_input'].shape[0], batch_sizetest, replace=False) 896 897 if % grid_updatefreq == 0 and < stop_grid_update_step and update_grid:

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

ValueError: Cannot take a larger sample than population when 'replace=False'ValueError Traceback (most recent call last) in <cell line: 1>() 31 32 ---> 33 results_kan = kan_model.train(dataset, opt="LBFGS", steps=steps, loss_fn=loss_fn, batch=batch, lr=lr) 34 35 epoch_end_time_KAN = time.time()

/usr/local/lib/python3.10/dist-packages/kan/KAN.py in train(self, dataset, opt, steps, log, lamb, lamb_l1, lamb_entropy, lamb_coef, lamb_coefdiff, update_grid, grid_update_num, loss_fn, lr, stop_grid_update_step, batch, small_mag_threshold, small_reg_factor, metrics, sglr_avoid, save_fig, in_vars, out_vars, beta, save_fig_freq, img_folder, device) 893 894 train_id = np.random.choice(dataset['train_input'].shape[0], batch_size, replace=False) --> 895 test_id = np.random.choice(dataset['test_input'].shape[0], batch_sizetest, replace=False) 896 897 if % grid_updatefreq == 0 and < stop_grid_update_step and update_grid:

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

ValueError: Cannot take a larger sample than population when 'replace=False'`

Not sure what is the real issue here because I checked my dataset size and everything seems to be fine.

sprog1 commented 4 months ago

Hey there, it looks like there might be an error in your code related to the relationship between _dataset['test_input'].shape[0]_and _batch_sizetest. np.random.choice requires the first quantity to be greater than or equal to the second. If your test data sample size is smaller than the set batch size, it can cause this error.

You can try outputting these two quantities separately to see their values. If the test data sample size is indeed smaller than the batch size, you have a couple of options:

  1. Repartition the dataset to ensure that the test data sample size is larger than the batch size.

  2. Modify the logic of _batch_sizetest in your code to make its value consistent with _dataset['testinput'].shape[0].

Hope this helps!