- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
PyTorch是一个非常常用的AI框架,主要归功于其简单易用的特点,深受广大科研人员的喜爱。在前面的一篇文章中我们介绍过制作PyTorch的Singularity镜像的方法,这里我们单独抽出PyTorch的安装和使用,再简单的聊一聊.
常规的安装方案可以使用源码安装、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框架下,我们可以通过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的安装与使用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 3 年前。 此帖子于去年编辑
据我所知,在使用 GPU 训练和验证模型时,GPU 内存主要用于加载数据,向前和向后。据我所知,我认为 GPU 内存使用应该相同 1) 训练前,2) 训练后,3) 验证前,4) 验证后。但在我的例子中
我正在尝试在 PyTorch 中将两个复数矩阵相乘,看起来 the torch.matmul functions is not added yet to PyTorch library for com
我正在尝试定义二分类问题的损失函数。但是,目标标签不是硬标签0,1,而是0~1之间的一个 float 。 Pytorch 中的 torch.nn.CrossEntropy 不支持软标签,所以我想自己写
我正在尝试让 PyTorch 与 DataLoader 一起工作,据说这是处理小批量的最简单方法,在某些情况下这是获得最佳性能所必需的。 DataLoader 需要一个数据集作为输入。 大多数关于 D
Pytorch Dataloader 的迭代顺序是否保证相同(在温和条件下)? 例如: dataloader = DataLoader(my_dataset, batch_size=4,
PyTorch 的负对数似然损失,nn.NLLLoss定义为: 因此,如果以单批处理的标准重量计算损失,则损失的公式始终为: -1 * (prediction of model for correct
在PyTorch中,new_ones()与ones()有什么区别。例如, x2.new_ones(3,2, dtype=torch.double) 与 torch.ones(3,2, dtype=to
假设我有一个矩阵 src带形状(5, 3)和一个 bool 矩阵 adj带形状(5, 5)如下, src = tensor([[ 0, 1, 2], [ 3, 4,
我想知道如果不在第 4 行中使用“for”循环,下面的代码是否有更有效的替代方案? import torch n, d = 37700, 7842 k = 4 sample = torch.cat([
我有三个简单的问题。 如果我的自定义损失函数不可微会发生什么? pytorch 会通过错误还是做其他事情? 如果我在我的自定义函数中声明了一个损失变量来表示模型的最终损失,我应该放 requires_
我想知道 PyTorch Parameter 和 Tensor 的区别? 现有answer适用于使用变量的旧 PyTorch? 最佳答案 这就是 Parameter 的全部想法。类(附加)在单个图像中
给定以下张量(这是网络的结果 [注意 grad_fn]): tensor([121., 241., 125., 1., 108., 238., 125., 121., 13., 117., 12
什么是__constants__在 pytorch class Linear(Module):定义于 https://pytorch.org/docs/stable/_modules/torch/nn
我在哪里可以找到pytorch函数conv2d的源代码? 它应该在 torch.nn.functional 中,但我只找到了 _add_docstr 行, 如果我搜索conv2d。我在这里看了: ht
如 documentation 中所述在 PyTorch 中,Conv2d 层使用默认膨胀为 1。这是否意味着如果我想创建一个简单的 conv2d 层,我必须编写 nn.conv2d(in_chann
我阅读了 Pytorch 的源代码,发现它没有实现 convolution_backward 很奇怪。函数,唯一的 convolution_backward_overrideable 函数是直接引发错
我对编码真的很陌生,现在我正在尝试将我的标签变成一种热门编码。我已经完成将 np.array 传输到张量,如下所示 tensor([4., 4., 4., 4., 4., 4., 4., 4., 4.
我正在尝试实现 text classification model使用CNN。据我所知,对于文本数据,我们应该使用一维卷积。我在 pytorch 中看到了一个使用 Conv2d 的示例,但我想知道如何
我有一个多标签分类问题,我正试图用 Pytorch 中的 CNN 解决这个问题。我有 80,000 个训练示例和 7900 个类;每个示例可以同时属于多个类,每个示例的平均类数为 130。 问题是我的
我是一名优秀的程序员,十分优秀!