TensorFlow2_200729系列---10、meshgrid操作实例
TensorFlow2_200729系列---10、meshgrid操作实例
一、总结
一句话总结:
point_x, point_y = tf.meshgrid(x, y)
import tensorflow as tf import matplotlib.pyplot as plt 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) # [500, 500] point_x, point_y = tf.meshgrid(x, y) # [500, 500, 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) # print(z) 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()
二、meshgrid操作实例
博客对应课程的视频位置:
import tensorflow as tf
import matplotlib.pyplot as plt
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)
# [500, 500]
point_x, point_y = tf.meshgrid(x, y)
# [500, 500, 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)
# print(z)
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()
详细过程分析
In [7]:
x = tf.linspace(0., 2*3.14, 5)
y = tf.linspace(0., 2*3.14, 5)
# [5, 5]
point_x, point_y = tf.meshgrid(x, y)
print(x)
print(y)
print("==========================================")
print(tf.meshgrid(x, y))
print("==========================================")
print(point_x)
print(point_y)
In [8]:
# [5, 5, 2]
points = tf.stack([point_x, point_y], axis=2)
print(points)
In [ ]: