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

Expected object of scalar type Long but got scalar type Int for sequence elment 1 in sequence argument at position #1 'tensors' #19

Closed
TheNomet opened this issue Apr 7, 2019 · 4 comments
Labels

Comments

@TheNomet
Copy link

TheNomet commented Apr 7, 2019

Hello, I was trying to run one of your examples in the documentation and found this.

import torch
from torch_cluster import knn_graph

x = torch.tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
edge_index = knn_graph(x, k=2, loop=False)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-58-47d38d59d882> in <module>
      3 
      4 x = torch.tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
----> 5 edge_index = knn_graph(x, k=2, loop=False)

~\Anaconda3\lib\site-packages\torch_cluster\knn.py in knn_graph(x, k, batch, loop)
    105     """
    106 
--> 107     edge_index = knn(x, x, k if loop else k + 1, batch, batch)
    108     if not loop:
    109         row, col = edge_index

~\Anaconda3\lib\site-packages\torch_cluster\knn.py in knn(x, y, k, batch_x, batch_y)
     75     row, col = row.view(-1)[mask], col.view(-1)[mask]
     76 
---> 77     return torch.stack([row, col], dim=0)
     78 
     79 

RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence elment 1 in sequence argument at position #1 'tensors'
@rusty1s
Copy link
Owner

rusty1s commented Apr 7, 2019

x must be a floating type tensor, so you should either use torch.Tensor or torch.tensor(..., dtype=torch.float).

@TheNomet
Copy link
Author

TheNomet commented Apr 8, 2019

import torch
from torch_cluster import knn_graph
x = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]])
edge_index = knn_graph(x, k=2, loop=False)
Traceback (most recent call last):
  File "C:\Users\fiore\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-9bf18e4256be>", line 5, in <module>
    edge_index = knn_graph(x, k=2, loop=False)
  File "C:\Users\fiore\Anaconda3\lib\site-packages\torch_cluster\knn.py", line 107, in knn_graph
    edge_index = knn(x, x, k if loop else k + 1, batch, batch)
  File "C:\Users\fiore\Anaconda3\lib\site-packages\torch_cluster\knn.py", line 77, in knn
    return torch.stack([row, col], dim=0)
RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence elment 1 in sequence argument at position #1 'tensors'
import torch
from torch_cluster import knn_graph
x = torch.tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]]).float()
x

Out[6]: 
tensor([[-1., -1.],
        [-1.,  1.],
        [ 1., -1.],
        [ 1.,  1.]])

edge_index = knn_graph(x, k=2, loop=False)

Traceback (most recent call last):
  File "C:\Users\fiore\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-08b386477039>", line 1, in <module>
    edge_index = knn_graph(x, k=2, loop=False)
  File "C:\Users\fiore\Anaconda3\lib\site-packages\torch_cluster\knn.py", line 107, in knn_graph
    edge_index = knn(x, x, k if loop else k + 1, batch, batch)
  File "C:\Users\fiore\Anaconda3\lib\site-packages\torch_cluster\knn.py", line 77, in knn
    return torch.stack([row, col], dim=0)
RuntimeError: Expected object of scalar type Long but got scalar type Int for sequence elment 1 in sequence argument at position #1 'tensors'

@rusty1s
Copy link
Owner

rusty1s commented Apr 8, 2019

You are using Windows? This seems kinda related to rusty1s/pytorch_sparse#13 where Windows/NumPy converts types differently than Linux/NumPy. I pushed a potential fix to master. It would be great if you could validate it.

@github-actions
Copy link

This issue had no activity for 6 months. It will be closed in 2 weeks unless there is some new activity. Is this issue already resolved?

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

No branches or pull requests

2 participants