Tensorflow2.0笔记 - where,scatter_nd, meshgrid相关操作

本笔记记录tf.where进行元素位置查找,scatter_nd用于指派元素到tensor的特定位置,meshgrid用作绘图的相关操作。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

tf.__version__

#where操作查找元素位置
#输入的tensor是True,False组成的tensor
tensor = tf.random.uniform([3,3], minval=-12, maxval=12, dtype=tf.int32)
print(tensor.numpy())

#获得大于0的值的mask
mask = tensor > 0
print(mask)
#方式1:通过boolean_mask获得大于0的元素的值
print("=====tf.boolean_mask(tensor, mask):\n", tf.boolean_mask(tensor, mask).numpy())
#方式2:先通过where查询到大于0的元素位置,然后用gather_nd收集
indices = tf.where(mask)
print("=====indices for the ones greater than 0:\n", indices.numpy())
print("=====tf.gather_nd(tensor, indices):\n", tf.gather_nd(tensor, indices))

#where带条件选择元素
#where(cond, tensor1, tensor2)
#传入cond,如果cond对应位置为True,会收集tensor1对应位置的元素,否则收集tensor2对应位置的元素
tensor1 = tf.random.uniform([3,3], minval=-12, maxval=12, dtype=tf.int32)
tensor2 = tf.random.uniform([3,3], minval=-12, maxval=12, dtype=tf.int32)
print(tensor1)
print(tensor2)

cond = tensor1 > 0
print("=====Condition:\n", cond)
print("=====where(cond, tensor1, tensor2):\n", tf.where(cond, tensor1, tensor2))


#scatter_nd将元素放到对应位置,其他值为0
#scatter_nd(indices, updates, shape)
#indices指定要更新到的位置
#updates指定更新的值
#shape表示tensor的形状

#1维tensor的例子
indices = tf.constant([[4], [3], [1], [9]])
updates = tf.constant([6, 7, 8, 9])
shape = tf.constant([10])

print("=====tf.scatter_nd(indices, updates, shape):\n", tf.scatter_nd(indices, updates, shape))

#多维tensor的scatrer_nd
# shape为5x4x4
#将值更新到大维度的0和2处,实际对应一个4x4的tensor
indices = tf.constant([[0], [2], [4]])
updates = tf.constant([[
                        [1, 1, 1, 1],
                        [1, 1, 1, 1],
                        [1, 1, 1, 1],
                        [1, 1, 1, 1],
                      ],
                     [
                        [2, 2, 2, 2],
                        [2, 2, 2, 2],
                        [2, 2, 2, 2],
                        [2, 2, 2, 2],
                     ],
                     [
                        [3, 3, 3, 3],
                        [3, 3, 3, 3],
                        [3, 3, 3, 3],
                        [3, 3, 3, 3],
                     ]])
shape = tf.constant([5,4,4])
print("=====tf.scatter_nd(indices, updates, shape):\n", tf.scatter_nd(indices, updates, shape))

#meshgrid绘图
#1. 设置x和y的linspace
y = tf.linspace(-2., 2, 5)
x = tf.linspace(-2., 2, 5)

#获得坐标点tensor
xPoints, yPoints = tf.meshgrid(x, y)
print("X points:\n", xPoints)
print("Y points:\n", yPoints)
#通过tf.stack获得点的xy集合
points = tf.stack([xPoints, yPoints], axis=2)
print("Collection of XY points on plane:\n", points)

#meshgrid实例,z = sin(x) +sin(y)
x = tf.linspace(0., 2 * 3.14, 500)
y = tf.linspace(0., 2 * 3.14, 500)
xPoints, yPoints = tf.meshgrid(x, y)
points = tf.stack([xPoints, yPoints], axis=2)

z = tf.math.sin(points[..., 0]) + tf.math.sin(points[..., 1])
#绘制z的值
plt.figure('z = sin(x) + sin(y)')
plt.imshow(z, origin='lower', interpolation='none')
plt.colorbar()

#绘制等高线
plt.figure('plot contour')
plt.contour(xPoints, yPoints, z)
plt.colorbar()
plt.show()

运行结果:

相关推荐
jun778895几秒前
机器学习-监督学习:朴素贝叶斯分类器
人工智能·学习·机器学习
FL16238631292 分钟前
基于yolov5的混凝土缺陷检测系统python源码+onnx模型+评估指标曲线+精美GUI界面
人工智能·python·yolo
Kenneth風车5 分钟前
【第十三章:Sentosa_DSML社区版-机器学习聚类】
人工智能·低代码·机器学习·数据分析·聚类
jndingxin13 分钟前
OpenCV运动分析和目标跟踪(4)创建汉宁窗函数createHanningWindow()的使用
人工智能·opencv·目标跟踪
立黄昏粥可温13 分钟前
Python 从入门到实战22(类的定义、使用)
开发语言·python
机器之心14 分钟前
o1 带火的 CoT 到底行不行?新论文引发了论战
android·人工智能
机器之心20 分钟前
从架构、工艺到能效表现,全面了解 LLM 硬件加速,这篇综述就够了
android·人工智能
今天也要加油丫42 分钟前
`re.compile(r“(<.*?>)“)` 如何有效地从给定字符串中提取出所有符合 `<...>` 格式的引用
python
jndingxin1 小时前
OpenCV特征检测(1)检测图像中的线段的类LineSegmentDe()的使用
人工智能·opencv·计算机视觉