这篇文章是从上一篇文章开始的(我将在下面的代码中使用相同的示例)。我已阅读有关这些错误的其他帖子,但我认为这些解决方法对我没有帮助。
目标:实现一个节点分类任务(类似于Cora,但应用不同)
问题:如何解决我遇到的这些错误?
- 'IndexError:用作索引的张量必须是长、字节或布尔张量'(在我的主代码中得到这个)
- 'RuntimeError: expected scalar type Long but found Float'(当我试图以公共友好的形式重现代码时,在我的代码中得到这个,以及上面的错误)
上下文:这是下面的代码:(在 Google Colab 中使用)
对于下面的示例,我将尝试对节点的类别进行分类,每个节点将有一个长度为 5 的特征向量(现在刚刚完全弥补)。我将使用邻接矩阵:
A = [(0, 1, 0, 1, 1), (1, 0, 1, 1, 0), (0, 1, 0, 0, 1), (1, 1, 0, 0, 0) , (1, 0, 1, 0, 0)]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline
import torch
from torch.nn import Linear
import torch.nn.functional as F
torch.__version__
import sklearn
from sklearn import preprocessing
# install pytorch geometric
!pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
from torch_geometric.nn import GCNConv
from torch_geometric.utils.convert import to_networkx, from_networkx
# Make the networkx graph
G = nx.Graph()
# Add some cars (just do 4 for now)
G.add_nodes_from([
(1, {'y': 0}),
(2, {'y': 1}),
(3, {'y': 2}),
(4, {'y': 3}),
(5, {'y': 4}),
])
# Add some edges --> A = [(0, 1, 0, 1, 1), (1, 0, 1, 1, 0), (0, 1, 0, 0, 1), (1, 1, 0, 0, 0), (1, 0, 1, 0, 0)]
G.add_edges_from([
(1, 2), (1, 4), (1, 5),
(2, 3), (2, 4),
(3, 2), (3, 5),
(4, 1), (4, 2),
(5, 1), (5, 3)
])
# add the price data to the graph as node features
for node in G.nodes():
G.nodes[node]['x'] = np.random.rand(5) * 5
# Convert the graph into PyTorch geometric
pyg_graph = from_networkx(G)
# Create the train_mask and test_mask
# Split the data
train_ratio = 0.2
num_nodes = data.x.shape[0]
num_train = int(num_nodes * train_ratio)
print(f'The number of training nodes is: {num_train}\n')
idx = [i for i in range(num_nodes)]
np.random.shuffle(idx)
train_mask = torch.full_like(data.y, False, dtype=bool)
train_mask[idx[:num_train]] = True
test_mask = torch.full_like(data.y, False, dtype=bool)
test_mask[idx[num_train:]] = True
我认为上面的一切都是正确的(我很幸运在后面部分得到了一些帮助)。概括地说,上面的代码:
一个。在中创建图形 (G)networkx
湾。分配品牌(即类别)和特征向量
C。将图形转换为 PyTorch 几何图形
d。为 PyTorch 几何定义 train_mask 和 test_mask
下面的大部分代码来自 Pytorch Geometric Cora 教程代码,只有一点点改变了一两次(例如变量的名称)。但是,这会导致错误,因此急于了解导致错误的原因。
# change the name of the variable to match the name in the online Cora tutorial
data = pyg_graph
from torch_geometric.nn import GCNConv
class GCN(torch.nn.Module):
def __init__(self, hidden_channels):
super().__init__()
torch.manual_seed(1234567)
self.conv1 = GCNConv(len(data.x[0]), hidden_channels) # in this case, we set num_features = len(data.x[0])
self.conv2 = GCNConv(hidden_channels, num_nodes) # in this case, num_classes = num_nodes
def forward(self, x, edge_index):
x = self.conv1(x, edge_index)
x = x.relu()
x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
model = GCN(hidden_channels=16)
print(model)
这是我现在尝试更改变量类型以防止错误的地方。
data.x = data.x.type(LongTensor)
data.y = data.y.type(LongTensor)
现在代码继续:
data.train_mask = train_mask
data.test_mask = test_mask
model = GCN(hidden_channels=16)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
criterion = torch.nn.CrossEntropyLoss()
def train():
model.train()
optimizer.zero_grad() # Clear gradients.
out = model(data.x, data.edge_index) # Perform a single forward pass.
loss = criterion(out[data.train_mask], data.y[data.train_mask]) # Compute the loss solely based on the training nodes.
loss.backward() # Derive gradients.
optimizer.step() # Update parameters based on gradients.
return loss
def test():
model.eval()
out = model(data.x, data.edge_index)
pred = out.argmax(dim=1) # Use the class with highest probability.
test_correct = pred[data.test_mask] == data.y[data.test_mask] # Check against ground-truth labels.
test_acc = int(test_correct.sum()) / int(data.test_mask.sum()) # Derive ratio of correct predictions.
return test_acc
for epoch in range(1, 101):
loss = train()
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}')\
data.x = data.x.type(LongTensor)
如/etc行所示。我已经尝试过这些解决方案,但似乎没有任何效果。此外,由于某种原因,在我的示例笔记本中执行该行时(此帖子的版本),我收到一个错误:NameError: name 'LongTensor' is not defined' 但我将把它放在另一篇文章中好像不能导入torch_C
。
任何帮助将不胜感激,并希望每个人都有一个愉快的假期。