本文仅涉及DGL库介绍与cuda配置,不包含神经网络及其训练测试。
起因:博主电脑安装了 CUDA 12.4 版本,但DGL疑似没有版本支持该CUDA版本。随即想到可利用Conda创建CUDA12.1版本的虚拟环境。

1. Conda环境
1.1 Conda环境简介
Conda :一个开源的 软件包管理系统 和 环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换,这有助于进行不同的数据科学实验。
1.1.1 Conda关系
Conda 各版本间关系如下图所示:

- Anaconda = Miniconda + 150高质量库
- Miniconda = Python (多版本) + 基础库
1.1.2 Anaconda安装
进入安装网址,可找到Anaconda和Miniconda的安装包。


下载后完成安装步骤即可。




安装完成后,由此进入Anaconda终端。

1.1.3 Navigator安装
Anaconda Navigator:配合Anaconda使用的可视化软件。
找到Anaconda安装路径下的scripts
文件夹,以管理员身份运行cmd
或PowerShell
窗口,执行安装命令。
cmd
conda install -c anaconda anaconda-navigator
这里在cmd
中调用了这个脚本。

执行安装命令,中途需要输入y
确认安装。



安装完成后,可在开始菜单找到Anaconda Navigator
,证明安装成功。

打开即可使用 Anaconda 的可视化界面。

1.2 常用命令
1.2.1 Conda相关
查询conda版本
cmd
conda --version
更新conda版本
cmd
conda update conda
查看conda环境详细信息
cmd
conda info
1.2.2 环境管理
查看环境列表
cmd
conda env list
cmd
conda info --envs
创建环境
在需要特指环境的名称时
-n
等价于--name
最新Python版本
cmd
conda create --name env_name
指定Python版本
cmd
conda create --name env_name python=3.9.0
激活环境
cmd
conda activate env_name
退出环境
cmd
conda deactivate
删除环境
cmd
conda remove --name env_name --all
复制环境
cmd
conda create --name env_name --clone old_env_name
分享/备份一个虚拟环境
可通过导出 .yml 文件,快速向他人分享实验环境。
导出虚拟环境
cmd
conda env export > environment.yml
导入虚拟环境(利用cd
命令切换至路径 / 更换为绝对路径)
cmd
conda env create -f environment.yml
1.2.3 包管理
镜像源配置
配置清华镜像源
cmd
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
查看镜像源
cmd
conda config --show channels
安装库
安装最新版本
cmd
conda install [package]
安装指定版本
cmd
conda install [package]=[version]
删除库
当前环境
cmd
conda remove [package]
升级库
当前环境某库
cmd
conda update [package]
当前环境所有库
cmd
conda update --all
注:Conda包管理与Pip包管理相互独立,彼此不能删除对方安装的库,一般不在Conda与Pip中安装相同的库。
2. DGL库 & cuda版本
2.1 DGL库 简介

DGL,全称为Deep Graph Library(深度图库),是一个用于处理图结构数据的Python库。
它提供了用于图神经网络(Graph Neural Networks,GNNs)和图分析的工具和接口。
DGL的主要目标是简化图深度学习的开发,并使其更容易应用于各种任务,包括节点分类、链接预测、图分类、图生成等。
其相较于原实现内存开销更小、训练速度更快,可结合NetworkX实现图数据的可视化。
官方网站:DGL官网
Why DGL?
为什么选择DGL?
In the last few years, deep learning has enjoyed plenty of extraordinary successes. Many challenging tasks have been solved or close to being solved by Deep Learning, such as image recognition, rich-resource machine translation, game playing. These were made possible by a set of techniques that are composed of a number of representationally powerful building-blocks, such as convolution, attention and recurrence, applied to images, video, text, speech and beyond.
在过去的几年里,深度学习取得了许多非凡的成功。深度学习已经解决或接近解决了许多具有挑战性的任务,如图像识别、资源丰富的机器翻译、游戏等。这些都是通过一系列技术实现的,这些技术由许多具有代表性的强大构建模块组成,例如卷积、注意力和递归,应用于图像、视频、文本、语音等领域。
The development and deployment of these techniques often depend on the simple correlation of the given data; for example, CNN is based on the spatial correlation between nearby pixels while RNN family dwells on the assumption that its input is sequence-like.
这些技术的发展和部署往往取决于给定数据的简单相关性;例如,CNN基于附近像素之间的空间相关性,而RNN家族则假设其输入是序列式的。
More recently, there has been a steady flow of new deep learning research focusing on graph-structured data. Some of them are more conventional graph related problems, like social networks, chemical molecules and recommender systems, where how the entity interacts with its neighborhood is as informative as, if not more than, the features of the entity itself.
最近,不断有新的深度学习研究聚焦于图结构数据。其中一些是更传统的图形相关问题,如社交网络、化学分子和推荐系统,在这些问题中,实体与其邻居的互动方式与实体本身的特征一样有信息,如果不是更多的话。
Some others nevertheless have applied graph neural networks to images, text or games. Very broadly speaking, any of the data structures we have covered so far can be formalized to graphs. For instance an image can be seen as grid of pixel, text a sequence of words... Together with matured recognition modules, graph can also be defined at higher abstraction level for these data: scene graphs of images or dependency trees of language.
然而,还有一些人将图形神经网络应用于图像、文本或游戏。从广义上讲,到目前为止我们所讨论的任何数据结构都可以形式化为图。例如,图像可以看作是像素的网格,文本可以看作是单词的序列,再加上成熟的识别模块,图也可以在更高的抽象层次上定义:图像的场景图或语言的依赖树。
To this end, we made DGL. We are keen to bringing graphs closer to deep learning researchers. We want to make it easy to implement graph neural networks model family. We also want to make the combination of graph based modules and tensor based modules (PyTorch or MXNet) as smooth as possible.
为此,我们制作了DGL。我们热衷于让图表更接近深度学习研究人员。我们希望使图神经网络模型族的实现变得容易。我们还希望使基于图的模块和基于张量的模块(PyTorch或MXNet)的组合尽可能平滑。
------以上内容摘自 DGL官网
2.2 DGL库 cuda版本
本文介绍安装DGL-cuda库
,配置cuda环境相关内容 可参考博客:配置cuda环境 & cuDNN库
由于Pytorch已经弃用利用conda的安装方式,所以不推荐使用conda安装。
(注:如果要使用最新的cuda版本,Python至少需要3.9版本及以上)

DGL库的pip
命令安装方式:版本对应关系

操作系统、Python、PyTorch、Apache MXNet、TensorFlow所需版本 如下图所示。

确认版本后,进入下载列表,找到所需的版本下载即可。
这里可下载cu121
最新版本的DGL库,还需要下载对应版本的Pytroch库。
下载链接:DGL全列表 PyTorch全列表

2.3 Conda 环境 配置库
创建名为Demo
的Conda
虚拟环境


激活(进入) 环境,前侧括号内表示虚拟环境所处路径,后者为cmd
或PowerShell
当前路径。

下载 torch-2.4.1+cu121-cp39-cp39-win_amd64.whl
与 dgl-2.0.0+cu121-cp39-cp39-win_amd64.whl
文件,并切换至所处路径。

利用pip
命令安装其他所需库,以下是常见pip镜像源和常用库。
常见pip镜像源
python
# 清华镜像
https://pypi.tuna.tsinghua.edu.cn/simple
# 中国科学技术大学
https://pypi.mirrors.ustc.edu.cn/simple
# 豆瓣
http://pypi.douban.com/simple/
# 阿里云
http://mirrors.aliyun.com/pypi/simple/
pip换源方式
python
# 换清华镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 换回默认源
pip config unset global.index-url
数据+数值库
cmd
pip install numpy
pip install scipy
pip install openpyxl
sk-learn库
python
pip install scikit-learn
JupyterLab库
python
pip install juyterlab
绘图相关库
python
pip install matplotlib
pip install seaborn
pip install networkx
PyQt6窗口库
python
pip install PyQt6
pip install pyqt6-tools
Pytorch库(CUDA版本)
python
pip install "torch-2.4.1+cu121-cp39-cp39-win_amd64.whl"
DGL库(CUDA版本)
python
pip install "dgl-2.0.0+cu121-cp39-cp39-win_amd64.whl"
Torchvision(CUDA版本)
cmd
pip install "torchvision-0.20.1+cu121-cp39-cp39-win_amd64.whl"


安装成功后,便可执行DGL相关代码。