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?
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!
Most helpful comment
Hi and sorry for the confusion! The
MoleculeNetdatasets come with discrete node features of typeLong(following the OGB convention here). You first need to convert your input features into continuous embeddings, e.g, by the usage of anAtomEncoder:```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)