今天在跑pyg的gcn的例子的时候出现了下面的错误:
import torch_geometric.data
File "/Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_geometric/data/__init__.py", line 1, in <module>
from .data import Data
File "/Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_geometric/data/data.py", line 9, in <module>
from torch_sparse import SparseTensor
File "/Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_sparse/__init__.py", line 19, in <module>
torch.ops.load_library(spec.origin)
File "/Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch/_ops.py", line 110, in load_library
ctypes.CDLL(path)
File "/Users/../anaconda3/envs/yolo/lib/python3.8/ctypes/__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_sparse/_convert_cpu.so, 6): Symbol not found: __ZN2at8internal13_parallel_runExxxRKNSt3__18functionIFvxxmEEE
Referenced from: /Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_sparse/_convert_cpu.so
Expected in: /Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch/lib/libtorch_cpu.dylib
in /Users/../anaconda3/envs/yolo/lib/python3.8/site-packages/torch_sparse/_convert_cpu.so
我的gcn的例子的代码来源为:
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
class GCN(torch.nn.Module):
def __init__(self):
super().__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)
from torch_geometric.datasets import Planetoid
dataset = Planetoid(root='./Cora', name='Cora')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GCN().to(device)
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).argmax(dim=1)
correct = (pred[data.test_mask] == data.y[data.test_mask]).sum()
acc = int(correct) / int(data.test_mask.sum())
print(f'Accuracy: {acc:.4f}')
解决方法
我用conda换了一个新的环境,然后执行下面的命令:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pyg -c pyg
然后就可以运行了