
【1-3】pytorch
文章平均质量分 52
pytorch的函数介绍,问题集,以及基本使用
阿颖&阿伟
这个作者很懒,什么都没留下…
展开
-
【pytorch】PyTorch - SOFTMAX回归的从零开始实现
学习教材:动手学深度学习 PYTORCH 版(DEMO)(https://github.com/ShusenTang/Dive-into-DL-PyTorch)PDF 制作by [Marcus Yang](https://github.com/chenyang1999)直接代码:'''导入所需的包'''import torchfrom torch import nnfrom torch.nn import initimport numpy as np# 下载并读取数据集def lo原创 2020-11-12 16:47:44 · 506 阅读 · 1 评论 -
【pytorch】报错:expected scalar type Float but found Double
这个问题很明显就是网络内的参数类型不同意;修改:在前面添加:torch.set_default_tensor_type(torch.DoubleTensor)或者,在运行网络前添加:net = net.double()原创 2020-11-16 17:16:56 · 22443 阅读 · 5 评论 -
【pytorch】PyTorch -多层感知机(MLP)的从零开始实现
学习教材:动手学深度学习 PYTORCH 版(DEMO)(https://github.com/ShusenTang/Dive-into-DL-PyTorch)PDF 制作by [Marcus Yang](https://github.com/chenyang1999)直接代码:import torchfrom torch import nnfrom torch.nn import initimport randomfrom IPython import displayfrom mat原创 2020-11-16 22:16:17 · 2018 阅读 · 1 评论 -
【pytorch】找不到d2lzh_pytorch包,No module named ‘d2lzh_pytorch’
这实际上是动手学深度学习PyTorch里的一个包,不可以直接pip下载。1.下载d2lzh_pytorch直接百度云:链接:https://pan.baidu.com/s/1zzPITzCscnyPUAIgSPAaiA提取码:svwp2.存放位置我是在Anaconda环境下安装PyTorch,然后在该位置解压d2lzh_pytorch包E:\anaconda\anaconda3\envs\pytorch\Lib3.调用:import syssys.path.append("..")原创 2020-10-06 08:37:59 · 15125 阅读 · 21 评论 -
【pytorch】torch.Tensor详解和常用操作
学习教材:动手学深度学习 PYTORCH 版(DEMO)(https://github.com/ShusenTang/Dive-into-DL-PyTorch)PDF 制作by [Marcus Yang](https://github.com/chenyang1999)1.tensor简介在PyTorch中,torch.Tensor是存储和变换数据的主要工具。Tensor与Numpy的多维数组非常相似。Tensor还提供了GPU计算和自动求梯度等更多功能,这些使Tensor更适合深度学习。2原创 2020-10-29 17:06:36 · 89347 阅读 · 3 评论 -
【pytorch】 自动求梯度详解
学习教材:动手学深度学习 PYTORCH 版(DEMO)(https://github.com/ShusenTang/Dive-into-DL-PyTorch)PDF 制作by [Marcus Yang](https://github.com/chenyang1999)本文目录:1. 前言2.Tensor的.requires_grad属性3. Tensor 的 detach()函数4. Tensor的 .grad_fn属性1. 前言Pytorch 提供的autograd包能够根据输入和前向传播原创 2020-10-30 19:28:15 · 2281 阅读 · 2 评论 -
【pytorch】PyTorch零基础实现线性回归
学习教材:动手学深度学习 PYTORCH 版(DEMO)(https://github.com/ShusenTang/Dive-into-DL-PyTorch)PDF 制作by [Marcus Yang](https://github.com/chenyang1999)直接码代码:#在jupyter notebook上运行需要添加%matplotlib inline import torch from IPython import displayfrom matplotlib im原创 2020-11-04 22:15:21 · 484 阅读 · 1 评论 -
【pytorch】torch.nn.Module.load_state_dict详解
参考博客:https://blog.youkuaiyun.com/weixin_40522801/article/details/106563354https://blog.youkuaiyun.com/yangwangnndd/article/details/100207686函数定义:load_state_dict(state_dict, strict=True)作用:使用 state_dict 反序列化模型参数字典。用来加载模型参数。将 state_dict 中的 parameters 和 buffers 复制原创 2021-05-10 20:49:21 · 22170 阅读 · 0 评论 -
【pytorch】torch.nn.DataParallel用法详解
参考博客:https://blog.youkuaiyun.com/baidu_35120637/article/details/110785801https://blog.youkuaiyun.com/zhjm07054115/article/details/104799661https://blog.youkuaiyun.com/anshiquanshu/article/details/108186955在多卡的GPU服务器,当我们在上面跑程序的时候,当迭代次数或者epoch足够大的时候,我们通常会使用nn.DataParallel原创 2021-05-10 23:34:13 · 53589 阅读 · 3 评论 -
【pytorch系列】model.eval()用法详解
格式:model.eval()作用:主要是针对model 在训练时和评价时不同的 Batch Normalization 和 Dropout 方法模式。Batch Normalization其作用对网络中间的每层进行归一化处理,并且使用变换重构(Batch Normalization Transform)保证每层提取的特征分布不会被破坏。训练时是针对每个mini-batch的,但是测试是针对单张图片的,即不存在batch的概念。由于网络训练完成后参数是固定的,每个batch的均值和方差是不变转载 2021-05-11 00:06:54 · 37389 阅读 · 0 评论 -
【pytorch】 torch.utils.data.DataLoader用法详解
参考:https://pytorch.org/docs/stable/data.html?highlight=torch%20utils%20data%20dataloader#torch.utils.data.DataLoaderhttps://blog.youkuaiyun.com/u014380165/article/details/79058479官方函数定义:class torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False,原创 2021-05-11 10:12:42 · 9259 阅读 · 0 评论 -
【pytorch】torchvision.transforms.Compose
class torchvision.transforms.Compose([``````])作用:几个图像变换组合在一起,按照给出的transform顺序进行计算。主要装载 transforms模块,提供了一般的图像转换操作类。class torchvision.transforms.ToTensor 把shape=(H x W x C)的像素值范围为[0, 255]的PIL.Image或者numpy.ndarray转换成shape=(C x H x W)的像素值范围为[0.0, 1.0]的to原创 2021-05-11 10:51:37 · 2265 阅读 · 0 评论 -
【pytorch】 with torch.no_grad():用法详解
在pytorch写的网络中,with torch.no_grad():非常常见。首先,关于python中的with:with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭/线程中锁的自动获取和释放等。例如:file = open("1.txt")data = file.read()file.close()存在问题如下:(1)文件读取发生异常,但没有进行任何处理;(2)可能忘记关闭文件句柄;改进:try:原创 2021-05-11 23:46:00 · 168740 阅读 · 23 评论 -
【pytorch】torch.unsqueeze()和torch.squeeze()
torch.unsqueeze()原型:torch.unsqueeze(input, dim, out=None) 参数:tensor (Tensor) – 输入张量dim (int) – 插入维度的索引out (Tensor, optional) – 结果张量或者:Tensor2 = Tensor1.torch.unsqueeze( dim) 参数:dim (int) – 插入维度的索引功能:返回一个新的tensor,这个tensor 在指定的位置被插入了一个大小为1的新维度原创 2021-05-12 08:59:36 · 349 阅读 · 0 评论 -
【pytorch】卷积操作原理解析与nn.Conv2d用法详解
stride=1,padding=0,groups=1,bias=True)函数作用:定义一个卷积核,方便后面进行二维卷积操作参数数据类型int输入图像通道数int卷积产生的通道数卷积核尺寸,可以设为1个int型数或者一个(int, int)型的元组。例如(2,3)是高2宽3卷积核stride卷积步长,默认为1。可以设为1个int型数或者一个(int, int)型的元组。padding填充操作,控制padding_mode的数目。简言之,就是决定图像边沿填充的方式。原创 2021-05-14 21:18:58 · 35565 阅读 · 12 评论 -
【pytorch】ResNet中的BasicBlock与bottleneck
https://zhuanlan.zhihu.com/p/349717627ResNet与残差块深度卷积网络的瓶颈:理论上,增加网络层数后,网络可以进行更加复杂的特征模式的提取,所以当模型更深时可以取得更好的结果。但VGG、GoogLeNet等网络单纯增加层数遇到了一些瓶颈:简单增加卷积层,训练误差不但没有降低,反而越来越高。在CIFAR-10、ImageNet等数据集上,单纯叠加3×3卷积,何恺明等[1]人发现,训练和测试误差都变大了。这主要是因为深层网络存在着梯度消失或者爆炸的问题,模型层数越多,原创 2021-05-16 00:08:59 · 38755 阅读 · 6 评论 -
pytorch,torchvision与cuda版本对应关系
查询官网:https://pytorch.org/get-started/previous-versions/原创 2021-06-15 19:59:54 · 5308 阅读 · 0 评论 -
pytorch 报错:RuntimeError: Invalid device string: ‘0‘
具体原因:windows下不支持函数 torch.cuda.set_device(args.gpu),在linux下支持。因此需要替换这行代码(怎么改不会)。如下:# torch.cuda.set_device(args.gpu)# model = model.cuda(args.gpu)cuda = torch.cuda.is_available()if cuda: model.cuda()...原创 2021-06-17 00:57:22 · 5179 阅读 · 1 评论 -
【pytorch系列】 torch.cat()
原文链接:https://www.cnblogs.com/JeasonIsCoding/p/10162356.html作用:torch.cat是将两个张量(tensor)拼接在一起,cat是concatenate的意思,即拼接,联系在一起。如果我们有两个tensor是A和B,想把他们拼接在一起,需要如下操作:C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)>>> impor转载 2021-06-18 12:59:13 · 836 阅读 · 0 评论 -
【pytorch】 nn.MultiheadAttention 详解
关于MultiheadAttention :一种注意力机制,常置于Transformer的开头。Transformer自2017年推出之后,已经横扫NLP领域,成为当之无愧的state-of-the-art。原始paper “Attention is All you Need”中对attention提出了通用的query/key/value抽象。新型的网络结构: Transformer,里面所包含的注意力机制称之为 self-attention。这套 Transformer 是能够计算 input 和原创 2021-06-29 11:18:31 · 59127 阅读 · 10 评论 -
【pytorch】torch.nn.MaxPool2d详解与尺寸计算
torch.nn.MaxPool2d功能:MaxPool 最大池化层,池化层在卷积神经网络中的作用在于特征融合和降维。池化也是一种类似的卷积操作,只是池化层的所有参数都是超参数,是学习不到的。作用:maxpooling有局部不变性而且可以提取显著特征的同时降低模型的参数,从而降低模型的过拟合。只提取了显著特征,而舍弃了不显著的信息,是的模型的参数减少了,从而一定程度上可以缓解过拟合的产生。函数原型:nn.MaxPool2d(kernel_size, stride, padding=1, dila转载 2021-07-07 23:32:39 · 14188 阅读 · 3 评论 -
pytorch预训练模型的相关研究(关于DEKR,SWAHR,HigherHRNet预训练模型的相关研究)
这是我研究2D buttom-up HPE算法遇到的一些问题。问题描述:3个网络模型都是2D姿态估计网络,代码相似度很高。然而默认都是使用了imagenet的与训练模型。咨询了CVPR的作者,推荐我使用coco的预训练模型来训练CrowdPose数据集。然而训练CrowdPose数据集的时候使用了coco的预训练模型,DEKR模型报错:通道数错误,这个情理之中的。因为coco采用17个关键点,而CrowdPose采用14个关键点,模型在hrnet之后就会卷积出不同的通道。然后疑惑的是,SWAHR,Hi原创 2021-09-03 19:33:36 · 1656 阅读 · 9 评论 -
【pytorch】姿态估计hrnet报错:ModuleNotFoundError: No module named ‘nms.cpu_nms‘
该问题可能出自基于HRNet框架的一系列2D-HPE系列网络代码运行一个2D姿态估计网络报错:原因:该代码继承hrnet的代码框架,github因此省略了安装步骤,我只是安装了依赖包requirements.txt,在安装过程中漏了一步。解决步骤:在lib目录下输入make即可。...原创 2022-01-10 22:27:01 · 2852 阅读 · 7 评论 -
【pytorch】torch.nn.Linear详解
在学习transformer时,遇到过非常频繁的nn.Linear()函数,这里对nn.Linear进行一个详解。参考:https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html概要 1. nn.Linear的原理:2. nn.Linear的使用:3. nn.Linear的源码定义:4. nn.Linear的官方源码:1. nn.Linear的原理:从名称就可以看出来,nn.Linear表示的是线性变换,原型就是初级数学原创 2022-03-18 10:58:50 · 62216 阅读 · 7 评论 -
【pytorch】报错:RuntimeError: one of the variables needed for gradient computation has been modified···
修改网络时pytroch报错:RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1600, 16, 256]], which is output 0 of CudnnConvolutionBackward, is at version 1; expected version 0 ins原创 2022-04-13 01:51:49 · 4668 阅读 · 0 评论