Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error trying to cluster from numpy #39

Open
carl-offerfit opened this issue Jun 28, 2023 · 1 comment
Open

Error trying to cluster from numpy #39

carl-offerfit opened this issue Jun 28, 2023 · 1 comment

Comments

@carl-offerfit
Copy link

carl-offerfit commented Jun 28, 2023

Hi, I'm not really using pytorch, but I want to use balanced kmeans. My code is as follows:

from torch import from_numpy
from balanced_kmeans import kmeans_equal
...
  # load X, a 23000x59 ndarray
  n_cluster = 50
  X_tensor = from_numpy(X)
  choices, centers = kmeans_equal(X_tensor,
                                  num_clusters=n_cluster,
                                  cluster_size=X.shape[0] // n_cluster)

I get the following error:
RuntimeError: expand(torch.LongTensor{[59]}, size=[]): the number of sizes provided (0) must be greater or equal to the number of dimensions in the tensor (1)

Am I doing something wrong creating my tensor from numpy? I apologize because I am asking more of like a general pytorch question and not really specific to kmeans_pytorch (and tbh I'm a total pytorch newb!) Is there an example anywhere of using kmeans_equal on numpy data? I bet other people would find that useful. Thanks in advance for any tips you can provide!

@carl-offerfit
Copy link
Author

I got a little farther by adding a batch dimension to my data since that seems to be expected by kmeans_equal. So now I use:

X_tensor = torch.reshape(torch.from_numpy(X), (1,X.shape[0], X.shape[1]))

But now I get this error:

 line 165, in kmeans_equal
    selected_ind = torch.argsort(cluster_positions, dim=-1)[:, :cluster_size]
IndexError: too many indices for tensor of dimension 1

Process finished with exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant