引言

本篇介绍Pytorch 的索引与切片

索引

1
2
3
4
5
6
7
In[3]: a = torch.rand(4,3,28,28)
In[4]: a[0].shape # 理解上相当于取第一张图片
Out[4]: torch.Size([3, 28, 28])
In[5]: a[0,0].shape # 第0张图片的第0个通道
Out[5]: torch.Size([28, 28])
In[6]: a[0,0,2,4] # 第0张图片第0个通道的第2行第4列的像素点 标量
Out[6]: tensor(0.4133) # 没有用 [] 包起来就是一个标量 dim为0

切片

  • 顾头不顾尾
1
2
3
4
5
6
7
8
9
10
In[7]: a.shape
Out[7]: torch.Size([4, 3, 28, 28])
In[8]: a[:2].shape # 前面两张图片的所有数据
Out[8]: torch.Size([2, 3, 28, 28])
In[9]: a[:2,:1,:,:].shape # 前面两张图片的第0通道的数据
Out[9]: torch.Size([2, 1, 28, 28])
In[11]: a[:2,1:,:,:].shape # 前面两张图片,第1,2通道的数据
Out[11]: torch.Size([2, 2, 28, 28])
In[10]: a[:2,-1:,:,:].shape # 前面两张图片,最后一个通道的数据 从-1到最末尾,就是它本身。
Out[10]: torch.Size([2, 1, 28, 28])

步长

  • 顾头不顾尾 + 步长
  • start : end : step
  • 对于步长为1的,通常就省略了。
1
2
3
4
a[:,:,0:28,0:28:2].shape    # 隔点采样
Out[12]: torch.Size([4, 3, 28, 14])
a[:,:,::2,::2].shape
Out[14]: torch.Size([4, 3, 14, 14])

具体的索引

  • .index_select(dim, indices)

    • dim为维度,indices是索引序号
    • 这里的indeces必须是tensor ,不能直接是一个list
1
2
3
4
5
6
7
8
9
10
In[17]: a.shape
Out[17]: torch.Size([4, 3, 28, 28])
In[19]: a.index_select(0, torch.tensor([0,2])).shape # 当前维度为0,取第0,2张图片
Out[19]: torch.Size([2, 3, 28, 28])
In[20]: a.index_select(1, torch.tensor([1,2])).shape # 当前维度为1,取第1,2个通道
Out[20]: torch.Size([4, 2, 28, 28])
In[21]: a.index_select(2,torch.arange(28)).shape # 第二个参数,只是告诉你取28行
Out[21]: torch.Size([4, 3, 28, 28])
In[22]: a.index_select(2, torch.arange(8)).shape # 取8行 [0,8)
Out[22]: torch.Size([4, 3, 8, 28])

...

  • ... 表示任意多维度,根据实际的shape来推断。
  • 当有 ... 出现时,右边的索引理解为最右边
  • 为什么会有它,没有它的话,存在这样一种情况 a[0,: ,: ,: ,: ,: ,: ,: ,: ,: ,2] 只对最后一个维度做了限度,这个向量的维度又很高,以前的方式就不太方便了。
1
2
3
4
5
6
7
8
9
10
In[23]: a.shape
Out[23]: torch.Size([4, 3, 28, 28])
In[24]: a[...].shape # 所有维度
Out[24]: torch.Size([4, 3, 28, 28])
In[25]: a[0,...].shape # 后面都有,取第0个图片 = a[0]
Out[25]: torch.Size([3, 28, 28])
In[26]: a[:,1,...].shape
Out[26]: torch.Size([4, 28, 28])
In[27]: a[...,:2].shape # 当有...出现时,右边的索引理解为最右边,只取两列
Out[27]: torch.Size([4, 3, 28, 2])

使用mask来索引

  • .masked_select()
  • 求掩码位置原来的元素大小
  • 缺点:会把数据,默认打平(flatten),
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
In[31]: x = torch.randn(3,4)
In[32]: x
Out[32]:
tensor([[ 2.0373, 0.1586, 0.1093, -0.6493],
[ 0.0466, 0.0562, -0.7088, -0.9499],
[-1.2606, 0.6300, -1.6374, -1.6495]])
In[33]: mask = x.ge(0.5) # >= 0.5 的元素的位置上为1,其余地方为0
In[34]: mask
Out[34]:
tensor([[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 1, 0, 0]], dtype=torch.uint8)
In[35]: torch.masked_select(x,mask)
Out[35]: tensor([2.0373, 0.6300]) # 之所以打平是因为大于0.5的元素个数是根据内容才能确定的
In[36]: torch.masked_select(x,mask).shape
Out[36]: torch.Size([2])

使用打平(flatten)后的序列

  • torch.take(src, torch.tensor([index]))
  • 打平后,按照index来取对应位置的元素
1
2
3
4
5
6
7
In[39]: src = torch.tensor([[4,3,5],[6,7,8]])		# 先打平成1维的,共6列
In[40]: src
Out[40]:
tensor([[4, 3, 5],
[6, 7, 8]])
In[41]: torch.take(src, torch.tensor([0, 2, 5])) # 取打平后编码,位置为0 2 5
Out[41]: tensor([4, 5, 8])

Pytorch-索引与切片的更多相关文章

  1. pytorch——不用包模拟简单线性预测,数据类型,创建tensor,索引与切片

    常见的学习种类 线性回归,最简单的y=wx+b型的,就像是调节音量大小.逻辑回归,是否问题.分类问题,是猫是狗是猪 最简单的线性回归y=wx+b 目的:给定大量的(x,y)坐标点,通过机器学习来找出最 ...

  2. numpy之索引和切片

    索引和切片 一维数组 一维数组很简单,基本和列表一致. 它们的区别在于数组切片是原始数组视图(这就意味着,如果做任何修改,原始都会跟着更改). 这也意味着,如果不想更改原始数组,我们需要进行显式的复制 ...

  3. Numpy系列(四)- 索引和切片

    Python 中原生的数组就支持使用方括号([])进行索引和切片操作,Numpy 自然不会放过这个强大的特性.  单个元素索引 1-D数组的单元素索引是人们期望的.它的工作原理与其他标准Python序 ...

  4. 金融量化分析【day110】:Pandas-DataFrame索引和切片

    一.实验文档准备 1.安装 tushare pip install tushare 2.启动ipython C:\Users\Administrator>ipython Python 3.7.0 ...

  5. Numpy学习二:数组的索引与切片

    1.一维数组索引与切片#创建一维数组arr1d = np.arange(10)print(arr1d) 结果:[0 1 2 3 4 5 6 7 8 9] #数组的索引从0开始,通过索引获取第三个元素a ...

  6. 数据类型&字符串得索引及切片

    一:数据类型 1):int     1,2,3用于计算 2):bool    ture  false  用于判断,也可做为if的条件 3):str     用引号引起来的都是str 存储少量数据,进行 ...

  7. 3.3Python数据处理篇之Numpy系列(三)---数组的索引与切片

    目录 (一)数组的索引与切片 1.说明: 2.实例: (二)多维数组的索引与切片 1.说明: 2.实例: 目录: 1.一维数组的索引与切片 2.多维数组的索引与切片 (一)数组的索引与切片 1.说明: ...

  8. NumPy学习(索引和切片,合并,分割,copy与deep copy)

    NumPy学习(索引和切片,合并,分割,copy与deep copy) 目录 索引和切片 合并 分割 copy与deep copy 索引和切片 通过索引和切片可以访问以及修改数组元素的值 一维数组 程 ...

  9. 编码,基本数据类型,str索引和切片,for循环

    1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 2**8 8位 ...

  10. Numpy:索引与切片

    numpy基本的索引和切片 import numpy as np arr = np.array([1,2,3,555,666,888,10]) arr array([ 1, 2, 3, 555, 66 ...

随机推荐

  1. hive-staging文件产生的原因和解决方案

    通过spark-sql.hive-sql.hue等提交select或者insert overwrite等sql到hive时,会产生该目录,用于临时存放执行结果,比如insert overwrite会将 ...

  2. HH的项链 HYSBZ - 1878 (莫队/ 树状数组)

    HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的项链变得越来越长.有一天,他突然 ...

  3. hive三种调用方式

    一.hive -e ‘sql语句’ (shell命令) 适合比较短的sql语句调用,优点是可以直接在shell中调用静音模式 -S 在执行HiveQL过程中,不在显示器输出MR的执行过程hive -S ...

  4. maven项目pom.xml中parent标签的使用(转)

    原文地址:https://blog.csdn.net/qq_41254677/article/details/81011681 使用maven是为了更好的帮项目管理包依赖,maven的核心就是pom. ...

  5. 【leetcode】1276. Number of Burgers with No Waste of Ingredients

    题目如下: Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as ...

  6. [Python自学] day-16 (JS、作用域、DOM、事件)

    一.JS中的三种函数 1.普通函数 function func(){ console.log("Hello World"); } func() 2.匿名函数 setInterval ...

  7. php文件夹上传源码

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  8. 未AC

    Count the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Luogu P4139 上帝与集合的正确用法

    题目链接:Click here Solution: 这道题就考你会不会扩展欧拉定理,根据扩展欧拉定理可知 \[ a^b \equiv a^{(b\,mod\,\varphi(p))+\varphi(p ...

  10. 用Python操作excel文档

    使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...