PyTorch的安装与使用

news/2024/10/4 11:27:54

技术背景

PyTorch是一个非常常用的AI框架,主要归功于其简单易用的特点,深受广大科研人员的喜爱。在前面的一篇文章中我们介绍过制作PyTorch的Singularity镜像的方法,这里我们单独抽出PyTorch的安装和使用,再简单的聊一聊。

安装Torch

常规的安装方案可以使用源码安装、pip安装、conda安装和容器安装等,这里我们首选推荐的是conda安装的方法。关于conda,其实没必要安装完整版本的anaconda,装一个miniconda就可以了。假定我们已经安装好了conda,那么首先要创建一个专用的pytorch虚拟环境:

$ conda create -n pytorch python=3.9
Retrieving notices: ...working... done
Collecting package metadata (current_repodata.json): done
Solving environment: done==> WARNING: A newer version of conda exists. <==current version: 23.1.0latest version: 24.4.0Please update conda by running$ conda update -n base -c defaults condaOr to minimize the number of packages updated during conda update useconda install conda=24.4.0## Package Plan ##environment location: /home/dechin/anaconda3/envs/pytorchadded / updated specs:- python=3.9The following packages will be downloaded:package                    |            build---------------------------|-----------------ca-certificates-2024.3.11  |       h06a4308_0         127 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainlibffi-3.4.4               |       h6a678d5_1         141 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainopenssl-3.0.13             |       h7f8727e_1         5.2 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainpip-23.3.1                 |   py39h06a4308_0         2.6 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainpython-3.9.19              |       h955ad1f_1        25.1 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainsetuptools-69.5.1          |   py39h06a4308_0        1003 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainsqlite-3.45.3              |       h5eee18b_0         1.2 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/maintk-8.6.14                  |       h39e8969_0         3.4 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/maintzdata-2024a               |       h04d1e81_0         116 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainwheel-0.43.0               |   py39h06a4308_0         109 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainxz-5.4.6                   |       h5eee18b_1         643 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainzlib-1.2.13                |       h5eee18b_1         111 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main------------------------------------------------------------Total:        39.8 MBThe following NEW packages will be INSTALLED:_libgcc_mutex      anaconda/pkgs/main/linux-64::_libgcc_mutex-0.1-main _openmp_mutex      anaconda/pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu ca-certificates    anaconda/pkgs/main/linux-64::ca-certificates-2024.3.11-h06a4308_0 ld_impl_linux-64   anaconda/pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1 libffi             anaconda/pkgs/main/linux-64::libffi-3.4.4-h6a678d5_1 libgcc-ng          anaconda/pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1 libgomp            anaconda/pkgs/main/linux-64::libgomp-11.2.0-h1234567_1 libstdcxx-ng       anaconda/pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1 ncurses            anaconda/pkgs/main/linux-64::ncurses-6.4-h6a678d5_0 openssl            anaconda/pkgs/main/linux-64::openssl-3.0.13-h7f8727e_1 pip                anaconda/pkgs/main/linux-64::pip-23.3.1-py39h06a4308_0 python             anaconda/pkgs/main/linux-64::python-3.9.19-h955ad1f_1 readline           anaconda/pkgs/main/linux-64::readline-8.2-h5eee18b_0 setuptools         anaconda/pkgs/main/linux-64::setuptools-69.5.1-py39h06a4308_0 sqlite             anaconda/pkgs/main/linux-64::sqlite-3.45.3-h5eee18b_0 tk                 anaconda/pkgs/main/linux-64::tk-8.6.14-h39e8969_0 tzdata             anaconda/pkgs/main/noarch::tzdata-2024a-h04d1e81_0 wheel              anaconda/pkgs/main/linux-64::wheel-0.43.0-py39h06a4308_0 xz                 anaconda/pkgs/main/linux-64::xz-5.4.6-h5eee18b_1 zlib               anaconda/pkgs/main/linux-64::zlib-1.2.13-h5eee18b_1 Proceed ([y]/n)? yDownloading and Extracting PackagesPreparing transaction: done                                                                                                                      
Verifying transaction: done                                                                                                                      
Executing transaction: done                                                                                                                      
#                                                                                                                                                
# To activate this environment, use                                                                                                              
#                                                                                                                                                
#     $ conda activate pytorch                                                                                                                   
#                                                                                                                                                
# To deactivate an active environment, use                                                                                                       
#                                                                                                                                                
#     $ conda deactivate                                                                                                                         

这里我们是基于Python3.9版本创建了一个Python虚拟环境。相比于容器和虚拟机来说,虚拟环境结构更加简单,非常适用于本地的Python软件管理。当然,如果是在服务器上面运行,那还是推荐容器的方案多一些。有了基础的Python环境之后,可以去PyTorch官网找找适用于自己本地环境的conda安装命令:

然后把这条命令复制到自己本地进行安装。建议在安装的时候加上-y的配置,就省的加载一半还需要你自己手动去配置一个输入一个y来决定是否继续下一步安装。因为这个安装的过程可能也会比较耗时,尤其网络对于一部分国内的IP可能并不是那么的友好。

$ conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia                                                                            
Solving environment: done                                                                                           ## Package Plan ##environment location: /home/dechin/anaconda3/envs/pytorchadded / updated specs:- pytorch- pytorch-cuda=11.8- torchaudio- torchvisionThe following packages will be downloaded:package                    |            build---------------------------|-----------------charset-normalizer-2.0.4   |     pyhd3eb1b0_0          35 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainjinja2-3.1.3               |   py39h06a4308_0         269 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainlibdeflate-1.17            |       h5eee18b_1          64 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/mainlibnpp-11.8.0.86           |                0       147.8 MB  nvidialibunistring-0.9.10        |       h27cfd23_0         536 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/maintyping_extensions-4.9.0    |   py39h06a4308_1          54 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main------------------------------------------------------------Total:       148.8 MBThe following NEW packages will be INSTALLED:blas               anaconda/pkgs/main/linux-64::blas-1.0-mkl bzip2              anaconda/pkgs/main/linux-64::bzip2-1.0.8-h5eee18b_6 certifi            anaconda/pkgs/main/linux-64::certifi-2024.2.2-py39h06a4308_0 charset-normalizer anaconda/pkgs/main/noarch::charset-normalizer-2.0.4-pyhd3eb1b0_0 cuda-cudart        nvidia/linux-64::cuda-cudart-11.8.89-0 cuda-cupti         nvidia/linux-64::cuda-cupti-11.8.87-0 ...pytorch            pytorch/linux-64::pytorch-2.3.0-py3.9_cuda11.8_cudnn8.7.0_0 pytorch-cuda       pytorch/linux-64::pytorch-cuda-11.8-h7e8668a_5 pytorch-mutex      pytorch/noarch::pytorch-mutex-1.0-cuda zstd               anaconda/pkgs/main/linux-64::zstd-1.5.5-hc292b87_2 Downloading and Extracting PackagesPreparing transaction: done                                                                                         
Verifying transaction: done                                                                                         
Executing transaction: done                         

安装完成后可以通过如下指令,在bash命令行里面检查一下是否安装成功了PyTorch的CUDA版本:

$ python3 -c "import torch;print(torch.cuda.is_available())"
True

如果输出为True则表明安装成功。另外顺便一提,如果在conda安装的过程中出现如下的报错:

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://conda.anaconda.org/nvidia/linux-64/libnpp-11.8.0.86-0.tar.bz2>                                                                                                              
Elapsed: -                                                                                                          An HTTP error occurred when trying to retrieve this URL.                                                            
HTTP errors are often intermittent, and a simple retry will get you on your way.                                    CancelledError()                                                                                                    
CancelledError()                                                                                                    
CancelledError()                                                                                                    
CancelledError()

一般情况下就是由网络问题导致的,但也并不是完全无法链接,我们同样的命令行多输入几次就可以了,直到安装完成为止。

PyTorch自动微分

关于自动微分的原理,读者可以参考一下之前的这篇手搓自动微分的文章,PyTorch大概就是使用的这个自动微分的原理。在PyTorch框架下,我们可以通过backward函数来自定义反向传播函数,这一点跟MindSpore框架有所不同,MindSpore框架下自定义反向传播函数使用的是bprop函数,MindSpore自定义反向传播相关内容可以参考下这篇文章。如下所示是一个Torch的用例:

# 忽略告警信息
import warnings
warnings.filterwarnings("ignore")import torch# 自定义可微分的类型
class Gradient(torch.autograd.Function):# 前向传播@staticmethoddef forward(ctx, x, w=None):# 保存一个参数到计算图中ctx.save_for_backward(w)return x# 反向传播@staticmethoddef backward(ctx, g):w,  = ctx.saved_tensorsif w is None:return gelse:return g * w, None# 非加权自动微分测试
x = torch.autograd.Variable(torch.tensor(3.14), requires_grad=True)
g = torch.autograd.Variable(torch.tensor(3.15))
gradient = Gradient()
# 前向传播
y = gradient.apply(x)
print (y)
# 反向传播
y.backward(g)
# 打印梯度
print (x.grad)
# 加权自动微分测试
x = torch.autograd.Variable(torch.tensor(3.14), requires_grad=True)
g = torch.autograd.Variable(torch.tensor(3.15))
w = torch.autograd.Variable(torch.tensor(2.0))
z = gradient.apply(x, w)
print (z)
z.backward(g)
print (x.grad)

输出结果为:

tensor(3.1400, grad_fn=<GradientBackward>)
tensor(3.1500)
tensor(3.1400, grad_fn=<GradientBackward>)
tensor(6.3000)

这样一来,就把需要输入到反向传播函数中的加权值传了进去。因为在正常的backward函数中,相关的输入类型都是规定好的,不能随便加输入,所以要从前向传播中传递给计算图。在这个案例中,顺便介绍下PyTorch开源仓库中的两个Issue。第一个问题是,PyTorch的前向传播函数中,如果从外部传入一个关键字参数,会报错

关于这个问题,官方做了如下解释:

大体意思就是,如果使用关键字类型的参数输入,会给参数校验和结果返回带来一些困难。同时给出了一个临时的解决方案:

其实也就是我们这个案例中所采用的方案,套一个条件语句就可以了。另外一条Issue是,如果涉及到多个输入,那么在反向传播函数中也要给到多个输出:

不过在这个Issue中,提Issue的人本身也给出了一个方案,就是直接在返回结果中给一个None值。

总结概要

本文介绍了热门AI框架PyTorch的conda安装方案,与简单的自动微分示例。并顺带讲解了一下PyTorch开源Github仓库中的两个Issue内容,分别是自动微分的关键词参数输入问题与自动微分参数数量不匹配时的参数返回问题,并包含了这两个Issue的解决方案。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/torch.html

作者ID:DechinPhy

更多原著文章:https://www.cnblogs.com/dechinphy/

请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

参考链接

  1. https://pytorch.org/get-started/locally/
  2. https://www.cnblogs.com/dechinphy/p/pytorch.html
  3. https://github.com/pytorch/pytorch/issues/16940
  4. https://github.com/Lightning-AI/pytorch-lightning/issues/6624
  5. https://blog.csdn.net/winycg/article/details/104410525

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/28965.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

DirectAU论文阅读笔记

Towards Representation Alignment and Uniformity in Collaborative Filtering论文阅读笔记 Abstract 现存的问题: ​ 现有的研究主要集中在设计更强大的编码器(如图神经网络)来学习更好的表示。然而,很少有人努力致力于研究CF中表示的期望属性,这对于理解现有CF方法的基…

rabbitmq开启ssl

官网:https://www.rabbitmq.com/docs/management#multiple-listeners生成证书openssl req -newkey rsa:2048 -nodes -keyout rsa_private.key -x509 -days 365 -out cert.crt //一次性生成私钥和证书2.使用 已有RSA 私钥生成自签名证书openssl req -new -x509 -days 365 -ke…

ITIL4视角下的IT监控与故障管理:守护服务健康的双刃剑

引言:监控的曙光 在IT服务管理的浩瀚星图中,"监控"这一璀璨星辰终于得到了应有的重视与聚焦。ITIL4的出台,不仅明确将监控告警纳入事件管理的广阔宇宙,而且强调了其在预防故障、保障服务连续性中的核心地位。当组织拥抱ITIL 4的管理智慧时,多套监控系统的部署成…

Python基础之基本运算符

【一】参考网站菜鸟教程:https://www.runoob.com/python/python-operators.html【二】算数运算符python支持的算数运算符与数学的运算符号是一样的# 【1】加法运算符 a = 3 b = 4 c = a + b print(c) # 输出 7# 【2】减法运算符 a = 3 b = 4 c = a - b print(c) # 输出 -1…

struct:Python二进制数据结构

在C/C++语言中,struct被称为结构体。而在Python中,struct是一个专门的库,用于处理字节串与原生Python数据结构类型之间的转换。 本篇,将详细介绍二进制数据结构struct的使用方式。 函数与Struct类 struct库包含了一组处理结构值得模块级函数,以及一个Struct类。格式指示符…

NumPy:Python科学计算基础包

NumPy 是 Python 科学计算的基础包,几乎所有用 Python 工作的科学家都利用了的强大功能。此外,它也广泛应用在开源的项目中,如:Pandas、Seaborn、Matplotlib、scikit-learn等。Numpy全称Numerical Python。它提供了2种基本的对象:ndarray与ufunc。 ndarray是存储单一数据的…

Python 如何优雅的操作 PyMySQL

一、PyMysql 在使用Python操作MySQL数据过的过程中,基本的增删改查操作如何更加高效优雅的执行。这里将以PyMySQL为例,介绍一下如何使用Python操作数据库。 Python对MySQL数据库进行操作,基本思路是先连接数据库 Connection 对象,建立游标 Cursor 对象,然后执行SQL语句对数…

7个鲜为人知的 Python 好库!

https://mp.weixin.qq.com/s/eY1QXpwbTNSOd08Wfpg4sQ①-PivotTableJS 无需任何代码就可以在Jupyter Notebook中交互式地分析数据。 https://pypi.org/project/pivottablejs/ ②-PyTube 只需5行代码就可以下载油管视频。可能这个是国内用户最用不到的Python库了。https://pytube…