首页 > 教程 > PyTorch的安装与使用

PyTorch的安装与使用

时间:2024-05-08 | 来源: | 阅读:167

话题: T C

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

技术背景

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.0
  latest version: 24.4.0

Please update conda by running

    $ conda update -n base -c defaults conda

Or to minimize the number of packages updated during conda update use

     conda install conda=24.4.0



## Package Plan ##

  environment location: /home/dechin/anaconda3/envs/pytorch

  added / updated specs:
    - python=3.9


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2024.3.11  |       h06a4308_0         127 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libffi-3.4.4               |       h6a678d5_1         141 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    openssl-3.0.13             |       h7f8727e_1         5.2 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    pip-23.3.1                 |   py39h06a4308_0         2.6 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    python-3.9.19              |       h955ad1f_1        25.1 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    setuptools-69.5.1          |   py39h06a4308_0        1003 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    sqlite-3.45.3              |       h5eee18b_0         1.2 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    tk-8.6.14                  |       h39e8969_0         3.4 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    tzdata-2024a               |       h04d1e81_0         116 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    wheel-0.43.0               |   py39h06a4308_0         109 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    xz-5.4.6                   |       h5eee18b_1         643 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    zlib-1.2.13                |       h5eee18b_1         111 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    ------------------------------------------------------------
                                           Total:        39.8 MB

The 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)? y


Downloading and Extracting Packages
                                                                                                                                                 
Preparing 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/pytorch

  added / updated specs:
    - pytorch
    - pytorch-cuda=11.8
    - torchaudio
    - torchvision


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    charset-normalizer-2.0.4   |     pyhd3eb1b0_0          35 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    jinja2-3.1.3               |   py39h06a4308_0         269 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libdeflate-1.17            |       h5eee18b_1          64 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    libnpp-11.8.0.86           |                0       147.8 MB  nvidia
    libunistring-0.9.10        |       h27cfd23_0         536 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    typing_extensions-4.9.0    |   py39h06a4308_1          54 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    ------------------------------------------------------------
                                           Total:       148.8 MB

The 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 Packages
                                                                                                                    
Preparing 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):
    # 前向传播
    @staticmethod
    def forward(ctx, x, w=None):
        # 保存一个参数到计算图中
        ctx.save_for_backward(w)
        return x
    # 反向传播
    @staticmethod
    def backward(ctx, g):
        w,  = ctx.saved_tensors
        if w is None:
            return g
        else:
            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=)
tensor(3.1500)
tensor(3.1400, grad_fn=)
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
推荐

最新好玩手游

更多

手游风云榜

更多

资讯阅读

更多


湘ICP备2022002427号-10 湘公网安备:43070202000427号
© 2013~2024 haote.com 好特网