RuntimeError: result type Floatcan't be cast to the desired output type Long
See original GitHub issue❓ Questions & Help
<type error when using GCNConv>import rdkit
from torch_geometric.datasets import MoleculeNet
dataset = MoleculeNet(root='/tmp',name = 'HIV')
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
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)
print(torch.cuda.is_available())
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net().to(device)
print(type(dataset))
print("dataset:",dataset[0])
data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(200):
optimizer.zero_grad()
out = model(data)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
model.eval()
_, pred = model(data).max(dim=1)
correct = float (pred[data.test_mask].eq(data.y[data.test_mask]).sum().item())
acc = correct / data.test_mask.sum().item()
print('Accuracy: {:.4f}'.format(acc))
**Error:**
> RuntimeError Traceback (most recent call last)
> <ipython-input-5-b875e5d5d8e5> in <module>()
> 39 for epoch in range(200):
> 40 optimizer.zero_grad()
> ---> 41 out = model(data)
> 42 loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
> 43 loss.backward()
>
> 4 frames
> /usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
> 720 result = self._slow_forward(*input, **kwargs)
> 721 else:
> --> 722 result = self.forward(*input, **kwargs)
> 723 for hook in itertools.chain(
> 724 _global_forward_hooks.values(),
>
> <ipython-input-5-b875e5d5d8e5> in forward(self, data)
> 21 x, edge_index = data.x, data.edge_index
> 22
> ---> 23 x = self.conv1(x, edge_index)
> 24 x = F.relu(x)
> 25 x = F.dropout(x, training=self.training)
>
> /usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
> 720 result = self._slow_forward(*input, **kwargs)
> 721 else:
> --> 722 result = self.forward(*input, **kwargs)
> 723 for hook in itertools.chain(
> 724 _global_forward_hooks.values(),
>
> /usr/local/lib/python3.7/site-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)
> 146 edge_index, edge_weight = gcn_norm( # yapf: disable
> 147 edge_index, edge_weight, x.size(self.node_dim),
> --> 148 self.improved, self.add_self_loops, dtype=x.dtype)
> 149 if self.cached:
> 150 self._cached_edge_index = (edge_index, edge_weight)
>
> /usr/local/lib/python3.7/site-packages/torch_geometric/nn/conv/gcn_conv.py in gcn_norm(edge_index, edge_weight, num_nodes, improved, add_self_loops, dtype)
> 61 row, col = edge_index[0], edge_index[1]
> 62 deg = scatter_add(edge_weight, col, dim=0, dim_size=num_nodes)
> ---> 63 deg_inv_sqrt = deg.pow_(-0.5)
> 64 deg_inv_sqrt.masked_fill_(deg_inv_sqrt == float('inf'), 0)
> 65 return edge_index, deg_inv_sqrt[row] * edge_weight * deg_inv_sqrt[col]
>
> RuntimeError: result type Floatcan't be cast to the desired output type Long
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Pytorch: RuntimeError: result type Float can't be cast to the ...
I was getting the same error doing this: loss_fn(output, target). where the output was Tensor torch.float32 and target was Tensor ...
Read more >result type Float can't be cast to the desired output ... - GitHub
Today, it threw an error while running train.py as follows. Before this, I successfully completed the training process. I have not made any ......
Read more >result type Float can't be cast to the desired output type Long
Doing multi-label binary classification with BCEWithLogitsLoss, but I get a RunTimeError “RuntimeError: result type Float can't be cast to ...
Read more >一步真实解决RuntimeError: result type Float can't be cast to ...
在使用YOLO框架训练自己的数据集时候,你是不是出现了这个问题? RuntimeError: result type Float can't be cast to the desired output type long int.
Read more >Float can't be cast to the desired output type Int - fastai
Hello Everyone, I am fairly new to fastai and I apologize if this problem seems fairly rudimentary. I've been struggling with this for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can use the following
AtomEncoder
instead of the one provided by OGB:Worked! Thank you so much!