【Python · Pytorch】Conda介绍 & DGL-cuda安装

本文仅涉及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官网 Anaconda安装网址

进入安装网址,可找到Anaconda和Miniconda的安装包。

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

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

Anaconda Navigator:配合Anaconda使用的可视化软件。


找到Anaconda安装路径下的scripts文件夹,以管理员身份运行cmdPowerShell窗口,执行安装命令。

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 环境 配置库

创建名为DemoConda虚拟环境

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

下载 torch-2.4.1+cu121-cp39-cp39-win_amd64.whldgl-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相关代码。

相关推荐
云泽野8 分钟前
Pytest之parametrize参数化
android·python·pytest
盖盖衍上1 小时前
Java 8 新特性
java·windows·python
mingupup2 小时前
创建一个MCP服务器,并在Cline中使用,增强自定义功能。
python
千里码aicood2 小时前
[含文档+PPT+源码等]精品基于Python实现的vue3+Django计算机课程资源平台
开发语言·python·django
小白教程2 小时前
Python核心技术,Django学习基础入门教程(附环境安装包)
python·学习·django·django教程·django项目·django项目实战·django入门教程
老大白菜3 小时前
基于Flask实现的多语言Hello World
后端·python·flask
KingDol_MIni3 小时前
一个使用ALIGNN神经网络对材料性能预测的深度学习案例解读
人工智能·pytorch·深度学习
乔大将军3 小时前
项目准备(flask+pyhon+MachineLearning)- 3
后端·python·flask
一小路一3 小时前
通往 AI 之路:Python 机器学习入门-数据结构
数据结构·后端·python·学习·机器学习
我要昵称干什么3 小时前
在Pycharm中将ui文件修改为py文件
ide·python·学习·ui·pycharm·pyqt