微信扫一扫打赏支持

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()
points: (500, 500, 2)
z: (500, 500)

详细过程分析

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)
tf.Tensor([0.   1.57 3.14 4.71 6.28], shape=(5,), dtype=float32)
tf.Tensor([0.   1.57 3.14 4.71 6.28], shape=(5,), dtype=float32)
==========================================
[<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0.  , 1.57, 3.14, 4.71, 6.28],
       [0.  , 1.57, 3.14, 4.71, 6.28],
       [0.  , 1.57, 3.14, 4.71, 6.28],
       [0.  , 1.57, 3.14, 4.71, 6.28],
       [0.  , 1.57, 3.14, 4.71, 6.28]], dtype=float32)>, <tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[0.  , 0.  , 0.  , 0.  , 0.  ],
       [1.57, 1.57, 1.57, 1.57, 1.57],
       [3.14, 3.14, 3.14, 3.14, 3.14],
       [4.71, 4.71, 4.71, 4.71, 4.71],
       [6.28, 6.28, 6.28, 6.28, 6.28]], dtype=float32)>]
==========================================
tf.Tensor(
[[0.   1.57 3.14 4.71 6.28]
 [0.   1.57 3.14 4.71 6.28]
 [0.   1.57 3.14 4.71 6.28]
 [0.   1.57 3.14 4.71 6.28]
 [0.   1.57 3.14 4.71 6.28]], shape=(5, 5), dtype=float32)
tf.Tensor(
[[0.   0.   0.   0.   0.  ]
 [1.57 1.57 1.57 1.57 1.57]
 [3.14 3.14 3.14 3.14 3.14]
 [4.71 4.71 4.71 4.71 4.71]
 [6.28 6.28 6.28 6.28 6.28]], shape=(5, 5), dtype=float32)
In [8]:
# [5, 5, 2]
points = tf.stack([point_x, point_y], axis=2)
print(points)
tf.Tensor(
[[[0.   0.  ]
  [1.57 0.  ]
  [3.14 0.  ]
  [4.71 0.  ]
  [6.28 0.  ]]

 [[0.   1.57]
  [1.57 1.57]
  [3.14 1.57]
  [4.71 1.57]
  [6.28 1.57]]

 [[0.   3.14]
  [1.57 3.14]
  [3.14 3.14]
  [4.71 3.14]
  [6.28 3.14]]

 [[0.   4.71]
  [1.57 4.71]
  [3.14 4.71]
  [4.71 4.71]
  [6.28 4.71]]

 [[0.   6.28]
  [1.57 6.28]
  [3.14 6.28]
  [4.71 6.28]
  [6.28 6.28]]], shape=(5, 5, 2), dtype=float32)
In [ ]:
 

 

 

 
posted @ 2020-08-03 01:43  范仁义  阅读(299)  评论(0编辑  收藏  举报