pygcn icon indicating copy to clipboard operation
pygcn copied to clipboard

TypeError: expected torch.FloatTensor (got torch.LongTensor)

Open Cartus opened this issue 6 years ago • 3 comments

Hi tkipf, thank you for sharing the source code.

I ran it on Pytorch 0.4.0 and Python 2.7, but got this type error. However, if I used python 3.5 it can be run. Loading cora dataset...

Traceback (most recent call last):
  File "train.py", line 49, in <module>
    dropout=args.dropout)
  File "build/bdist.linux-x86_64/egg/pygcn/models.py", line 11, in __init__
    self.gc2 = GraphConvolution(nhid, nclass)
  File "build/bdist.linux-x86_64/egg/pygcn/layers.py", line 43, in __init__
    self.bias = Parameter(torch.Tensor(out_features))
TypeError: expected torch.FloatTensor (got torch.LongTensor)

May I ask how to solve this issue? Thank you

Cartus avatar May 31 '18 07:05 Cartus

PyTorch 0.4.0 is currently not supported (see README). Pytorch 0.2.0 is the officially supported version, however 0.3.0 might work as well.

tkipf avatar May 31 '18 10:05 tkipf

On line 47 of train.py you need to cast (labels.max()+1) as an int instead on a torch.LongTensor. so switch

model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=labels.max() + 1,
            dropout=args.dropout)

to

model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=(labels.max() + 1).data.numpy(),
            dropout=args.dropout)

EdwardSmith1884 avatar Jun 19 '18 13:06 EdwardSmith1884

Now if you use Pytorch 0.4 with the updated merge (that @nkolot referenced to) this issue is solved (by having labels.max().item()).

sharifza avatar Jul 05 '18 13:07 sharifza