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