一、view/reshape

In [1]: import torchIn [2]: a = torch.rand(4,1,28,28)In [3]: a.shape
Out[3]: torch.Size([4, 1, 28, 28])In [4]: a.view(4,28*28)
Out[4]:
tensor([[0.3060, 0.2680, 0.3763,  ..., 0.6596, 0.5645, 0.8772],[0.7751, 0.9969, 0.0864,  ..., 0.7230, 0.4374, 0.1659],[0.5146, 0.5350, 0.1214,  ..., 0.2056, 0.2646, 0.8539],[0.5737, 0.5637, 0.2420,  ..., 0.7731, 0.6198, 0.6113]])In [5]: a.view(4,28*28).shape
Out[5]: torch.Size([4, 784])In [6]: a.view(4*28,28).shape
Out[6]: torch.Size([112, 28])In [7]: a.view(4*1,28,28).shape
Out[7]: torch.Size([4, 28, 28])In [8]: b = a.view(4,784)

二、squeeze/unsqueeze
unsqueeze增加维度

In [12]: a.unsqueeze(0).shape
Out[12]: torch.Size([1, 4, 1, 28, 28])In [13]: a.shape
Out[13]: torch.Size([4, 1, 28, 28])In [14]: a.unsqueeze(0).shape
Out[14]: torch.Size([1, 4, 1, 28, 28])In [15]: a.unsqueeze(-1).shape
Out[15]: torch.Size([4, 1, 28, 28, 1])In [16]: a.unsqueeze(4).shape
Out[16]: torch.Size([4, 1, 28, 28, 1])In [17]: a.unsqueeze(-4).shape
Out[17]: torch.Size([4, 1, 1, 28, 28])In [18]: a.unsqueeze(-5).shape
Out[18]: torch.Size([1, 4, 1, 28, 28])In [19]: a.unsqueeze(5).shape
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-19-b54eab361a50> in <module>
----> 1 a.unsqueeze(5).shapeRuntimeError: Dimension out of range (expected to be in range of [-5, 4], but got 5)
In [20]: a = torch.tensor([1.2,2.3])In [21]: a.unsqueeze(-1).shape
Out[21]: torch.Size([2, 1])In [22]: a.shape
Out[22]: torch.Size([2])In [23]: a.unsqueeze(-1)
Out[23]:
tensor([[1.2000],[2.3000]])In [24]: a
Out[24]: tensor([1.2000, 2.3000])In [25]: a.unsqueeze(0)
Out[25]: tensor([[1.2000, 2.3000]])In [26]: a.unsqueeze(0).shape
Out[26]: torch.Size([1, 2])

squeeze压缩维度

In [27]: b = torch.rand(1,32,1,1)In [28]: b.shape
Out[28]: torch.Size([1, 32, 1, 1])In [29]: b.squeeze().shape
Out[29]: torch.Size([32])In [30]: b.squeeze(0).shape
Out[30]: torch.Size([32, 1, 1])In [31]: b.squeeze(-1).shape
Out[31]: torch.Size([1, 32, 1])In [32]: b.squeeze(1).shape
Out[32]: torch.Size([1, 32, 1, 1])In [33]: b.squeeze(-1).shape
Out[33]: torch.Size([1, 32, 1])In [34]: b.squeeze(-4).shape
Out[34]: torch.Size([32, 1, 1])

三、transpose/t/permute
transpose一次只能完成数据内部的两两交换;
permute可以完成数据内部的任意交换

In [15]: a.shape
Out[15]: torch.Size([4, 3, 32, 32])In [16]: a1 = a.transpose(1,3).view(4,3*32*32).view(4,3,32,32)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-262e84a7fdcb> in <module>
----> 1 a1 = a.transpose(1,3).view(4,3*32*32).view(4,3,32,32)RuntimeError: invalid argument 2: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Call .contiguous() before .view(). at c:\a\w\1\s\windows\pytorch\aten\src\th\generic/THTensor.cpp:213In [17]: a1 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,3,32,32)In [18]: a2 = a.transpose(1,3).contiguous().view(4,3*32*32).view(4,32,32,3).transpose(1,3)In [19]: a1.shape,a2.shape
Out[19]: (torch.Size([4, 3, 32, 32]), torch.Size([4, 3, 32, 32]))In [20]: torch.all(torch.eq(a,a1))
Out[20]: tensor(0, dtype=torch.uint8)In [21]: torch.all(torch.eq(a,a2))
Out[21]: tensor(1, dtype=torch.uint8)
In [22]: a = torch.rand(4,3,28,28)In [23]: a.transpose(1,3).shape
Out[23]: torch.Size([4, 28, 28, 3])In [24]: b = torch.rand(4,3,28,32)In [25]: b.transpose(1,3).shape
Out[25]: torch.Size([4, 32, 28, 3])In [26]: b.transpose(1,3).transpose(1,2).shape
Out[26]: torch.Size([4, 28, 32, 3])In [27]: b.permute(0,2,3,1).shape
Out[27]: torch.Size([4, 28, 32, 3])

四、expand/repeat
expand维度扩展在有需要的时候增加数据,不增加数据时只是改变数据的理解方式。
repeat增加了数据

In [35]: a = torch.rand(4,32,14,14)In [36]: b = torch.rand(1,32,1,1)In [37]: a.shape
Out[37]: torch.Size([4, 32, 14, 14])In [38]: b.shape
Out[38]: torch.Size([1, 32, 1, 1])In [39]: b.expand(4,32,14,14).shape#维度扩展,复制了数据
Out[39]: torch.Size([4, 32, 14, 14])In [40]: b.expand(-1,32,-1,-1).shape#维度扩展,未增加数据
Out[40]: torch.Size([1, 32, 1, 1])In [41]: b.expand(-1,32,-1,-4).shape#bug
Out[41]: torch.Size([1, 32, 1, -4])
In [42]: b.shape
Out[42]: torch.Size([1, 32, 1, 1])In [43]: b.repeat(4,1,1,1).shape#dim0复制4次
Out[43]: torch.Size([4, 32, 1, 1])In [44]: b.repeat(4,32,1,1).shape#dim0复制4次,dim1复制32次
Out[44]: torch.Size([4, 1024, 1, 1])In [45]: b.repeat(4,1,32,32).shape#dim0复制4次,dim2复制32次,dim3复制32次
Out[45]: torch.Size([4, 32, 32, 32])

pytorch维度变换相关推荐

  1. pytorch reshape_pytorch常用总结 之 tensor维度变换

    小白最近刚开始使用pytorch,经常需要在各个网站反复查询一些函数的区别.但是不同的博客说的有时候不一样,趁着实验室停电,小白写了这篇文章,方便自己食用,不定期更新.如有错误,欢迎大家热烈指正. 第 ...

  2. pytorch空间变换网络

    pytorch空间变换网络 本文将学习如何使用称为空间变换器网络的视觉注意机制来扩充网络.可以在DeepMind paper 有关空间变换器网络的内容. 空间变换器网络是对任何空间变换的差异化关注的概 ...

  3. 【TensorFlow2.0】(4) 维度变换、广播

    各位同学好,今天我和大家分享一下TensorFlow2.0中有关数学计算的相关操作,主要内容有: (1) 改变维度:reshape():(2) 维度转置:transpose():(3) 增加维度:ex ...

  4. CRNN维度变换的解释这样你也可以自定义CRNN了

    # x=torch.rand([1, 3, 256, 256])# 首先要知道lstm是必须输入三维度的# nn.LSTM(256,128) 256 是输入维度[任意维度,任意维度,256] 输出是1 ...

  5. 深度学习(8)TensorFlow基础操作四: 维度变换

    深度学习(8)TensorFlow基础操作四: 维度变换 1. View 2. 示例 3. Reshape操作可能会导致潜在的bug 4. tf.transpose 5. Squeeze VS Exp ...

  6. python维度变换_Python NumPy用法

    介绍 NumPy是Python数值计算最重要的基础包,大多数提供科学计算的包都是用NumPy的数组作为构建基础.NumPy本身并没有提供多么高级的数据分析功能,理解NumPy数组以及面向数组的计算,将 ...

  7. tensor 增加维度_tensor维度变换

    维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一. 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念.所谓Vi ...

  8. TensorFlow张量的维度变换

    在神经网络运算过程中,维度变换是最核心的张量操作,通过维度变换可以将数据任意地切换形式,满足不同场合的运算需求. 维度变换的一个例子: Y = X@W + b X 的 shape 为[2,4] W 的 ...

  9. TensorFlow——维度变换与Broadcasting

    TensorFlow 维度变换 文章目录 TensorFlow 维度变换 一.Reshape 二.tf.transpose 三.Squeeze和Expand_dims Broadcasting 前言 ...

最新文章

  1. echarts中graphic_使用Pyecharts进行奥运会可视化分析!
  2. android n进入分屏代码分析_智慧分屏为“何物”?华为MatePad Pro解锁便捷又高效的新姿势...
  3. CSS三种写法的优先级
  4. SQL COUNT() 函数
  5. codeforces 1027 B - Numbers on the Chessboard(规律)
  6. OpenCV+python实现视频文件读写
  7. GetWindowLong和SetWindowLong函数
  8. Android之项目中如何用好构建神器Gradle?
  9. rsync同步服务部署
  10. 免费查题合集大推荐,付费根本不存在的!
  11. node2vec 包安装
  12. Kettle下载安装教程
  13. 知了课堂python_Python框架Flask系列课程(2)—全栈开发[知了课堂]
  14. 使用PYQT5打开海康威视工业相机并获取图像进行显示
  15. c语言 步进电机 程序,步进电机加速启动C语言程序
  16. Expected Array got Object
  17. 工作经验分享|你在工作中应该注意什么?
  18. 原生滑动选择器 html,html选择器
  19. 微信发红包测试用例设计
  20. PMSG类型究竟是什么意思?

热门文章

  1. Chrome之控制台使用【转载】
  2. 为什么有些人看了别人的总结、经验、教训,依然没有用。
  3. 为何高于四次的方程没有根式解?
  4. MVC中使用KindEditor
  5. react+typescript报错集锦持续更新
  6. pytorch 中的 split
  7. Tornado快速入门
  8. Spring使用ComponentScan扫描Maven多模块工程的其它模块
  9. CoreData 从入门到精通(三)关联表的创建
  10. 第二阶段第五次站立会议