当前位置: 代码网 > it编程>前端脚本>Python > PyTorch的安装与使用示例详解

PyTorch的安装与使用示例详解

2024年05月26日 Python 我要评论
技术背景pytorch是一个非常常用的ai框架,主要归功于其简单易用的特点,深受广大科研人员的喜爱。在前面的一篇中我们介绍过制作pytorch的singularity镜像的方法,这里我们单独抽出pyt

技术背景

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-h2181459_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-h2234567_1 
  libgomp            anaconda/pkgs/main/linux-64::libgomp-11.2.0-h2234567_1 
  libstdcxx-ng       anaconda/pkgs/main/linux-64::libstdcxx-ng-11.2.0-h2234567_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        |       h37cfd23_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=<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

参考链接

到此这篇关于pytorch的安装与使用的文章就介绍到这了,更多相关pytorch的安装与使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com