import tensorflow as tf

a = tf.random.normal([3, 3])
a
mask = a > 0
mask
# 为True元素,即>0的元素的索引
indices = tf.where(mask)
indices
# 取回>0的值
tf.gather_nd(a, indices)
A = tf.ones([3, 3])
B = tf.zeros([3, 3])
# True的元素会从A中选值,False的元素会从B中选值
tf.where(mask, A, B)
indices = tf.constant([[4], [3], [1], [7]])
updates = tf.constant([9, 10, 11, 12])
shape = tf.constant([8])
# 把updates按照indices的索引放在底板shape上
tf.scatter_nd(indices, updates, shape)
indices = tf.constant([[0], [2]])
updates = tf.constant([
[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
])
updates.shape
shape = tf.constant([4, 4, 4])
tf.scatter_nd(indices, updates, shape)
import numpy as np

points = []

for y in np.linspace(-2, 2, 5):
for x in np.linspace(-2, 2, 5):
points.append([x, y]) np.array(points)
y = tf.linspace(-2., 2, 5)
y
x = tf.linspace(-2., 2, 5)
x
points_x, points_y = tf.meshgrid(x, y)
points_x.shape
points_x
points_y
points = tf.stack([points_x, points_y], axis=2)
points

吴裕雄--天生自然TensorFlow2教程:高阶操作的更多相关文章

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

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

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

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

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

  4. 吴裕雄--天生自然TensorFlow2教程:链式法则

    import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...

  5. 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  6. 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度

    import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...

  7. 吴裕雄--天生自然TensorFlow2教程:损失函数及其梯度

    import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...

  8. 吴裕雄--天生自然TensorFlow2教程:激活函数及其梯度

    import tensorflow as tf a = tf.linspace(-10., 10., 10) a with tf.GradientTape() as tape: tape.watch( ...

  9. 吴裕雄--天生自然TensorFlow2教程:梯度下降简介

    import tensorflow as tf w = tf.constant(1.) x = tf.constant(2.) y = x * w with tf.GradientTape() as ...

随机推荐

  1. java有参

    一.带参数的方法 语法: 访问修饰符  方法返回值类型   方法名称 (参数列表){ // 方法体 注意:每个参数之间用","分割 二.形参和实参 形参:在方法定义时指定的参数 实 ...

  2. OKR-Periods of Words「POI 2006」

    题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串 P 是串 A 的前缀,当且仅当存在串 B,使得 A = PB.如果 P != A 并且 P 不是一个空串,那么我们说 P  ...

  3. vld扩展

    PHP代码的执行实际上是在执行代码解析后的各种opcode.通过vld扩展可以很方便地看到执行过程中的opcode. 一.安装vld扩展 git clone https://github.com/de ...

  4. 使用six库将Python2的项目完全转移到python3

    SIX是用于python2与python3兼容的库. 它存在的目的是为了拥有无需修改即可在Python 2和Python 3上同时工作的代码.话虽这么说,但是这并不代表在Python 3中引用该库就可 ...

  5. 8051单片机中访问int中字节的方法

    在使用单片机中,unsigned int 占2个字节,unsigned char 占一个字节.而单片机是实行的字节寻址.16字节的bit寻址实在是不好用, 不好用在不能建数组. 在实际的开发过程中,要 ...

  6. hadoop-mapreduce的官方示例的测试执行方法

    1.根据给出的精度参数计算 pi : hadoop jar /export/servers/hadoop-2.6.0-cdh5.14.0/share/hadoop/mapreduce/hadoop-m ...

  7. db.mybatis.config

    config1: <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryB ...

  8. css调试与样式优先级

    如何查看一个标签的当前css样式 如上图所示 先用标签选择器选择某个标签 然后在elements区域就会自动找到该标签 然后在右侧的styles区域整个区域都是该标签的样式,从上到下是显示的优先级,被 ...

  9. dedeCMS网站栏目管理---释义

    一.常规选项 文件保存目录:可以指定保存的目录,也可以用拼音,指定为拼音时系统会自动生成栏目拼音的目录,文件保存目录一般用拼音即可. 目录相对位置:有上级目录,CMS目录与站点根目录.实际上指的都是网 ...

  10. nginx配置多个项目

    使用Nginx要在同一个域名下配置多个项目有两种方式: nginx按不同的目录分发给不同的项目 启用二级域名,不同的项目分配不同的二级域名 1.nginx按不同的目录分发给不同的项目: server ...