pythontypespytorchpytorch-geometric

PyTorch Geometric SAGEConv - Expected scalar type Long, but found Float?


I am trying to implement graph neural networks from the torch_geometric library of model types. I am receiving an error: "RuntimeError: expected scalar type Long but found Float" in this line of the SAGEConv module:

"(My Path)\Python310\lib\site-packages\torch_geometric\nn\dense\linear.py", line 147, in forward
    F.linear(x, self.weight, self.bias)
RuntimeError: expected scalar type Long but found Float

I realize that there are similar questions on Stack Overflow, but I don't think that there is clear guidance regarding how to troubleshoot effectively in torch_geometric. I tried to reduce my problem to the simplest code possible:

First, I import SAGEConv:

import torch
from torch_geometric.nn import SAGEConv
sconv = SAGEConv((-1, -1), 64)

Then, I create very basic node and edge tensors for the graph:

x = torch.tensor([[1,0],[2,4],[5,7]]) # Three Node Graph; Two "features" per node
ei = torch.tensor([[1, 1],[0,2]]) # Edge Index Matrix - Node 1 to Node 0 and Node 1 to Node 2

Finally, I call my SAGEConv layer

sconv(x, ei)
>>> (...) RuntimeError: expected scalar type Long but found Float

I can't wrap my head around this because both "x" and "ei" variables are LongTensor type:

x.type()
>>> 'torch.LongTensor'
ei.type()
>>> 'torch.LongTensor'

This has been driving me a little crazy. Any assistance with finding what I've done wrong would be extremely appreciated. In case of a version issue, here is my pip freeze for the packages:

torch==2.0.1
torch_geometric==2.5.0

Edit 1:

I upgraded both torch and torch_geometric to the most recent versions and the error still exists. Though the message is now "expected m1 and m2 to have the same dtype, but got: __int64 != float"


Solution

  • I confused myself by reading the error message incorrectly. Per the maintainer of torch_geometric - the inputs are meant to be float, and not long type:

    https://github.com/pyg-team/pytorch_geometric/discussions/9135#discussioncomment-8998822

    x = torch.tensor([[1,0],[2,4],[5,7]], dtype=torch.float)  
    ei = torch.tensor([[1, 1],[0,2]], dtype=torch.float)