**

一 基本的索引方式

**
给定每个维度的索引,直接进行索引

In [1]: import tensorflow as tf                                                 In [2]: import numpy as np                                                      In [3]: a = tf.ones([1,5,5,3])                                                  In [4]: a[0][0]
Out[4]:
<tf.Tensor: id=10, shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=float32)>In [5]: a[0][0][0]
Out[5]: <tf.Tensor: id=23, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>In [6]: a[0][0][0][2]
Out[6]: <tf.Tensor: id=40, shape=(), dtype=float32, numpy=1.0>

**

二 Numpy风格索引

**
采用 [1,2,3] 的形式进行索引,较 [1][2][3] 更加简洁,代码的可读性强

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

**

三 start:end形式的切片操作

**

--start : end为含头不含尾的切片操作,表示从start位开始,一直到end-1位结束.
--[-1:]从-1位(即最后一位开始)一直到最后一位,就是最后一位
--[-2:]从-2位(即倒数第二位开始)一直到最后一位,就是最后两位
--[2:10]从第2位开始(默认为0位开始)一直到第9位结束
--[:2]从开头一直到第1位结束,即开头前两位
--[:-1]从开头一直到末尾,但未包含末尾最后一位
In [12]: a = tf.range(10)                                                       In [13]: a
Out[13]: <tf.Tensor: id=67, shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>In [14]: a[-1:]#start:end = -1:末尾 = 最后一位
Out[14]: <tf.Tensor: id=72, shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>In [15]: a[-2:]#start:end = -2:末尾 = 最后两位
Out[15]: <tf.Tensor: id=77, shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>In [16]: a[:2]#start:end = 开头:2 = 前面两位
Out[16]: <tf.Tensor: id=82, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>In [17]: a[:-1]#start:end = 开头:-1 = 除最后一位的所有
Out[17]: <tf.Tensor: id=87, shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)>
In [1]: import tensorflow as tf                                                 In [2]: import numpy as np                                                      In [3]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       In [4]: a.shape
Out[4]: TensorShape([4, 28, 28, 3])In [5]: a[0].shape   # = a[0,:,:,:].shape
Out[5]: TensorShape([28, 28, 3])In [6]: a[0,:,:,:].shape
Out[6]: TensorShape([28, 28, 3])In [7]: a[0,1,:,:].shape
Out[7]: TensorShape([28, 3])In [8]: a[:,:,:,0].shape
Out[8]: TensorShape([4, 28, 28])In [9]: a[:,:,:,2].shape
Out[9]: TensorShape([4, 28, 28])In [10]: a[:,0,:,:].shape
Out[10]: TensorShape([4, 28, 3])

**

四 start : end : step形式的切片操作

**
–start : end : step为含头不含尾的切片操作,表示从start位开始,一直到end-1位结束,其步长为step

In [1]: import tensorflow as tf                                                 In [2]: a = tf.random.normal([4,28,28,3],mean=1,stddev=1)                       In [3]: a.shape
Out[3]: TensorShape([4, 28, 28, 3])In [4]: a[0:2,:,:,:].shape
Out[4]: TensorShape([2, 28, 28, 3])In [5]: a[:,0:28:2,0:28:2,:].shape
Out[5]: TensorShape([4, 14, 14, 3])In [6]: a[:,:14,:14,:].shape
Out[6]: TensorShape([4, 14, 14, 3])In [7]: a[:,14:,14:,:].shape
Out[7]: TensorShape([4, 14, 14, 3])In [8]: a[:,::2,::2,:].shape
Out[8]: TensorShape([4, 14, 14, 3])

–: : -1 默认从末尾逆序采样到开头,其步长为1
–: : -2 默认从末尾逆序采样到开头,其步长为2
–2: : -2 从第2位逆序采样到开头,其步长为2
–A:B: -2 从位置A逆序采样到第位,其步长为2

In [9]: a = tf.range(5)                                                         In [10]: a
Out[10]: <tf.Tensor: id=29, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>In [11]: a[::-1]#从默认的末尾逆序采样到默认的开头,其步长为1
Out[11]: <tf.Tensor: id=34, shape=(5,), dtype=int32, numpy=array([4, 3, 2, 1, 0], dtype=int32)>In [12]: a[::-2]#从默认的末尾逆序采样的默认的开头,其步长为2
Out[12]: <tf.Tensor: id=39, shape=(3,), dtype=int32, numpy=array([4, 2, 0], dtype=int32)>In [13]: a[2::-2]#从第2位开始逆序采样的默认的开头,其步长为2
Out[13]: <tf.Tensor: id=44, shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>In [14]: a[3:1:-2]#从第3位开始逆序采样的第1为,其步长为2
Out[14]: <tf.Tensor: id=49, shape=(1,), dtype=int32, numpy=array([3], dtype=int32)>

–…切片

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

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

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

    ** 一 tf.gather( ) 函数 ** –tf.gather(params, indices, validate_indices=None, name=None, axis=0) params ...

  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. 数据处理的两个基本问题05 - 零基础入门学习汇编语言42
  2. 什么样的架构师才是真正的架构师?
  3. 肇东一中2021高考成绩查询,肇东一中2018高考喜报成绩
  4. linux系统写一个脚本,编写一个简单的linuxshell脚本
  5. Jetpack 新成员 SplashScreen:为全新的应用启动效果赋能!
  6. 前端浏览器兼容性网站
  7. ubuntu安装最新版apktool(最新版)反编译工具
  8. 93岁计算机密码发明人去世:创建全球首个分时系统,成为计算机普及开端
  9. 拓端tecdat|R语言曲线回归:多项式回归、多项式样条回归、非线性回归数据分析
  10. 数据结构 第四章 串
  11. 计算机的外面板接口,(电脑各种接口规范.doc
  12. 小程序API可以实现哪些功能
  13. Pytorch之Optim(优化器)
  14. 2016 知识点汇总 mindmap
  15. 指纹识别 matlab
  16. 罕见霜降胡杨照片,太美了!
  17. 如何科学地评价妹子身材?三围符合黄金比例是审美标准?你错了!
  18. ArcGisJS实现地图常用工具条、距离测量和面积测量(非官方实例)
  19. 你的走路姿势正确吗?步态不对牵连多种疾病
  20. matlab语法介绍——fft()、ifft()、fftshift()

热门文章

  1. signal() 和 sigaction()
  2. Java 数据库编程 ResultSet 的 使用方法
  3. WAMPSERVER 启动后打开LOCALHOST是一张IIS7的图片的解决
  4. Zend Framework学习(3)第一个zend应用
  5. C#使用Monitor类、Lock和Mutex类进行多线程同步
  6. Ext this.getView(...).saveDocumentAs is not a function
  7. SAP FI配置步骤
  8. 解决80端口被占用的情况
  9. 【Flask】ORM 关系一对一
  10. winform窗体对象 单例模式与泛型结合