a = tf.random.normal([3, 3])
mask = a>0
#
# array([[ True, True, True],
# [False, True, True],
# [ True, True, True]])>
tf.boolean_mask(a, mask)
#
# array([0.469083 , 0.78703344, 2.418932 , 1.9637926 , 0.31090873,
# 0.11894408, 0.70458823, 0.00413397], dtype=float32)>
indices = tf.where(mask)
#
# array([[0, 0],
# [0, 1],
# [0, 2],
# [1, 1],
# [1, 2],
# [2, 0],
# [2, 1],
# [2, 2]], dtype=int64)>
tf.gather_nd(a, indices)
#
# array([0.469083 , 0.78703344, 2.418932 , 1.9637926 , 0.31090873,
# 0.11894408, 0.70458823, 0.00413397], dtype=float32)>
注:
mask
#
# array([[ True, True, True],
# [False, True, True],
# [ True, True, True]])>
A = tf.ones([3,3])
#
# array([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]], dtype=float32)>
B = tf.zeros([3,3])
#
# array([[0., 0., 0.],
# [0., 0., 0.],
# [0., 0., 0.]], dtype=float32)>
tf.where(mask, A, B)
#
# array([[1., 1., 1.],
# [0., 1., 1.],
# [1., 1., 1.]], dtype=float32)>
注:
updates shape output
indices = tf.constant([[4],[3],[1],[7]])
updates = tf.constant([9, 10, 11, 12])
shape = tf.constant([8])
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]]])
shape = tf.constant([4, 4, 4])
tf.scatter_nd(indices, updates, shape)
#
# array([[[5, 5, 5, 5],
# [6, 6, 6, 6],
# [7, 7, 7, 7],
# [8, 8, 8, 8]],
# [[0, 0, 0, 0],
# [0, 0, 0, 0],
# [0, 0, 0, 0],
# [0, 0, 0, 0]],
# [[5, 5, 5, 5],
# [6, 6, 6, 6],
# [7, 7, 7, 7],
# [8, 8, 8, 8]],
# [[0, 0, 0, 0],
# [0, 0, 0, 0],
# [0, 0, 0, 0],
# [0, 0, 0, 0]]])>
import numpy as np
def meshgrid():
points = []
for y in np.linspace(-2, 2, 5):
for x in np.linspace(-2, -2, 5):
points.append([x,y])
return np.array(points)
注:
y = tf.linspace(-2., 2, 5)
#
x = tf.linspace(-2., 2, 5)
#
points_x, points_y = tf.meshgrid(x, y)
points_x.shape
points_x
<tf.Tensor: id=206, shape=(5, 5), dtype=float32, numpy=
array([[-2., -1., 0., 1., 2.],
[-2., -1., 0., 1., 2.],
[-2., -1., 0., 1., 2.],
[-2., -1., 0., 1., 2.],
[-2., -1., 0., 1., 2.]], dtype=float32)>
points_y
<tf.Tensor: id=207, shape=(5, 5), dtype=float32, numpy=
array([[-2., -2., -2., -2., -2.],
[-1., -1., -1., -1., -1.],
[ 0., 0., 0., 0., 0.],
[ 1., 1., 1., 1., 1.],
[ 2., 2., 2., 2., 2.]], dtype=float32)>
points = tf.stack([points_x, points_y], axis=2)
#
# array([[[-2., -2.],
# [-1., -2.],
# [ 0., -2.],
# [ 1., -2.],
# [ 2., -2.]],
# [[-2., -1.],
# [-1., -1.],
# [ 0., -1.],
# [ 1., -1.],
# [ 2., -1.]],
# [[-2., 0.],
# [-1., 0.],
# [ 0., 0.],
# [ 1., 0.],
# [ 2., 0.]],
# [[-2., 1.],
# [-1., 1.],
# [ 0., 1.],
# [ 1., 1.],
# [ 2., 1.]],
# [[-2., 2.],
# [-1., 2.],
# [ 0., 2.],
# [ 1., 2.],
# [ 2., 2.]]], dtype=float32)>
z = sin ( x ) + sin ( y ) z = \sin (x) + \sin(y) z=sin(x)+sin(y)
import tensorflow as tf
import os
import matplotlib.pyplot as plt
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def func(x):
"""
:param x: [b, 2]
:return:
"""
z = tf.math.sin(x[...,0]) + tf.math.sin(x[...,1])
return z
x = tf.linspace(0., 2*3.14, 500)
y = tf.linspace(0., 2*3.14, 500)
# [50, 50]
point_x, point_y = tf.meshgrid(x, y)
# [50, 50, 2]
points = tf.stack([point_x, point_y], axis=2)
# points = tf.reshape(points, [-1, 2])
print('points:', points.shape)
z = func(points)
print('z:', z.shape)
plt.figure('plot 2d func value')
plt.imshow(z, origin='lower', interpolation='none')
plt.colorbar()
plt.figure('plot 2d func contour')
plt.contour(point_x, point_y, z)
plt.colorbar()
plt.show()