小白学Pytorch系列--Torch.nn API Vision Layers(15)

这篇具有很好参考价值的文章主要介绍了小白学Pytorch系列--Torch.nn API Vision Layers(15)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

小白学Pytorch系列–Torch.nn API Vision Layers(15)

小白学Pytorch系列--Torch.nn API Vision Layers(15)

方法 注释
nn.PixelShuffle 将形状张量 ( ∗ , C r 2 , H , W ) (*,C r^2,H,W) (Cr2,H,W)中的元素重新排列为形状张量 ( ∗ , C , H r , W r ) (*,C,H r,W r) (C,Hr,Wr),其中r是一个高阶因子。
nn.PixelUnshuffle 通过将形状张量 ( ∗ , C , H r , W r ) (*,C,H r,W r) (C,Hr,Wr)中的元素重新排列为形状张量 ( ∗ , C r 2 , H , W ) (*,C r^2,H,W) (Cr2,H,W)来反转PixelShuffle操作,其中r是一个降尺度因子。
nn.Upsample 对给定的多通道1D(时间)、2D(空间)或3D(体积)数据进行上采样。
nn.UpsamplingNearest2d 对由多个输入通道组成的输入信号应用二维最近邻上采样。
nn.UpsamplingBilinear2d 对由多个输入通道组成的输入信号应用二维双线性上采样。

nn.PixelShuffle

将形状张量 ( ∗ , C r 2 , H , W ) (*,C r^2,H,W) (Cr2,H,W)中的元素重新排列为形状张量 ( ∗ , C , H ∗ r , W ∗ r ) (*,C,H * r,W *r) (C,Hr,Wr),其中r是一个高阶因子。
小白学Pytorch系列--Torch.nn API Vision Layers(15)

>>> pixel_shuffle = nn.PixelShuffle(3)
>>> input = torch.randn(1, 9, 4, 4)
>>> output = pixel_shuffle(input)
>>> print(output.size())
torch.Size([1, 1, 12, 12])

nn.PixelUnshuffle

通过将形状张量 ( ∗ , C , H ∗ r , W ∗ r ) (*,C,H* r,W *r) (C,Hr,Wr)中的元素重新排列为形状张量 ( ∗ , C r 2 , H , W ) (*,C r^2,H,W) (Cr2,H,W)来反转PixelShuffle操作,其中r是一个降尺度因子。
小白学Pytorch系列--Torch.nn API Vision Layers(15)

>>> pixel_unshuffle = nn.PixelUnshuffle(3)
>>> input = torch.randn(1, 1, 12, 12)
>>> output = pixel_unshuffle(input)
>>> print(output.size())
torch.Size([1, 9, 4, 4])

nn.Upsample

小白学Pytorch系列--Torch.nn API Vision Layers(15)
小白学Pytorch系列--Torch.nn API Vision Layers(15)
小白学Pytorch系列--Torch.nn API Vision Layers(15)

>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[1., 2.],
          [3., 4.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='nearest')
>>> m(input)
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear')  # align_corners=False
>>> m(input)
tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],
          [1.5000, 1.7500, 2.2500, 2.5000],
          [2.5000, 2.7500, 3.2500, 3.5000],
          [3.0000, 3.2500, 3.7500, 4.0000]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> m(input)
tensor([[[[1.0000, 1.3333, 1.6667, 2.0000],
          [1.6667, 2.0000, 2.3333, 2.6667],
          [2.3333, 2.6667, 3.0000, 3.3333],
          [3.0000, 3.3333, 3.6667, 4.0000]]]])

>>> # Try scaling the same data in a larger tensor
>>> input_3x3 = torch.zeros(3, 3).view(1, 1, 3, 3)
>>> input_3x3[:, :, :2, :2].copy_(input)
tensor([[[[1., 2.],
          [3., 4.]]]])
>>> input_3x3
tensor([[[[1., 2., 0.],
          [3., 4., 0.],
          [0., 0., 0.]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear')  # align_corners=False
>>> # Notice that values in top left corner are the same with the small input (except at boundary)
>>> m(input_3x3)
tensor([[[[1.0000, 1.2500, 1.7500, 1.5000, 0.5000, 0.0000],
          [1.5000, 1.7500, 2.2500, 1.8750, 0.6250, 0.0000],
          [2.5000, 2.7500, 3.2500, 2.6250, 0.8750, 0.0000],
          [2.2500, 2.4375, 2.8125, 2.2500, 0.7500, 0.0000],
          [0.7500, 0.8125, 0.9375, 0.7500, 0.2500, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])

>>> m = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
>>> # Notice that values in top left corner are now changed
>>> m(input_3x3)
tensor([[[[1.0000, 1.4000, 1.8000, 1.6000, 0.8000, 0.0000],
          [1.8000, 2.2000, 2.6000, 2.2400, 1.1200, 0.0000],
          [2.6000, 3.0000, 3.4000, 2.8800, 1.4400, 0.0000],
          [2.4000, 2.7200, 3.0400, 2.5600, 1.2800, 0.0000],
          [1.2000, 1.3600, 1.5200, 1.2800, 0.6400, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])

nn.UpsamplingNearest2d

小白学Pytorch系列--Torch.nn API Vision Layers(15)

>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[1., 2.],
          [3., 4.]]]])

>>> m = nn.UpsamplingNearest2d(scale_factor=2)
>>> m(input)
tensor([[[[1., 1., 2., 2.],
          [1., 1., 2., 2.],
          [3., 3., 4., 4.],
          [3., 3., 4., 4.]]]])

nn.UpsamplingBilinear2d

小白学Pytorch系列--Torch.nn API Vision Layers(15)文章来源地址https://www.toymoban.com/news/detail-421087.html

>>> input = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
>>> input
tensor([[[[1., 2.],
          [3., 4.]]]])

>>> m = nn.UpsamplingBilinear2d(scale_factor=2)
>>> m(input)
tensor([[[[1.0000, 1.3333, 1.6667, 2.0000],
          [1.6667, 2.0000, 2.3333, 2.6667],
          [2.3333, 2.6667, 3.0000, 3.3333],
          [3.0000, 3.3333, 3.6667, 4.0000]]]])

到了这里,关于小白学Pytorch系列--Torch.nn API Vision Layers(15)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Pytorch-----torch.nn.Module.modules()

    在使用pytorch构建神经网络时,定义的网络模型必须要继承自torch.nn.Module这一父类。在Module类中,有一个函数可以返回网络中所有模块的迭代器。这就是torch.nn.Module.modules() 提示:以下是本篇文章正文内容,下面案例可供参考 源码中的解释如下: 不只是返回网络中的某一层,

    2024年02月14日
    浏览(33)
  • PyTorch中的torch.nn.Parameter() 详解

    今天来聊一下PyTorch中的torch.nn.Parameter()这个函数,笔者第一次见的时候也是大概能理解函数的用途,但是具体实现原理细节也是云里雾里,在参考了几篇博文,做过几个实验之后算是清晰了,本文在记录的同时希望给后来人一个参考,欢迎留言讨论。 先看其名,parameter,中文

    2023年04月08日
    浏览(76)
  • PyTorch中的torch.nn.Linear函数解析

    torch.nn是包含了构筑神经网络结构基本元素的包,在这个包中,可以找到任意的神经网络层。这些神经网络层都是nn.Module这个大类的子类。torch.nn.Linear就是神经网络中的线性层,可以实现形如y=Xweight^T+b的加和功能。 nn.Linear():用于设置网络中的全连接层,需要注意的是全连接

    2024年02月16日
    浏览(28)
  • 深度学习之pytorch 中 torch.nn介绍

    pytorch 中必用的包就是 torch.nn,torch.nn 中按照功能分,主要如下有几类: 1. Layers(层):包括全连接层、卷积层、池化层等。 2. Activation Functions(激活函数):包括ReLU、Sigmoid、Tanh等。 3. Loss Functions(损失函数):包括交叉熵损失、均方误差等。 4. Optimizers(优化器):包括

    2024年02月22日
    浏览(32)
  • Pytorch:torch.nn.Module.apply用法详解

    torch.nn.Module.apply 是 PyTorch 中用于递归地应用函数到模型的所有子模块的方法。它允许对模型中的每个子模块进行操作,比如初始化权重、改变参数类型等。 以下是关于 torch.nn.Module.apply 的示例: 1. 语法 Module:PyTorch 中的神经网络模块,例如 torch.nn.Module 的子类。 fn:要应用到

    2024年01月15日
    浏览(35)
  • 深入浅出Pytorch函数——torch.nn.Module

    分类目录:《深入浅出Pytorch函数》总目录 Pytorch中所有网络的基类,我们的模型也应该继承这个类。 Modules 也可以包含其它 Modules ,允许使用树结构嵌入他们,我们还可以将子模块赋值给模型属性。 语法 方法 torch.nn.Module.apply 实例 通过上面方式赋值的 submodule 会被注册,当调

    2024年02月12日
    浏览(43)
  • 深入浅出Pytorch函数——torch.nn.Softmax

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 机器学习中的数学——激活函数:Softmax函数 · 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax · 深入浅出Pytorch函数——torch.nn.Softmax 将Softmax函数应用于 n n n 维输入张量,重新缩放它们,使得 n n n 维输出张量的

    2024年02月15日
    浏览(36)
  • 深入浅出Pytorch函数——torch.nn.Linear

    分类目录:《深入浅出Pytorch函数》总目录 对输入数据做线性变换 y = x A T + b y=xA^T+b y = x A T + b 语法 参数 in_features :[ int ] 每个输入样本的大小 out_features :[ int ] 每个输出样本的大小 bias :[ bool ] 若设置为 False ,则该层不会学习偏置项目,默认值为 True 变量形状 输入变量:

    2024年02月12日
    浏览(25)
  • 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 机器学习中的数学——激活函数:Softmax函数 · 深入浅出Pytorch函数——torch.softmax/torch.nn.functional.softmax · 深入浅出Pytorch函数——torch.nn.Softmax 将Softmax函数应用于沿 dim 的所有切片,并将重新缩放它们,使元素位于 [ 0 ,

    2024年02月15日
    浏览(43)
  • 深入浅出Pytorch函数——torch.nn.init.eye_

    分类目录:《深入浅出Pytorch函数》总目录 相关文章: · 深入浅出Pytorch函数——torch.nn.init.calculate_gain · 深入浅出Pytorch函数——torch.nn.init.uniform_ · 深入浅出Pytorch函数——torch.nn.init.normal_ · 深入浅出Pytorch函数——torch.nn.init.constant_ · 深入浅出Pytorch函数——torch.nn.init.ones_ ·

    2024年02月11日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包