pytorch的索引与切片
**
一、从左边开始索引
**
In [12]: a = torch.rand(4,3,28,28)In [13]: a.shape
Out[13]: torch.Size([4, 3, 28, 28])In [14]: a[0].shape
Out[14]: torch.Size([3, 28, 28])In [15]: a[0,0].shape
Out[15]: torch.Size([28, 28])In [16]: a[0,0,2,4]
Out[16]: tensor(0.4559)
**
二、选前或后的数据
**
In [17]: a.shape
Out[17]: torch.Size([4, 3, 28, 28])In [18]: a[:2].shape#dim 0索引0,1
Out[18]: torch.Size([2, 3, 28, 28])In [19]: a[:2,:1,:,:].shape#dim0索引0,1 dim1索引0
Out[19]: torch.Size([2, 1, 28, 28])In [20]: a[:2,1:,:,:].shape#dim0索引0,1 dim1索引1到last
Out[20]: torch.Size([2, 2, 28, 28])In [21]: a[:2,-1:,:,:].shape#dim0索引0,1 dim1索引-1到last
Out[21]: torch.Size([2, 1, 28, 28])
**
三、步进索引
**
start?step包含start,不包含end,步进为step
In [22]: a[:,:,::2,::2].shape#dim2步进2 dim3步进2
Out[22]: torch.Size([4, 3, 14, 14])In [23]: a[:,:,0:28:2,0:28:2].shape#dim2步进2 dim3步进2
Out[23]: torch.Size([4, 3, 14, 14])
**
四、具体索引
**
a.index_select(0,torch.tensor([0,2]))第一个参数为对a的哪个维度进行操作,第二个参数是选取特定的索引
In [24]: a.shape
Out[24]: torch.Size([4, 3, 28, 28])In [25]: a.index_select(0,torch.tensor([0,2])).shape#选取0维度的0,2
Out[25]: torch.Size([2, 3, 28, 28])In [26]: a.index_select(1,torch.tensor([0,2])).shape#选取1维度的0,2
Out[26]: torch.Size([4, 2, 28, 28])In [27]: a.index_select(2,torch.arange(28)).shape#选取2维度的前28
Out[27]: torch.Size([4, 3, 28, 28])In [28]: a.index_select(2,torch.arange(8)).shape#选取2维度的前8
Out[28]: torch.Size([4, 3, 8, 28])
In [29]: a.shape
Out[29]: torch.Size([4, 3, 28, 28])In [30]: a[...].shape
Out[30]: torch.Size([4, 3, 28, 28])In [31]: a[0,...].shape
Out[31]: torch.Size([3, 28, 28])In [32]: a[:,1,...].shape
Out[32]: torch.Size([4, 28, 28])In [33]: a[...,:2].shape
Out[33]: torch.Size([4, 3, 28, 2])
**
五、掩码切片
**
In [34]: x = torch.randn(3,4)In [35]: x
Out[35]:
tensor([[ 0.2171, -0.4370, -0.9547, -0.1153],[ 0.1009, 1.4227, 0.9283, -0.4541],[-0.8437, -0.7954, -1.8106, -1.8765]])In [36]: mask = x.ge(0.5)#x中大于0.5的掩码In [37]: mask
Out[37]:
tensor([[0, 0, 0, 0],[0, 1, 1, 0],[0, 0, 0, 0]], dtype=torch.uint8)In [39]: torch.masked_select(x,mask)#掩码切片
Out[39]: tensor([1.4227, 0.9283])In [40]: torch.masked_select(x,mask).shape
Out[40]: torch.Size([2])
**
六、打平索引
**
In [41]: src = torch.tensor([[4,3,5],[6,7,8]])In [42]: torch.take(src,torch.tensor([0,2,5]))#先打平,再按打平索引
Out[42]: tensor([4, 5, 8])
pytorch的索引与切片相关推荐
- Pytorch中Tensor的索引,切片以及花式索引(fancy indexing)
目录 理解Tensor的dim 索引 简单索引 用1维的list,numpy,tensor索引 用booltensor索引 切片 花式索引 结语 前一段时间遇到一个花式索引的问题,在搜索良久之后没有找 ...
- 08_索引与切片,Indexing,Python风格的索引,index_select()选择特定索引,使用...索引任意多的维度,使用mask索引,take索引
1.8.索引与切片 1.8.1.Indexing 1.8.2.Python风格的索引 1.8.3.index_select()选择特定索引 1.8.4.使用-索引任意多的维度 1.8.5.使用mask ...
- 【TensorFlow2.0】(3) 索引与切片操作
各位同学好,今天我和大家分享一下TensorFlow2.0中索引与切片.内容有: (1) 给定每一维度的索引来获取数据:(2) 切片索引:(3) 省略号应用:(4) tf.gather() 方法:(5 ...
- Numpy入门教程:02. 索引、切片与迭代
背景 什么是 NumPy 呢? NumPy 这个词来源于两个单词 – Numerical和Python.其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景: 执 ...
- python 字符串,字符串运算,比较,索引,切片等
一: 字符串: str 作用:用来记录文本信息, 字面值表示方法:用引号括起来的部分都是字符串. '' 单引号 "" 双引号 ''' 三引号 """ ...
- dataframe两个表合并_Part25:Pandas基础(Series,DataFrame类的创建、索引、切片、算术方法)...
一.为什么学习pandas numpy已经可以帮助我们进行数据的处理了,那么学习pandas的目的是什么呢? numpy能够帮助我们处理的是数值型的数据,当然在数据分析中除了数值型的数据还有好多其他类 ...
- Python 科学计算库 Numpy (二) —— 索引及切片
目录 1. 索引及切片 (1)通过下标以及内置函数进行索引切片 (2)使用冒号分隔参数进行切片索引 (3)对部分元素进行索引并切片 (4)对多维数组进行索引切片 2. 高级索引 (1)整数数组索引 ( ...
- 【Python】掌握Python中的索引和切片
作者 | Giorgos Myrianthous 编译 | VK 来源 | Towards Data Science 在Python中,像字符串或列表这样的有序序列的元素可以通过它们的索引单独访问.这 ...
- Pandas的学习(2.Series的索引和切片、基本概念以及Series的运算)
Series的索引和切片 可以取中括号取单个索引(此时返回的是元素类型),或者中括号里一个列表取多个索引(此时返回的仍然是一个Series类型) 分为显示索引和隐式索引: (1) 显示索引 (必须给索 ...
最新文章
- 避不开的算法,如何吃透?
- python 生成器笔记
- UrlEncode编码算法
- jenkins~集群分发功能和职责处理
- 听说麦当劳,买一个雪糕就送一个男友!
- OpenCV学习笔记:矩阵的掩码操作
- 睡觉时,禁带6种东西,最后一点最严重,可能致命
- C语言预处理#pragma
- azw3转换为pdf_怎么合并几个PDF为一个?快用这个PDF转换器!
- python correlation_相关性系数介绍+python代码实现 correlation analysis
- 英语词根与说文解字词典读书笔记,并总结输出思维导图
- win10系统中如何查看wifi密码
- C#利用QRCode动态生成自定义二维码图片
- 服务器总出现异常?几个小方法助你防范于未然
- qq邮箱html源码,qq邮箱源码
- VPS与云主机指南:了解五个主要区别
- 回顾小米公司的成功过程,用五个层次的问题阐述,小米的成功基础、小米的爆品特点、小米生态链模式的根本原因。
- GDUT 2.25 D
- QML地图简介(1)
- 我们的23种设计模式(四)