torch_geometric踩坑实战--安装与运行 亲测有效!!

文章讲述了在安装和使用torch_geometric库时遇到的问题,包括依赖安装不全、版本不匹配导致的错误。作者通过卸载重装、手动下载特定版本的依赖库,并调整torch_geometric版本,最终成功运行代码。文中提供的解决步骤对遇到类似问题的开发者有参考价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

torch_geometric是PyG中必不可少的一个包,也是进行图神经网络学习的必备,然而安装这个包并运行一段简单的代码踩了不少坑,记录一下。

1、安装torch_geometric

一开始,我直接pip

pip intsall torch_geometric

果然报错,提示没有torch_sparse

很显然是没有安装依赖,于是我去查需要哪些依赖

官网地址:Installation — pytorch_geometric documentation (pytorch-geometric.readthedocs.io)

按照官网给出的代码进行安装

pip install pyg_lib torch_scatter torch_sparse torch_cluster torch_spline_conv -f https://data.pyg.org/whl/torch-1.13.0+cpu.html

运行依旧没有成功...

于是手动下载

下载地址:https://data.pyg.org/whl/

另一个下载地址:pytorch-geometric.com/whl/torch-1.4.0.html

(1)卸载安装的相关包

pip uninstall torch-geometric torch-scatter torch-sparse torch-cluster torch-spline-conv

(2)选择适合自己的torch和cuda版本

 (3)按照自己的python版本和操作系统下载

(4)进入conda激活虚拟环境,然后进入下载地址

这里注意需要进入D盘,cmd中进入D盘的命令为:

D:

 然后cd进入包的下载页面,直接pip install

安装完所有的依赖后记得 

pip install torch_geometric

 2、运行代码

import torch_geometric

 依旧报错:

Pytorch AttributeError: module 'torch' has no attribute 'sparse_scs'

这个就很让人费解,于是查看了一下

 发现torch.sparse里面确实没有这个模块,那为什么会报错呢?查看一些帖子后发现报这种错一般是因为版本不对,于是,降低torch_geometric版本,果然成功!!

我的版本:

 至此就安装成功啦!!

简单跑了个程序

import torch.nn.functional as F
class GCN(torch.nn.Module):
    def __init__(self, num_node_features, num_classes):
        super(GCN, self).__init__()
        self.conv1 = GCNConv(num_node_features, 16)
        self.conv2 = GCNConv(16, 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)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)
        x = F.softmax(x, dim=1)

        return x
model = GCN(dataset.num_node_features, dataset.num_classes)
print(model)

def train(model, data):
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-4)
    loss_function = torch.nn.CrossEntropyLoss()
    model.train()
    for epoch in range(200):
        out = model(data)
        optimizer.zero_grad()
        loss = loss_function(out[data.train_mask], data.y[data.train_mask])
        loss.backward()
        optimizer.step()

        print('Epoch {:03d} loss {:.4f}'.format(epoch, loss.item()))

train(model,data)

运行结果:

没问题!!接下来就可以建立一个自己的图神经网络啦~ 

<think>我们根据引用信息,特别是引用[4]中的命令,来安装PyTorch 1.8.1+cu111兼容的torch_geometric及其依赖。 步骤: 1. 确保已经安装PyTorch 1.8.1+cu111。如果没有,可以使用引用[2]中的命令安装: pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html 2. 安装依赖库:torch-scatter, torch-sparse, torch-cluster, torch-geometric。注意,引用[4]中提供了针对torch-1.8.1+cu111的安装命令,但版本号可能会随时间变化。我们使用引用[4]中指定的版本: pip install torch-scatter==2.0.6 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html pip install torch-sparse==0.6.9 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html pip install torch-cluster==1.5.9 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html pip install torch-geometric==1.6.3 注意:上述命令中的URL是PyG官方提供的预编译包,针对特定PyTorch和CUDA版本。 3. 验证安装:可以尝试导入torch_geometric,检查是否有错误。 但是,请注意引用[3]中提到,Python3.9~Python3.13可能无法直接安装,需要先安装四个包。因此,请确保你的Python版本在3.7或3.8(官方推荐),或者按照引用[3]的方法处理更高版本。 如果遇到问题,可以查看引用[3]中的详细步骤。 另外,引用[1]提到,官网提供了安装教程,但如果你在安装过程中遇到问题,可能需要检查CUDA版本和PyTorch版本的匹配性。 总结步骤: - 安装PyTorch 1.8.1+cu111 - 按顺序安装四个依赖库(scatter, sparse, cluster, geometric),并指定版本和下载链接。 注意:由于网络原因,可能需要多次尝试或使用国内镜像源。 如果上述版本无法安装,可以尝试访问PyG官网(引用[3])查看最新适配的版本。 因此,具体命令如下:</think>根据引用信息,安装PyTorch 1.8.1+cu111兼容的torch_geometric版本需按顺序执行以下命令: ### 安装步骤 1. **安装PyTorch基础环境**(若未安装): ```bash pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html ``` [^2] 2. **安装依赖库**(注意版本匹配): ```bash pip install torch-scatter==2.0.6 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html pip install torch-sparse==0.6.9 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html pip install torch-cluster==1.5.9 -f https://data.pyg.org/whl/torch-1.8.1+cu111.html ``` [^4] 3. **安装torch_geometric**: ```bash pip install torch-geometric==1.6.3 ``` [^4] ### 关键说明 1. **版本严格匹配**: - PyTorch必须为`1.8.1+cu111` - 依赖库版本需精确对应(如`torch-scatter==2.0.6`) - 使用`-f`参数指定预编译包地址确保兼容性 2. **Python版本限制**: - 若使用Python≥3.9,需从源码编译依赖库(引用[3]) - 推荐Python 3.8环境避免兼容问题 3. **验证安装**: ```python import torch from torch_geometric import __version__ print(torch.__version__) # 应输出1.8.1+cu111 print(__version__) # 应输出1.6.3 ``` ### 常见问题解决 - **安装超时/失败**:添加`--default-timeout=1000`延长超时时间 - **版本冲突**:先卸载旧版`pip uninstall torch-scatter torch-sparse torch-cluster torch-geometric` - **CUDA不匹配**:确认`nvcc --version`输出CUDA 11.1 > 提示:完整安装指南可参考[PyG官方文档](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html)[^3]
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值