PyTorch
PyTorch
西笑生
西笑生
展开
-
PyTorch 自定义序列数据集
主要是DataLoader, TensorDataset。inputs和targets的内容。PyTorch 自定义序列数据集。原创 2023-03-15 11:19:48 · 218 阅读 · 0 评论 -
PyTorch 如何按照不同的策略调整学习率
PyTorch 如何按照不同的策略调整学习率flyfish加快训练的一个办法是随时间慢慢减少学习率,这个就是学习率衰减(learning rate decay)例如我们想要在训练的过程,学习率不是一直都是0.01,我们想根据规则进行变化的。1、根据 scale更改学习率def adjust_learning_rate(optimizer, scale): """ Scale...原创 2020-03-13 19:14:46 · 791 阅读 · 0 评论 -
PyTorch - Neural Networks(神经网络)
PyTorch - Neural Networks(神经网络)flyfish参考网址import torchimport torch.nn as nnimport torch.nn.functional as Fclass Net(nn.Module): def __init__(self): super(Net, self).__init__() ...原创 2019-05-24 18:03:27 · 747 阅读 · 0 评论 -
PyTorch 错误 导出onnx提示 Unexpected key(s) in state_dict: “module.classifier.0.weight
PyTorch 错误 导出onnx提示 Unexpected key(s) in state_dict: "module.classifier.0.weightflyfishTraceback (most recent call last): model.load_state_dict(checkpoint['state_dict']) File "torch/nn/modules/module.py", line 1052, in load_state_dict self.__cl原创 2021-03-08 18:28:11 · 2491 阅读 · 0 评论 -
PyTorch 警告 UserWarning: This overload of cuda is deprecated cuda(torch.device device, bool async,...
PyTorch 警告 UserWarning: This overload of cuda is deprecated cuda(torch.device device, bool async,…flyfishProcessinggender.py:204: UserWarning: This overload of cuda is deprecated: cuda(torch.device device, bool async, *, torch.memory_format memory_forma原创 2021-03-08 18:27:01 · 3247 阅读 · 0 评论 -
PyTorch - transforms.ColorJitter 改变图像的属性:亮度(brightness)、对比度(contrast)、饱和度(saturation)和色调(hue)
PyTorch - transforms.ColorJitter 改变图像的属性:亮度(brightness)、对比度(contrast)、饱和度(saturation)和色调(hue)flyfishjitter的意思v. 紧张不安 / 抖动 / 战战兢兢 / 神经过敏n. 紧张不安 / 晃动 / 偏移 / 振动这里抖动或者偏移 是随机变化的含义下面以亮度brightness举例,假设brightness设置为0.5brightness_change = transforms.ColorJit原创 2020-09-28 19:18:14 · 61973 阅读 · 1 评论 -
PyTorch - torch.gather
PyTorch - torch.gatherflyfish这是一篇让您能懂的torch.gather的文章,下面的例子比官网更容易说明该函数的用法函数的作用,沿dim指定的轴收集值(Gathers values along an axis specified by dim),相当于 我们有一个二维表根据指定的索引值,把数据取出来。例如一个索引中存储了二维表中每行的最大值或者最小值,我们就可以使用该函数把二维表中每行的最大值都取出来。重要的3个参数input 表示输入的tensor,从哪里找数据d原创 2020-05-22 19:31:06 · 169 阅读 · 0 评论 -
PyTorch 错误 RuntimeError: invalid argument 5: k not in range for dimension at /pytorch/aten/src/THC/g
PyTorch 错误 RuntimeError: invalid argument 5: k not in range for dimension at /pytorch/aten/src/THC/generic/THCTensorTopK.cu:26flyfish具体提示如下Processinggender.py:212: UserWarning: This overload of cuda is deprecated: cuda(torch.device device, bool async,原创 2021-03-05 19:30:53 · 2583 阅读 · 0 评论 -
PyTorch错误 if input.dim() == 2 and bias is not None
PyTorch错误 if input.dim() == 2 and bias is not Noneflyfish详细提示 return F.linear(input, self.weight, self.bias) File "python3.6/site-packages/torch/nn/functional.py", line 1688, in linear if input.dim() == 2 and bias is not Nonedef linear(input,原创 2021-03-27 15:54:54 · 1095 阅读 · 0 评论 -
PyTorch 警告 UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` inst
PyTorch 警告 UserWarning: volatile was removed and now has no effect. Use with torch.no_grad(): instead.flyfishtorch.autograd.Variable(inputs, volatile=True)新版PyTorch中的Variables已经废弃#之前的代码...x = Variable(torch.randn(1), volatile=True)return x#现在的代码原创 2021-03-08 18:26:01 · 1575 阅读 · 0 评论 -
PyTorch 错误 RuntimeError: CUDA error: device-side assert triggered
PyTorch 错误 RuntimeError: CUDA error: device-side assert triggeredflyfish具体提示如下/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:108: cunn_ClassNLLCriterion_updateOutput_kernel: block: [0,0,0], thread: [2,0,0] Assertion `t >= 0 && t < n_classe原创 2021-03-05 19:40:35 · 57010 阅读 · 11 评论 -
PyTorch - unsqueeze和squeeze
PyTorch - unsqueeze和squeezeflyfishimport torchimport torch.nn as nnimport torch.nn.functional as F#a=torch.randn(3,2)a=torch.randn(3,1)b0=a.unsqueeze(-2)b1=a.unsqueeze(-1)b2=a.unsqueeze(0)...原创 2019-06-17 18:12:08 · 1037 阅读 · 0 评论 -
PyTorch - torch.nn.Sequential
PyTorch - torch.nn.Sequentialflyfish官网的示例# Example of using Sequentialmodel = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() ...原创 2019-05-28 17:59:54 · 1863 阅读 · 0 评论 -
PyTorch - torchvision.transforms.Compose 几个图像变换组合在一起
PyTorch - torchvision.transforms.Compose 几个图像变换组合在一起flyfishtransforms是常见的图像变换torchvision.transforms.Compose是将几个变换组合在一起原始图片经过变换的图片代码如下import numpy as npimport cv2import osimport torchimport mathimport torchvision.transforms as transformsfrom原创 2020-09-28 19:19:03 · 1995 阅读 · 1 评论 -
PyTorch 警告 UserWarning: You are trying to export the model with onnx:Upsample for ONNX opset version
PyTorch 警告 UserWarning: You are trying to export the model with onnx:Upsample for ONNX opset version 9flyfish警告详细内容PyTorch的pth模型转换onnx格式时出现的警告lib/python3.6/site-packages/torch/onnx/symbolic_helper.py:267: UserWarning: You are trying to export the model原创 2021-03-22 17:52:56 · 2832 阅读 · 1 评论 -
PyTorch编写代码遇到的问题
训练人脸检测mtcnn遇到的问题flyfishno module named xxxxxx为自定义文件夹的名字因为搜索不到,所以将当前路径加入到包的搜索目录解决方法:import syssys.path.append(’…’) #将上层目录加入到搜索路径中sys.path.append(’/home/xxx’) # 绝对路径import ossys.path.append(os...原创 2020-03-03 17:50:12 · 15165 阅读 · 1 评论 -
PyTorch - index_fill
PyTorch - index_fillflyfish通过按index中给定的顺序 选择索引,用val值填充 自己(自张量)的元素。dim(int)–索引所依据的维度index(LongTensor)–要填充的自张量的索引val(浮点数)–要填充的值import torcha = torch.randn(4, 3)print(a)# tensor([[-1.7189, 0.9798, -0.0428],# [ 0.7184, -0.2824, -1.0289],#原创 2020-05-21 20:22:19 · 3536 阅读 · 0 评论 -
PyTorch错误 torch.addmm RuntimeError: mat1 dim 1 must match mat2 dim 0
PyTorch错误 torch.addmm RuntimeError: mat1 dim 1 must match mat2 dim 0flyfish详细提示File "python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/media/ubuntu/data/pytorch1.7/soothsaye原创 2021-03-27 15:54:24 · 6978 阅读 · 0 评论 -
PyTorch 迁移模型的两种方案
PyTorch 迁移模型的两种方案 - 如何将mobilenet v2的输出更改为三分类flyfish原版提供的模型通常是在ImageNet上训练好的,迁移可以是从头开始训练这是使用偷懒的方案,第一种方案是使用预先训练好的网络初始化我们自己的网络后面做的与平常做没差别第二种方案 是不仅是使用预先训练好的网络初始化我们自己的网络,还要冻结权重。也就是最后一层的网络之外的网络不变,权重值...原创 2020-03-05 18:54:29 · 1395 阅读 · 2 评论 -
PyTorch - torch.nn.PReLU
PyTorch - torch.nn.PReLUflyfishPReLU是Parametric ReLU示例1import torchimport torch.nn as nninput = torch.arange(0, 12).view(1,3,2,2).float()input = torch.randn(1,3,2,2)print(input)m = nn.PReLU(3)output = m(input)print(output)计算过程数学表达式:PReLU(x)=原创 2020-06-09 19:23:25 · 14281 阅读 · 0 评论 -
PyTorch - torch.ones_like、torch.zeros_like、 torch.full_like
PyTorch - torch.ones_like、 torch.zeros_like、 torch.full_likeflyfishimport torchinput = torch.rand(3, 4)print(input)# tensor([[0.5840, 0.8260, 0.7539, 0.2138],# [0.9743, 0.0964, 0.7610, 0.5746],# [0.6247, 0.3334, 0.6949, 0.9065]])#原创 2021-07-26 19:55:16 · 1105 阅读 · 0 评论 -
PyTorch 在CPU推理的某种情况下内存增长的问题
PyTorch 在CPU推理的某种情况下内存增长的问题flyfishPyTorch版本1.5,1.7在CPU的推理下PyTorch使用的是Intel的mkldnn通过mkldnn.enabled=False,确定问题出在 MKLDNN解决方案1为了提高性能,可以将LRU_CACHE_CAPACITY设置为一个较小的数字(默认为1024,可以设置为1),以减少内存的使用。mkldnn允许用户通过环境值LRU_CACHE_CAPACITY更改mkldnn-bridge中的原始缓存的功能执行命令如原创 2021-03-25 17:55:55 · 3119 阅读 · 1 评论 -
PyTorch - torch.clamp
PyTorch - torch.clampflyfish将输入input张量每个元素的缩放到区间 [min,max]clamp的意思是n. 夹具;夹钳;夹子;车轮夹锁(用于锁住违章停放的车辆)v. 固定;紧紧抓住;紧夹住;被抓住这里是数据的缩放,相当于我有一大群数字大部分是 1到10之间,有极个别是 -10, 100等,我想去除极个别数字的影响,就可以用到该函数输入参数的时候min max 也可以从中选择一个import torcha = torch.arange(0, 16).view原创 2020-05-22 19:33:57 · 474 阅读 · 0 评论 -
torch.utils.data.DataLoader中参数collate_fn的作用
import torchimport torch.nn.functional as Fimport numpy as npfrom torch.utils.data import Datasetclass CustomDataset(Dataset): def __init__(self, x, y): self.x = x self.y = y def __getitem__(self, idx): return self.x[i原创 2020-12-18 19:17:41 · 1947 阅读 · 0 评论 -
PyTorch - torch.Tensor.repeat
PyTorch - repeatflyfishtorch.Tensor.repeat沿指定维度重复此张量import torch# 可以用行列来描述,更容易理解,如何重复[1, 2, 3]的问题a = torch.tensor([1, 2, 3])#tensor([1, 2, 3])#如何重复[1, 2, 3]的问题print(a.repeat(2)) # 2 * 原列数#tensor([1, 2, 3, 1, 2, 3])print(a.repeat(1,2)) #里面的数与a.r原创 2021-07-26 19:56:20 · 205 阅读 · 0 评论 -
PyTorch - 语句中优先级的一个例子
PyTorch - 语句中优先级的一个例子语句是 b = a[:, 1] >= 0.2flyfish语法解释import torcha = torch.randn(4, 3)print("a:",a)# a: tensor([[ 1.5335, 0.4832, 0.7277],# [-0.3768, -0.7656, -0.7159],# [-0.6221, -0.3336, -0.4310],# [ 0.1093, 0.59原创 2020-05-21 20:22:38 · 360 阅读 · 0 评论 -
PyTorch - torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le
PyTorch - torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.leflyfishtorch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le以上全是简写参数是input, other, out=None逐元素比较input和other返回是torch.BoolTensorimport torcha=torch.tensor([[1, 2], [3, 4]])b=torch.tens原创 2020-05-28 20:44:21 · 8637 阅读 · 0 评论 -
PyTorch - BatchNorm2d
PyTorch - BatchNorm2dflyfish术语问题在《深入浅出PyTorch》这本书中翻译成归一化在花书《深度学习》书中翻译成标准化在《深度学习之美》书中翻译成规范化在《动手学深度学习》书中翻译成归一化在《深度学习卷积神经网络从入门到精通》书中翻译成归一化归一化,因为带了一字,容易被理解成将数据映射到[0,1], 而标准化有把数据映射到一个合理的分布的意思,翻译的不统一,容易造成讨论的概念不一致,可以参考特征缩放(Feature_scaling)我这里采用了 标准化 的翻译原创 2020-06-08 20:23:17 · 1407 阅读 · 0 评论 -
PyTorch - torch.max、torch.min
PyTorch - torch.max、torch.minflyfishtorch.max找出输入张量中所有元素的最大值参数input(张量)–输入张量。dim(int)–要减少的维。keepdim(布尔 bool)–输出张量是否保持dim。 默认值:False。out(元组 tuple,可选)–两个输出张量的结果元组(max,max_indices)import torch# 一维的情况a = torch.randn(1, 3)print(a)#tensor([[ 0.4084,原创 2020-05-21 20:22:48 · 1653 阅读 · 0 评论 -
PyTorch - torch.nn.Upsample
PyTorch - torch.nn.Upsampleflyfish上采样输入是minibatch x channels x height x width输出是H × scale_factorW × scale_factor本来名字是上采样,还可以根据参数使用变成下采样与torch.nn.functional.interpolate对比torch.nn.functional.interpolate完全可以替代该函数import torchimport torch.nn as nn原创 2020-06-02 19:28:13 · 1965 阅读 · 0 评论 -
PyTorch实现线性回归(推荐的版本包括模型训练、保存、推理使用等)
TensorFlow - 线性回归(1)flyfish摘自 《面向机器智能TensorFlow实践》import tensorflow as tfW = tf.Variable(tf.zeros([2, 1]), name="weights")b = tf.Variable(0., name="bias")def inference(X): return tf.m原创 2018-01-24 19:33:57 · 452 阅读 · 0 评论 -
PyTorch - torch.nn.ModuleList
PyTorch - torch.nn.ModuleListflyfishimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom collections import namedtuple#from collections import listfrom typing import List, Tuple...原创 2019-06-17 18:12:42 · 2918 阅读 · 0 评论 -
PyTorch - What is PyTorch? (PyTorch 入门)
PyTorch - What is PyTorch? (PyTorch 入门)flyfish参考网址一import torchx = torch.empty(4, 3)print(x) # =============================================================================# tensor([[6.7333e+2...原创 2019-05-24 18:03:50 · 355 阅读 · 0 评论 -
PyTorch - Autograd: Automatic Differentiation(自动微分)
PyTorch - Autograd: Automatic Differentiation(自动微分)flyfish参考网址import torchimport numpy as npfrom torch.autograd import Variablea = torch.randn(2, 2)print(a)a = ((a * 3) / (a - 1))print(a)pri...原创 2019-05-24 18:03:38 · 734 阅读 · 0 评论 -
PyTorch的 transpose、permute、view、reshape
PyTorch的 transpose、permute、view、reshapeflyfish版本Python 3.6.9 GCC 8.3.0 on linuxPyTorch 1.4.0>>> import torch>>> print(torch.__version__) 1.4.0transpose() 和 permute()的区别a =...原创 2020-04-03 19:56:23 · 2174 阅读 · 0 评论 -
PyTorch - torch.nn.AdaptiveAvgPool2d
PyTorch - torch.nn.AdaptiveAvgPool2d自适应平均池化torch.nn.AdaptiveAvgPool2d 对应的是torch.nn.functional.adaptive_avg_pool2d输出数据相当于 minibatch x channels x height x widthimport torchimport torch.nn as nninput = torch.arange(0, 16).view(1,1,4,4).float()# tensor(原创 2020-06-02 19:18:38 · 3675 阅读 · 2 评论 -
PyTorch 错误 IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T
PyTorch 错误 IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a numberflyfish解决方案loss.data[0] 更改为loss.item()如果是其他tensor提示,改成tensor.item()...原创 2021-03-05 19:25:28 · 3989 阅读 · 2 评论