图神经网络学习(1)- 安装部署指南
图神经网络的安装部署指南
图神经网络(Graph Neural Networks,GNNs)近年来在处理图结构方面 ,广泛应用于社交网络分析、分子结构预测、推荐系统等领域。本文将详细介绍如何在本地环境中安装和部署图神经网络的相关库。
环境准备
在开始安装之前,建议创建一个独立的 Python 环境,以避免依赖冲突。这里 使用 conda
来创建环境:
conda create --name 2025_py39_gnn python=3.9
conda activate 2025_py39_gnn
安装 PyTorch
图神经网络通常依赖于 PyTorch 框架,因此首先需要安装 PyTorch。为了加速安装过程, 使用清华大学的 PyPI 镜像,最新的是 PyTorch 2.5.1 版本
pip install torch torchvision -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
安装图神经网络相关库
安装 PyTorch Geometric 依赖库
PyTorch Geometric 是一个专门用于图神经网络的库,依赖于一些额外的扩展库,如 torch-scatter、torch-sparse 等。这些库提供了高效的图操作和并行计算支持。安装这些依赖库时,需要指定与 PyTorch 版本和 CUDA 版本相匹配的 wheel 文件。这里以 PyTorch 2.5.1 为例:
pip install -q torch-scatter -f https://data.pyg.org/whl/torch-2.5.1+cpu.html -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
pip install -q torch-sparse -f https://data.pyg.org/whl/torch-2.5.1+cpu.html -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
pip install -q pyg_lib -f https://data.pyg.org/whl/torch-2.5.1+cpu.html -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
pip install -q torch_cluster -f https://data.pyg.org/whl/torch-2.5.1+cpu.html -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/
pip install -q git+https://github.com/pyg-team/pytorch_geometric.git
安装 PyTorch Geometric
接下来安装 PyTorch Geometric 本身。可以直接从 GitHub 上获取最新版本:
pip install -q git+https://github.com/pyg-team/pytorch_geometric.git
验证安装
安装完成后,可以通过以下代码验证 PyTorch Geometric 是否安装成功:
import torch_geometric
print(torch_geometric.__version__)
如果能够正常输出版本号,说明安装成功。
通过上述步骤,你已经成功安装了图神经网络所需的环境和库。PyTorch Geometric 提供了丰富的图神经网络模型和工具,可以帮助你快速构建和训练自己的图神经网络模型。
希望这篇博客对你有帮助!