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))

运行结果:

相关推荐
在所不辞兄1 小时前
【零基础学智能仿真-53】多智能体协作——有限元自动化实战从工程需求到可审核的仿真报告
人工智能·深度学习·神经网络·机器学习·有限元
张张123y2 小时前
后端第一课:输入 URL 后发生了什么?我用手写 Socket 把全链路跑通了
python·websocket·ai·socket·url·后端开发
AI Coder 码农王4 小时前
AgentScope Java v2 第3课,从 ReActAgent 到 HarnessAgent
人工智能
格物致胜工业数智技术4 小时前
明治EOS系列传感器技术解析:精密制造高反光金属件在位检测
人工智能·制造
甲维斯4 小时前
DeepSeek 新版还是难评!
人工智能
Answer1st4 小时前
【嵌入式模块】UART通信模块
笔记
晨间晓4 小时前
图片细节丰富不丰富?用华为云码道开发 OpenCV 纹理复杂度分析工具
人工智能·opencv·华为云
全栈弄潮儿4 小时前
Python实战第7期:字典与集合
后端·python·编程语言
和侧4 小时前
离子注入后为什么必须配套退火工艺?
笔记·学习
2603_965148114 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化