Pytorch_geometric: RuntimeError when trying to use 2-layer GCN in example with Tox21 dataset.

Created on 17 Aug 2020  ยท  2Comments  ยท  Source: rusty1s/pytorch_geometric

โ“ Questions & Help

I'm new to deep learning so I'm not sure whether this is a bug or not. I'm getting RuntimeError: result type Floatcan't be cast to the desired output type Long when running the 2-layer GCN in the introduction with Tox21 dataset.

Here is my code:

import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.datasets import MoleculeNet, Planetoid'

#dataset = Planetoid(root='Planetoid', name='Cora')
dataset = MoleculeNet('MoleculeNet', "Tox21")

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = GCNConv(dataset.num_node_features, 16)
        self.conv2 = GCNConv(16, dataset.num_classes)

    def forward(self, data):
        x, edge_index = data.x, data.edge_index

        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)
        x = self.conv2(x, edge_index)

        return F.log_softmax(x, dim=1)


test = dataset[0]
net = Net()
out = net(test)

The RuntimeError is thrown at this line. The code above runs fine with Planetoid dataset though. Any suggestion for the code above?

Most helpful comment

Hi and sorry for the confusion! The MoleculeNet datasets come with discrete node features of type Long (following the OGB convention here). You first need to convert your input features into continuous embeddings, e.g, by the usage of an AtomEncoder:
```python
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.emb = AtomEncoder(16)
self.conv1 = GCNConv(16, 16)
self.conv2 = GCNConv(16, dataset.num_classes)

def forward(self, data):
    x, edge_index = data.x, data.edge_index
    x = self.emb(x)

    x = self.conv1(x, edge_index)
    x = F.relu(x)
    x = F.dropout(x, training=self.training)
    x = self.conv2(x, edge_index)

    return F.log_softmax(x, dim=1)

All 2 comments

Hi and sorry for the confusion! The MoleculeNet datasets come with discrete node features of type Long (following the OGB convention here). You first need to convert your input features into continuous embeddings, e.g, by the usage of an AtomEncoder:
```python
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.emb = AtomEncoder(16)
self.conv1 = GCNConv(16, 16)
self.conv2 = GCNConv(16, dataset.num_classes)

def forward(self, data):
    x, edge_index = data.x, data.edge_index
    x = self.emb(x)

    x = self.conv1(x, edge_index)
    x = F.relu(x)
    x = F.dropout(x, training=self.training)
    x = self.conv2(x, edge_index)

    return F.log_softmax(x, dim=1)

Thank you. It worked!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SaschaStenger picture SaschaStenger  ยท  4Comments

WMF1997 picture WMF1997  ยท  4Comments

FerranAlet picture FerranAlet  ยท  4Comments

a060146251 picture a060146251  ยท  3Comments

datavistics picture datavistics  ยท  4Comments