Tensorflow2.0笔记 - 范式norm,reduce_min/max/mean,argmax/min, equal,unique

练习norm,reduce_min/max,argmax/min, equal,unique等相关操作。

范数主要有三种:

复制代码
import tensorflow as tf
import numpy as np

tf.__version__

#范数参考:https://blog.csdn.net/HiWangWenBing/article/details/119707541
tensor = tf.convert_to_tensor([[-3,-4],[3,4]])
tensor = tf.cast(tensor, dtype=tf.float32)
print(tensor)

#L1范数,ord=1
print("========L1 Norm============")
#不指定axis,所有元素的绝对值相加
print("====>tf.norm(tensor, ord=1):", tf.norm(tensor, ord=1))
#指定axis
print("====>tf.norm(tensor, ord=1, axis=0):", tf.norm(tensor, ord=1, axis=0))
print("====>tf.norm(tensor, ord=1, axis=1):", tf.norm(tensor, ord=1, axis=1))

#tf.norm不带参数,默认为L2范数(平方和开根号) 
print("========L2 Norm============")
print("===>tf.norm(tensor):", tf.norm(tensor))
print("    sqrt(reduce_sum(square(tensor))):", tf.sqrt(tf.reduce_sum(tf.square(tensor))))
#指定axis维度上进行L2范数求解,
#对于例子中的2x2 tensor来说,axis=0表示按照行的维度进行计算
#(实际就是计算每列元素的L2范数,这里可能有点绕,抓住行的概念,
# 可以理解成矩阵的每行每个元素做平方和,然后对所有行进行相加,都是以行为基本概念即可,不拆分列)
print("====>tf.norm(tensor, axis=0):", tf.norm(tensor, axis=0))
print("====>tf.norm(tensor, axis=1):", tf.norm(tensor, ord=2, axis=1))

#inf-norm,无穷阶范数
print("========Inf Norm============")
#不带参数
print("===>tf.norm(tensor, ord=np.inf):", tf.norm(tensor, ord=np.inf))
print("===>tf.norm(tensor, ord=np.inf, axis=0):", tf.norm(tensor, ord=np.inf, axis=0))
print("===>tf.norm(tensor, ord=np.inf, axis=1):", tf.norm(tensor, ord=np.inf, axis=1))


#reduce_min/max/mean,找最小值/最大值/均值
tensor = tf.random.uniform([4,10], maxval=6, dtype=tf.int32)
tensor = tf.cast(tensor, dtype=tf.float32)
print(tensor)

#不指定维度,对所有元素进行统计
print("=====>axis not specified")
print("min:", tf.reduce_min(tensor))
print("max:", tf.reduce_max(tensor))
print("mean", tf.reduce_mean(tensor))
#指定维度
print("=====>axis=0")
print("min:", tf.reduce_min(tensor, axis=0))
print("max:", tf.reduce_max(tensor, axis=0))
print("mean", tf.reduce_mean(tensor, axis=0))

print("=====>axis=1")
print("min:", tf.reduce_min(tensor, axis=1))
print("max:", tf.reduce_max(tensor, axis=1))
print("mean", tf.reduce_mean(tensor, axis=1))

#argmin/max求最大最小值所在位置
tensor = tf.random.uniform([4,10], maxval=100, dtype=tf.int32)
tensor = tf.cast(tensor, dtype=tf.float32)
print(tensor)

#默认axis=0
print("=====>tf.argmax(tensor):", tf.argmax(tensor))
print("=====>tf.argmin(tensor, axis=1):", tf.argmin(tensor, axis=1))

#tf.equal,比较对应元素是否相等,相等返回True,不等返回False
tensor0 = tf.constant([1,2,2,3,5])
tensor1 = tf.range(5)

print(tensor0)
print(tensor1)
print("=====>tf.equal(tensor0,tensor1):", tf.equal(tensor0, tensor1))
#统计匹配上的元素个数
print("=====>tf.reduce_sum(tf.cast(tf.equal(tensor0,tensor1), dtype=tf.int32)):", tf.reduce_sum(tf.cast(tf.equal(tensor0,tensor1), dtype=tf.int32)))


#tf.unique用于统计重复元素,返回一个去重后的tensor和对应的index列表
#参考:https://www.w3cschool.cn/tensorflow_python/tensorflow_python-duv62o0r.html
#仅针对1维tensor
tensor = tf.random.uniform([10], maxval=10, dtype=tf.int32)
tensor = tf.cast(tensor, dtype=tf.float32)
print(tensor)

print("=====>tf.unique(tensor):", tf.unique(tensor))

运行结果:

相关推荐
回眸&啤酒鸭4 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智4 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
AI的探索之旅4 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein4 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu4 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
美狐美颜SDK开放平台4 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk