import tensorflow as tf

a = tf.ones([1, 5, 5, 3])
a.shape a[0][0]
numpy : 索引

a = tf.random.normal([4, 28, 28, 3])
a.shape a[1].shape a[1, 2].shape
a[1][2][3].shape
a[1, 2, 3, 2].shape
一维切片
a = tf.range(10)
a a[-1:]
a[-2:]
a[:2]
a[:-1]
多维切片
a = tf.random.normal([4, 28, 28, 3])
a.shape a[0].shape a[0, :, :, :].shape a[0, 1, :, :].shape a[:, :, :, 0].shape a[:, :, :, 2].shape a[:, 0, :, :].shape
步长::step
a = tf.random.normal([4, 28, 28, 3])
a.shape a[0:2, :, :, :].shape a[:, 0:28:2, 0:28:2, :].shape a[:, :14, :14, :].shape a[:, 14:, 14:, :].shape a[:, ::2, ::2, :].shape
倒序::-1
a = tf.range(4)
a a[::-1] a[::-2] a[2::-2]
省略号...
a = tf.random.normal([2, 4, 28, 28, 3])
a.shape a[0].shape a[0, :, :, :, :].shape a[0, ...].shape a[:, :, :, :, 0].shape a[..., 0].shape a[0, ..., 2].shape a[1, 0, ..., 0].shape
gather

a = tf.random.normal([4, 35, 8])
a.shape tf.gather(a, axis=0, indices=[2, 3]).shape a[2:4].shape tf.gather(a, axis=0, indices=[2, 1, 3, 0]).shape tf.gather(a, axis=1, indices=[2, 3, 7, 9, 16]).shape tf.gather(a, axis=2, indices=[2, 3, 7]).shape aa = tf.gather(a,axis,[several students])
aaa = tf.gather(aa,axis,[several subjects])
gather_nd

a = tf.random.normal([4, 35, 8])
a.shape tf.gather_nd(a, [0]).shape # [[0],[],[]] tf.gather_nd(a, [0, 1]).shape tf.gather_nd(a, [0, 1, 2]).shape tf.gather_nd(a, [[0, 1, 2]]).shape tf.gather_nd(a, [[0, 0], [1, 1]]).shape tf.gather_nd(a, [[0, 0], [1, 1], [2, 2]]).shape # 第一个班级第一个学生的第一门课
# 第二个班级第二个学生的第二门课
# 第三个班级第三个学生的第三门课
tf.gather_nd(a, [[0, 0, 0], [1, 1, 1], [2, 2, 2]]).shape tf.gather_nd(a, [[[0, 0, 0], [1, 1, 1], [2, 2, 2]]]).shape
boolean_mask
a = tf.random.normal([4, 28, 28, 3])
a.shape tf.boolean_mask(a, mask=[True, True, False, False]).shape tf.boolean_mask(a, mask=[True, True, False], axis=3).shape a = tf.ones([2, 3, 4])
a.shape # [2,3],还剩下4,三个True,因此是3*4True
tf.boolean_mask(a, mask=[[True, False, False], [False, True, True]]).shape

吴裕雄--天生自然TensorFlow2教程:numpy [ ] 索引的更多相关文章

  1. 吴裕雄--天生自然TensorFlow2教程:高阶操作

    import tensorflow as tf a = tf.random.normal([3, 3]) a mask = a > 0 mask # 为True元素,即>0的元素的索引 i ...

  2. 吴裕雄--天生自然TensorFlow2教程:张量排序

    import tensorflow as tf a = tf.random.shuffle(tf.range(5)) a tf.sort(a, direction='DESCENDING') # 返回 ...

  3. 吴裕雄--天生自然TensorFlow2教程:创建Tensor

    import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...

  4. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  5. 吴裕雄--天生自然TensorFlow2教程:数据统计

    import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf ...

  6. 吴裕雄--天生自然TensorFlow2教程:维度变换

    图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片 ...

  7. 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型

    list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...

  8. 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战

    import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...

  9. 吴裕雄--天生自然TensorFlow2教程:反向传播算法

随机推荐

  1. Lombok认知

    Lombok的简介 Lombok是一款Java开发插件,公司项目到处使用,整体效果很棒,代码更干净.Java开发人员可以节省出重复构建,诸如hashCode和equals这样的方法以及各种业务对象模型 ...

  2. RabbitMQ整合Spring Booot【消费者补偿幂等问题】

    如果消费者 运行时候 报错了 package com.toov5.msg.SMS; import org.springframework.amqp.rabbit.annotation.RabbitHa ...

  3. oracle11g导出dmp文件时不能导出空表,导致缺表

    一.执行 select 'analyze table '||table_name||' compute statistics;' from user_tables; 将该查询语句查询到的结果粘贴到sq ...

  4. V-Distpicker不能完整显示内容

    V-Distpicker插件在列表中,或者在dialog中只显示了第一次的内容,第二次就开始报错.这个和前篇中的地图问题其实如出一辙. 解决办法,重加载,局部刷新. <el-form-item ...

  5. P5091 【模板】欧拉定理(欧拉降幂)

    P5091 [模板]欧拉定理 以上3张图是从这篇 博客 里盗的,讲的比较清楚. #include<bits/stdc++.h> using namespace std; typedef l ...

  6. python matplotlib给图中的点加标签

    在写论文用到matplotlib画散点图,想着如果能把每个点对应的ID打在点的旁边就好了,经过一番搜索,最后找到了方法. 首先是打点,先把所有的点画好,举例如下: p1 = ax.scatter(X[ ...

  7. c++ opencv 动态内存

    1.CvMemStorage定义动态内存存储器   内存存储器是一个用来存储诸如序列.轮廓.图形和子划分等动态增长数据结构的底层结构 2.示例 CvMemStorage *mems = cvCreat ...

  8. 【LeetCode】二叉树的最大深度

    [问题]给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数.说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

  9. 【LeetCode】克隆图

    [问题]给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆).图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[Node]). 解释: 节点 的值是 ,它有两个邻居:节点 和 ...

  10. hibernate.hbm.xml必须必须配置主键

    hibernate.hbm.xml必须必须配置主键 <id name="XXid" type="java.lang.long" column=" ...