作用:调取数据中某一个或者某一块想取的数据

(1)常规索引方式(即a[ ][ ][ ]的方式)

In [16]: a = tf.ones([3,28,28,3])In [17]: a[0][5][7]
Out[17]: <tf.Tensor: id=50, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

(2)Numpy风格的索引

In [16]: a = tf.ones([3,28,28,3])In [18]: a[1,8,9,2]
Out[18]: <tf.Tensor: id=54, shape=(), dtype=float32, numpy=1.0>In [20]: a[2,5,9]
Out[20]: <tf.Tensor: id=62, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

(3)使用start:end

In [22]: a = tf.ones([3,28,28,3])In [23]: a.shape
Out[23]: TensorShape([3, 28, 28, 3])In [24]: a[0:2,:,:,:].shape
Out[24]: TensorShape([2, 28, 28, 3])In [25]: a[0:2,1:,:-1,:].shape
Out[25]: TensorShape([2, 27, 27, 3])In [26]: a[0:2,:2,-3:,:].shape
Out[26]: TensorShape([2, 2, 3, 3])

单一个“:”表示这一轴全取;

“0:2”表示取该轴下,序号为0,1的数据(不包含序号为2);

“1:”表示取该轴下,序号为1至最后;

“:-1”表示取该轴下,最开始到序号为-1(序号-1即该轴下最后一位,依次向前分别为-2,-3,....),但不取序号为-1这一位;

“:2”表示取该轴下,最开头到序号为2,但不包含序号为2。和“0:2”效果一样;

“-3:”表示取该轴下,序号为-3到最后。

(4)使用start:end:step

In [28]: a = tf.ones([3,28,28,3])In [29]: a[:,0:28:2,::2,:].shape
Out[29]: TensorShape([3, 14, 14, 3])In [39]: a[:,::-2,::-1,:].shape
Out[39]: TensorShape([3, 14, 28, 3])In [40]: a[:,0:28:-2,0::-2,:].shape
Out[40]: TensorShape([3, 0, 1, 3])

“0:28:2”表示对该轴下序号0至序号28进行间隔采样,每隔2采一次,也就是采序号为0,2,4,6,...;

"::2"与“0:28:2”效果一样;

“::-2”表示逆向隔行采集,采集方向从后向前,当step为负数时,前面不能有数字,比如:“0:28:-2”、“0::-2”。

(5)使用...

In [42]: a = tf.ones([3,28,28,3])In [43]: a[0,:,:,:].shape
Out[43]: TensorShape([28, 28, 3])In [44]: a[0,...].shape
Out[44]: TensorShape([28, 28, 3])In [45]: a[0,...,2].shape
Out[45]: TensorShape([28, 28])In [47]: a[...,2,2].shape
Out[47]: TensorShape([3, 28])

a[0,:,:,:].shape与a[0,...].shape效果一致,即"..."可以省略一些内容。

(6)使用tf.gather

In [3]: a = tf.random.normal([4,35,8])In [4]: tf.gather(a,axis=0,indices=[2,3]).shape
Out[4]: TensorShape([2, 35, 8])In [5]: tf.gather(a,axis=0,indices=[2,3,1,0]).shape
Out[5]: TensorShape([4, 35, 8])

该函数参数axis表示选择的轴 ,indices即序号。上述表示选择轴0中序号为2,3的数据。且采集顺序依照indices的顺序。

(7)使用tf.gather_nd

In [3]: a = tf.random.normal([4,35,8])In [7]: tf.gather_nd(a,[0,1]).shape
Out[7]: TensorShape([8])In [9]: tf.gather_nd(a,[[0,1],[1,1]]).shape
Out[9]: TensorShape([2, 8])In [10]: tf.gather_nd(a,[[0,1,1],[1,1,1],[1,2,5]]).shape
Out[10]: TensorShape([3])

 (8) 使用tf.boolean_mask

In [2]: a = tf.random.normal([4,28,28,3])In [5]: tf.boolean_mask(a,mask=[True,False,False,False]).shape
Out[5]: TensorShape([1, 28, 28, 3])In [6]: tf.boolean_mask(a,mask=[True,False,False],axis=3).shape
Out[6]: TensorShape([4, 28, 28, 1])

axis同样代表轴,对应于mask中True即为选中,False即不选。需注意的是mask的个数需与axis的轴的数相等。

Tensor的索引与切片相关推荐

  1. Pytorch中Tensor的索引,切片以及花式索引(fancy indexing)

    目录 理解Tensor的dim 索引 简单索引 用1维的list,numpy,tensor索引 用booltensor索引 切片 花式索引 结语 前一段时间遇到一个花式索引的问题,在搜索良久之后没有找 ...

  2. Pytorch——如何创建一个tensor与索引和切片(一)

    创建Tensor numpy是一个非常常见的数据的一个载体,数据可以先从numpy中间先导进tensor来: 1.从numpy引入 import from numpy a=np.array([2,3. ...

  3. Pytorch——如何创建一个tensor与索引和切片(二)

    1.两种常见的随机初始化 (1) rand函数 rander函数就是随机的使用0和1的均值分布来初始化,也就是说它从零和一的空间中随机的均匀的sample出来,这样数据就回均匀的分布在0和1之间. t ...

  4. tensorflow tensor 张量 部分采样 切片和索引

    文章目录 视频 索引和切片 索引 一维张量切片 二维张量切片 三维.四维张量切片 gether函数 gather_en函数 视频 https://www.bilibili.com/video/BV1j ...

  5. 【TensorFlow2.0】(3) 索引与切片操作

    各位同学好,今天我和大家分享一下TensorFlow2.0中索引与切片.内容有: (1) 给定每一维度的索引来获取数据:(2) 切片索引:(3) 省略号应用:(4) tf.gather() 方法:(5 ...

  6. tensorflow中tensor的索引

    tensorflow中tensor的索引 1.print(sess.run(outputs[0:2,0:2,:])) 2.print(sess.run(tf.slice(outputs,[0,0,0] ...

  7. 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 ...

  8. 深度学习(7)TensorFlow基础操作三: 索引与切片

    深度学习(7)TensorFlow基础操作三: 索引与切片 一. 基础索引 1. Basic indexing 2. Numpy-style indexing 3. start : end 4. 切片 ...

  9. 【TensorFlow】——索引与切片

    目录 1.利用index进行索引 2.利用":"和"..."进行索引与切片 3.tf.gather()--对一个维度进行乱序索引 优势: 缺点: 例子 4.tf ...

最新文章

  1. jenkins部署web项目
  2. Android教程 -09 数据的持久化存储
  3. 【感想文】感情经历,是否给你我带来的些许提升?我想,有。
  4. tableau三轴合并_《Tableau数据可视化实战》——1.12节合并不同数据源-阿里云开发者社区...
  5. uva 12589——Learning Vector
  6. 2Python全栈之路系列之MysQl基本数据类型
  7. DataList和Repeater分页
  8. vue学习-声明周期和实例
  9. 华为双前置摄像头_双录—华为手机前置摄像头双录画质提升办法
  10. 深入理解JVM专题目录
  11. PR菜鸟教程:如何剪切掉其中不需要的片段
  12. clone远程代码 在不同电脑上git_Git 同一电脑配置多个远程仓库
  13. 支付宝小程序访问浙里办应用,提示页面访问受限,IOS无法访问
  14. 美国男人欢迎中国的丑女人?------------说说洁
  15. 微服务商城系统(十四)微信支付
  16. 笑破肚皮!考驾照现场的爆笑故事!
  17. 发布订单平台小程序开发制作
  18. python我的所得税计算器_python实现计算器简易版
  19. 弈剑听雨阁战场克敌生存最新1.0版
  20. SQL2005 使用证书实现数据库镜像

热门文章

  1. 数字化方法基础(三)_导入本地模型
  2. 数据结构笔记(三十)-- 查找的基本概念和相关的顺序查找
  3. 地图不显示_图灵搜不显示地图,软件打开一片空白,怎么解决?
  4. github pages markdown_赏金$25000的GitHub漏洞:通过 GitHub Pages 不安全的Kramdown配置实现多个RCE...
  5. java map 集合实例_Java之集合类【HashMap】【入门版,实例解析】
  6. svn关键字替换_SVN关键字替换示例
  7. android 静态_Google静态地图Android
  8. c语言如何将8个字符串串联_C ++中的字符串串联:串联字符串的4种方法
  9. Java ResultSet教程
  10. 具有Eclipse和嵌入式JBoss HornetQ Server的简单JMS 1.1生产者和使用者示例