
torch
文章平均质量分 50
Nicola-Zhang
0
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
各种loss实现
bsz = pred.shape[0] if pred.dim() != target.dim(): # one_hot_target, weight = _expand_onehot_labels(target, pred.size(-1)) one_hot_target = F.one_hot(target).float() # pred_norm = pred.sigmoid() if self.requ...原创 2021-12-31 14:45:32 · 863 阅读 · 0 评论 -
Torch之读取梯度
文章目录pytorch: grad can be implicitly created only for scalar outputs原理: 出现这个原因是由于在backword时,没有计算loss导致,需要计算loss;或者在backword时,传入参考,作为loss计算的标准。读取gradient针对具体变量针对model中的某些层pytorch: grad can be implicitly created only for scalar outputs原理: 出现这个原因是由于在backword原创 2020-12-16 10:27:08 · 2884 阅读 · 0 评论 -
Pytorch :Trying to backward through the graph a second time, but the buffers have already been freed
Pytorch :Trying to backward through the graph a second time, but the buffers have already been freed运行原理解决方案运行原理stackoverflow.com/questions/47120126/how-to-calculate-loss-over-a-number-of-images-and-then-back-propagate-the-averagblog.youkuaiyun.com/qq_429071原创 2020-12-16 09:53:11 · 409 阅读 · 0 评论 -
torch之Sequential使用方式
torch之Sequential如下两种方式一致,一种是自动命名,一种是手动命名。# Example of using Sequential model = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() )原创 2020-08-23 09:04:39 · 3940 阅读 · 0 评论 -
torch之随机数生成
Torch之随机数生成方式torch.rand() torch.randn()torch.normal()torch.linespace()1. 均匀分布torch.rand(*sizes, out=None) → Tensor返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。参数:sizes (int...) - 整数序列,定义了输出张量的形状out (Tensor, optinal) - 结果张量例子:torch.ra原创 2020-06-03 14:51:04 · 27549 阅读 · 0 评论 -
Torch中关于参数requires_grad=True的讲解
Torch中关于参数requires_grad=True的讲解转载 2020-03-23 17:48:01 · 28605 阅读 · 1 评论 -
torch.nn.Linear函数参数使用
import torch x = torch.randn(128, 20) # 输入的维度是(128,20)m = torch.nn.Linear(20, 30) # 20,30是指维度output = m(x)print('m.weight.shape:\n ', m.weight.shape)print('m.bias.shape:\n', m.bias.shape)print(...原创 2020-02-16 16:37:06 · 7994 阅读 · 0 评论 -
label smoothing
转载 2019-08-20 10:26:25 · 384 阅读 · 0 评论 -
torch之transforms使用
torchvision.transforms是pytorch中的图像预处理包一般用Compose把多个步骤整合到一起,比如说 transforms.Compose([transforms.CenterCrop(10), transforms.ToTensor(),])这样就把两个步骤整合到一起;接下来介绍transforms中的函数 Resize:...原创 2019-08-20 17:38:54 · 7185 阅读 · 1 评论 -
torch之tensor.contiguous
contiguous:view只能用在contiguous的variable上。如果在view之前用了transpose, permute等,需要用contiguous()来返回一个contiguous copy。一种可能的解释是:有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把...转载 2019-08-20 22:07:24 · 1218 阅读 · 1 评论 -
torch下imageNet数据集存放格式
下载数据集;提取training data:mkdir train && mv ILSVRC2012_img_train.tar train/ && cd traintar -xvf ILSVRC2012_img_train.tar && rm -f ILSVRC2012_img_train.tarfind . -name "*.tar"...原创 2019-08-21 18:50:00 · 3171 阅读 · 0 评论 -
torch之nn.Linear
首先说明一点,nn.Linear为类,而不是函数,这在torch很是常见,不要想当然认为是C语言中;CLASS torch.nn.Linear(in_features, out_features, bias=True)原理:矩阵相乘,和数学上的形式完全一致参数in_features – size of each input sampleout_features – size...原创 2019-08-22 22:41:20 · 8915 阅读 · 0 评论 -
torch之tensor
torch中tensor中默认tensor的requires_grad = False(default)原创 2019-08-31 13:36:11 · 321 阅读 · 0 评论 -
torch之模型加载load_state_dict
ERROR:PyTorch加载模型model.load_state_dict()问题,Unexpected key(s) in state_dict: "module.features…,Expected .希望将训练好的模型加载到新的网络上。如上面题目所描述的,PyTorch在加载之前保存的模型参数的时候,遇到了问题。Unexpected key(s) in state_dict: "mod...转载 2019-09-02 11:04:35 · 102517 阅读 · 7 评论 -
torch中对nn.Parameters中部分参数加入噪声
Write to .data instead? [RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.]如题:当进行下述操作时,my_adam = optim.Adam([z], lr=self.lr_shape)# and this comes in the tra...原创 2019-09-09 17:09:39 · 943 阅读 · 0 评论 -
PyTorch中 tensor.detach() 和 tensor.data 的区别
PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad = False, 这样有些时候是不安全的, 因为 x.data 不能被 autograd 追踪求微分 。 .detach() 返回相同数据的 tensor ,且 requires_grad=False...转载 2019-10-02 12:13:09 · 478 阅读 · 0 评论 -
关于pytorch inplace opeartion,需要知道的几件事
关于pytorch inplace opeartion,需要知道的几件事原文链接:https://zhuanlan.zhihu.com/p/38475183转载 2019-10-02 12:17:27 · 226 阅读 · 0 评论 -
torch中dataloader加速
class data_prefetcher(): def __init__(self, loader): self.loader = iter(loader) self.stream = torch.cuda.Stream() self.mean = torch.tensor([0.485 * 255, 0.456 * 255, 0.406 ...原创 2019-12-02 12:10:18 · 926 阅读 · 1 评论 -
torch之nn.utils.clip_grad_norm
nn.utils.clip_grad_norm(parameters, max_norm, norm_type=2)这个函数是根据参数的范数来衡量的Parameters:parameters (Iterable[Variable]) – 一个基于变量的迭代器,会进行归一化(原文:an iterable of Variables that will have gradients normal...原创 2019-08-10 20:44:37 · 19657 阅读 · 0 评论 -
torch中国模型保存与加载
pytorch保存模型等相关参数,利用torch.save(),以及读取保存之后的文件假设网络为model = Net(), optimizer = optim.Adam(model.parameters(), lr=args.lr), 假设在某个epoch,我们要保存模型参数,优化器参数以及epoch一、保存模型先建立一个字典,保存三个参数:state = {‘net':model....原创 2019-08-23 18:55:30 · 664 阅读 · 0 评论 -
torch之optimizer.step() 和loss.backward()和scheduler.step()的关系与区别
torch之optimizer.step() 和loss.backward()和scheduler.step()的关系与区别由于接触torch时间不久,所有对此比较困惑,遇到如下博文解释十分详细,故转载至此。(原文地址)因为有人问我optimizer的step为什么不能放在min-batch那个循环之外,还有optimizer.step和loss.backward的区别;那么我想把答案记...转载 2019-07-12 15:32:58 · 26383 阅读 · 3 评论 -
AttributeError: module 'scipy.misc' has no attribute 'imread'
解决方案:https://www.jianshu.com/p/fc4b33439b7d转载 2019-06-08 21:53:00 · 6125 阅读 · 0 评论 -
torch学习笔记
CPU/GPU上加载模型https://blog.youkuaiyun.com/g11d111/article/details/80909403原创 2019-12-02 12:08:26 · 178 阅读 · 0 评论 -
torch之nn.moduleList 和Sequential由来、用法和实例
nn.moduleList 和Sequential由来、用法和实例注意:本博文详细记述了nn.moduleList 和 nn.Sequential的区别以及使用,解释比较清晰。转载 2019-07-08 17:11:33 · 1512 阅读 · 0 评论 -
ERROR:tensorboardx中TypeError: __init__() got an unexpected keyword argument 'log_dir'
问题描述:TypeError: init() got an unexpected keyword argument ‘log_dir’解决方案:v1.6 (log_dir): https://tensorboardx.readthedocs.io/en/v1.6/tensorboard.html#tensorboardX.SummaryWriter.initv1.7 (logdir): ...原创 2019-07-08 23:09:21 · 4794 阅读 · 0 评论 -
ERROR:ValueError: not enough values to unpack (expected 5, got 4)
问题描述:ValueError: not enough values to unpack (expected 5, got 4)eg:self.train_loader1, self.train_loader2, self.val_loader, self.test_loader, self.nclass = make_data_loader(args, **kwargs)解决方案:...原创 2019-07-08 23:14:31 · 13950 阅读 · 2 评论 -
torch之manual_seed
torch.manual_seed(args.seed) #为CPU设置种子用于生成随机数,以使得结果是确定的 if args.cuda: #为当前GPU设置随机种子;如果使用多个GPU,应该使用torch.cuda.manual_seed_all()为所有的GPU设置种子。 torch.cuda.manual_seed(args.seed)...原创 2019-07-03 22:36:47 · 694 阅读 · 0 评论 -
torch之数据类型及加载/模型加载在CPU和GPU间的转换
CPU/GPU上加载模型注:以上为原文地址pytorch可以将模型数据加载到CPU或者GPU上进行训练,也可以在两者之间转换,方式如下:CPU–>CPU,GPU–>GPUtorch.load('a.pk1')GPU–>CPUtorch.load('a.pk1', map_location = lambda storage, loc:storage)...转载 2019-07-13 17:46:40 · 2414 阅读 · 0 评论 -
torch之Dataloader详解
Dataloader原理及其使用注:torch学习过程中对于Datalodaer不解,以上博文对其原理解释较为清晰,故转之。转载 2019-07-13 17:49:53 · 26915 阅读 · 0 评论 -
torch之损失函数总结
详细博文总结如下:https://blog.youkuaiyun.com/weixin_41278720/article/details/90217694转载 2019-07-04 21:25:52 · 563 阅读 · 0 评论 -
PyTorch中在反向传播前为什么要手动将梯度清零?
对于torch中训练师时,反向传播前将梯度手动清零的理解知乎中见解:https://www.zhihu.com/question/303070254/answer/573037166转载 2019-07-04 23:38:04 · 7346 阅读 · 1 评论 -
Python之parser.add_argument
在使用parser进行传参时,遇到参数action,一直不得其解,终有所获,解释如下:parser.add_argument(‘--is_train’, action=’store_true’, default=False)其中“–is_train”参数在调用时,如下:python demo1.py #无参数传入时,使用默认值False python demo1.py --i...原创 2019-07-10 18:26:25 · 32523 阅读 · 7 评论 -
torch之distributed training(分布式训练)
对于分布式训练的理解分布式训练是多线程多节点训练,但是若是模型过大,导致单一图像已经超出单个GPU显存大小,则和单机多卡中的数据并行效果一致,都会导致out of memary问题;模型并行其运行速率太慢,相当于是每次只能在单一卡上运行,会出现显卡空闲的情况,解析见下:pytorch单机并行训练torch-1.0分布式训练实例解析,主要流程:parsers中添加相关参数解析...原创 2019-07-10 21:21:07 · 16458 阅读 · 4 评论 -
torch之DataLoader参数pin_memory解析
关于什么是锁页内存:pin_memory就是锁页内存,创建DataLoader时,设置pin_memory=True,则意味着生成的Tensor数据最开始是属于内存中的锁页内存,这样将内存的Tensor转义到GPU的显存就会更快一些。主机中的内存,有两种存在方式,一是锁页,二是不锁页,锁页内存存放的内容在任何情况下都不会与主机的虚拟内存进行交换(注:虚拟内存就是硬盘),而不锁页内存在主机内存不...原创 2019-07-10 21:52:26 · 21660 阅读 · 2 评论 -
torch之nn.Upsample
nn.Upsample详见官方手册nn.Upsample,但是简单解释如下:作用:上采样定义:CLASS torch.nn.Upsample(size=None, scale_factor=None, mode='nearest', align_corners=None)参数:计算:...原创 2019-07-11 16:34:27 · 19778 阅读 · 0 评论 -
torch之nn.conv2d
原博客解析:https://blog.youkuaiyun.com/g11d111/article/details/82665265转载 2019-07-06 21:18:56 · 922 阅读 · 0 评论 -
torch之BatchNorm2D详解
知乎上面有关各种Normalization算法理解note 11:BatchNorm2D官方手册,具体解析,如下:How to set learning rate as 0 in BN layer中所讲有关的参数affine理解如下:Setting affine=False will remove the gamma and beta terms from the calculati...原创 2019-07-06 22:10:56 · 30935 阅读 · 0 评论 -
torch之AvgPool2d
torch之AvgPool2d定义:CLASS torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)参数:shape:参考:官方手册原创 2019-07-11 19:32:55 · 22746 阅读 · 1 评论 -
Torch之CUDA使用指定显卡
改变系统环境变量仅使目标显卡,编辑 .bashrc文件,添加系统变量export CUDA_VISIBLE_DEVICES=0 #这里是要使用的GPU编号,正常的话是从0开始使用torch.cuda接口函数#在生成网络对象之前:torch.cuda.set_device(0)运行文件之前指定显卡CUDA_VISIBLE_DEVICES=7 python ret...原创 2019-05-29 23:17:28 · 25078 阅读 · 2 评论