**

一 tf.gather( ) 函数

**
–tf.gather(params, indices, validate_indices=None, name=None, axis=0)
params:待切片的参数
indices:取出数据所在的位置
axis:指定切片数据所在的维度
tf.gather(a,axis=0,indices=[2,3]): 对参数a进行切片, 对维度0进行操作,将维度0上面的数据切出2,3的数据.

In [1]: import tensorflow as tf                                                 In [2]: a = tf.random.normal([4,35,8],mean=1,stddev=1)                          In [3]: a.shape
Out[3]: TensorShape([4, 35, 8])In [4]: tf.gather(a,axis=0,indices=[2,3]).shape
Out[4]: TensorShape([2, 35, 8])In [5]: a[2:4].shape
Out[5]: TensorShape([2, 35, 8])In [6]: tf.gather(a,axis=0,indices=[2,1,3,0]).shape
Out[6]: TensorShape([4, 35, 8])In [7]: tf.gather(a,axis=1,indices=[2,3,7,9,16]).shape
Out[7]: TensorShape([4, 5, 8])In [8]: tf.gather(a,axis=2,indices = [2,3,7]).shape
Out[8]: TensorShape([4, 35, 3])

利用tf.gather( ) 函数进行随机打散

In [1]: import tensorflow as tf                                                 In [2]: idx = tf.range(100) #索引                                                    In [3]: idx
Out[3]:
<tf.Tensor: id=3, shape=(100,), dtype=int32, numpy=
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99],dtype=int32)>In [4]: idx = tf.random.shuffle(idx)#对索引进行随机打散                                            In [5]: idx
Out[5]:
<tf.Tensor: id=5, shape=(100,), dtype=int32, numpy=
array([11, 58, 72, 35, 86, 54,  2, 85, 88, 48, 23, 17, 46, 33, 79, 32, 62,70, 28, 13, 99, 83, 20, 37, 29, 61, 16, 98, 52, 63, 55, 97, 31, 44,92, 89, 94, 47, 76, 73, 67,  7, 95, 87,  1, 42, 38,  5, 10, 64, 26,51, 56, 40, 65, 59, 39, 90, 24, 27, 25, 21, 75, 84, 77,  4, 30, 34,50, 69, 57, 22, 71, 15, 49, 43,  6, 81, 66, 80, 82, 45, 96,  3, 36,78, 91, 68, 41,  8, 14, 74,  0, 18,  9, 19, 60, 93, 53, 12],dtype=int32)>In [6]: a = tf.random.uniform([100,64,64,3],minval=0,maxval=100,dtype=tf.int32)#随机生成100张图片数组 In [7]: y = tf.range(100)                                                       In [8]: y = tf.one_hot(y,depth=100)#标签y                                             In [9]: y
Out[9]:
<tf.Tensor: id=18, shape=(100, 100), dtype=float32, numpy=
array([[1., 0., 0., ..., 0., 0., 0.],[0., 1., 0., ..., 0., 0., 0.],[0., 0., 1., ..., 0., 0., 0.],...,[0., 0., 0., ..., 1., 0., 0.],[0., 0., 0., ..., 0., 1., 0.],[0., 0., 0., ..., 0., 0., 1.]], dtype=float32)>In [11]: a = tf.gather(a,indices=idx,axis=0)#对数据利用索引进行打散                                    In [12]: y = tf.gather(y,indices=idx,axis=0)#对输出利用索引进行打散                                    In [13]: y
Out[13]:
<tf.Tensor: id=23, shape=(100, 100), dtype=float32, numpy=
array([[0., 0., 0., ..., 0., 0., 0.],[0., 0., 0., ..., 0., 0., 0.],[0., 0., 0., ..., 0., 0., 0.],...,[0., 0., 0., ..., 0., 0., 0.],[0., 0., 0., ..., 0., 0., 0.],[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)>

**

二 tf.gather_nd( )函数

**
在多个维度上进行索引

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

**

三 tf.boolean_mask( )函数

**

In [1]: import os                                                               In [2]: import tensorflow as tf                                                 In [3]: os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2'                                In [4]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       In [5]: a.shape
Out[5]: TensorShape([4, 28, 28, 3])In [6]: tf.boolean_mask(a,mask=[True,True,False,False],axis=0).shape            Out[6]: TensorShape([2, 28, 28, 3])In [7]: tf.boolean_mask(a,mask=[True,True,False],axis=3).shape
Out[7]: TensorShape([4, 28, 28, 2])In [8]: a = tf.ones([2,3,4])                                                    In [9]: a.shape
Out[9]: TensorShape([2, 3, 4])In [10]: tf.boolean_mask(a,mask = [[True,False,False],[False,True,True]])
Out[10]:
<tf.Tensor: id=92, shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],[1., 1., 1., 1.],[1., 1., 1., 1.]], dtype=float32)>

TensorFlow2.0:索引和切片(2)相关推荐

  1. TensorFlow2.0:索引和切片(1)

    ** 一 基本的索引方式 ** 给定每个维度的索引,直接进行索引 In [1]: import tensorflow as tf In [2]: import numpy as np In [3]: ...

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

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

  3. mybatis-plus对datetime返回去掉.0_华为AI认证-TensorFlow2.0编程基础

    参考<HCIA-AI2.0培训教材><HCIA-AI2.0实验手册> 认证要求: 了解TensorFlow2.0是什么以及其特点 掌握TensorFlow2.0基础和高阶操作方 ...

  4. TensorFlow2.0学习

    文章目录 一.TensorFlow的建模流程 1.1 结构化数据建模流程范例 1.1.1 准备数据 1.1.2 定义模型 1.1.3 训练模型 1.1.4 评估模型 1.1.5 使用模型 1.1.6 ...

  5. 神经网络与深度学习理论,tensorflow2.0教程,cnn

    *免责声明: 1\此方法仅提供参考 2\搬了其他博主的操作方法,以贴上路径. 3* 场景一:神经网络与深度学习理论 场景二:tensorflow的安装 场景三:numpy包介绍 场景四:机器学习基础 ...

  6. ​TensorFlow2.0系列教程集合版(附PDF下载)

    文章来源于机器学习算法与Python实战,作者奥辰 TensorFlow2.0(1):基本数据结构--张量 TensorFlow2.0(2):数学运算 TensorFlow2.0(3):张量排序.最大 ...

  7. 【TensorFlow2.0】(7) 张量排序、填充、复制、限幅、坐标选择

    各位同学好,今天和大家分享一下TensorFlow2.0中的一些操作.内容有: (1)排序 tf.sort().tf.argsort().top_k():(2)填充 tf.pad():(3)复制 tf ...

  8. 【TensorFlow2.0】(6) 数据统计,范数、最值、求和、均值、最值位置、唯一值、张量比较

    各位同学好,今天和大家分享一下TensorFlow2.0中的数据分析操作.内容有: (1)范数 tf.norm():(2)最值 tf.reduce_min(), tf.reduce_max()(3)求 ...

  9. Numpy入门教程:02. 索引、切片与迭代

    背景 什么是 NumPy 呢? NumPy 这个词来源于两个单词 – Numerical和Python.其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景: 执 ...

最新文章

  1. H3C 交换机命名规则
  2. vue中弹窗input框聚焦_Vue 中如何让 input 聚焦?(包含视频讲解)
  3. 运行一个Hadoop Job所需要指定的属性
  4. Extjs 之 initComponent 和 constructor的区别(转)
  5. office另存为pdf的加载项_pdf怎么转换成word?打工人必备的丛林法则
  6. 解决新安装的Ubuntu18.04没有网络连接的问题
  7. x64位windows上程序开发的注意事项
  8. 使用DevKit开发插件
  9. 如何删除我的计算机里的搜索记录,计算机里搜索栏的历史记录怎么删除?
  10. 詹克团为“夺权”动作频频 引发比特大陆员工不满
  11. android 下拉状态栏(SystemUI)常见修改记录
  12. php interface类,类相关的关键字 - interface
  13. 七天搞定Node.js微信公众号
  14. 导出RK3288开发板上的根文件系统,并打包img
  15. 《美通社头条》祝大家国庆节快乐!
  16. 【基础篇】MySQL系列之where条件查询
  17. Bootstrap 组件 Button 按钮
  18. Stata画图——散点图与折线图
  19. iMeta视频教程 | StrainPanDA分析宏基因组共存菌株的组成和基因成分谱
  20. 微信小程序项目实例——记账本

热门文章

  1. 向Array中添加二分插入排序
  2. C# Winform程序本地化应用
  3. [BZOJ2958]序列染色
  4. Mongodb常规操作【一】
  5. unity---------------------关于BuildAssetBundles的使用(打包)
  6. POJ 3262 Protecting the Flowers 贪心(性价比)
  7. 开发一款浏览器内核需要学习哪些方面的知识?
  8. PHP如何实现网址伪静态
  9. ShaderLab学习总结
  10. 搭建顶级域名下的个人博客网站