Tensorflow2.0笔记 - Tensor的限值clip操作

本笔记主要记录使用maximum/minimum,clip_by_value和clip_by_norm来进行张量值的限值操作。

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

tf.__version__


#maximum/minimumz做上下界的限值
tensor = tf.random.shuffle(tf.range(10))
print(tensor)

#maximum(x, y, name=None)
#对比x和y,保留两者最大值,可以用作保证最小值为某一个特定值
#https://blog.csdn.net/qq_36379719/article/details/104321914
print("=====tf.maximum(tensor, 5):\n", tf.maximum(tensor, 5).numpy())
print("=====tf.maximum(tensor, [6,6,6,5,5,5,4,4,4,3]):\n", tf.maximum(tensor, [6,6,6,5,5,5,4,4,4,3]).numpy())
#minimum作用和maximum正好相反,可以用来保证最大值为某一个特定值
print("=====tf.minimum(tensor, 5):\n", tf.minimum(tensor, 5).numpy())
print("=====tf.minimum(tensor, [6,6,6,5,5,5,4,4,4,3]):\n", tf.minimum(tensor, [6,6,6,5,5,5,4,4,4,3]).numpy())


#clip_by_value,可以指定上下界参数
#这个函数本身可以用maximum和minimum组合实现
tensor = tf.random.shuffle(tf.range(10))
print(tensor)
#限定tensor的元素值在[2,5]区间
print("=====tf.clip_by_value(tensor,2,5):\n", tf.clip_by_value(tensor, 2, 5).numpy())

#多维tensor
tensor = tf.random.uniform([2,3,3], maxval=10, dtype=tf.int32)
print(tensor)
print("=====tf.clip_by_value(tensor,2,5):\n", tf.clip_by_value(tensor, 2, 5).numpy())


#relu函数限值,大于0的值保留原值,小于零的值变为0
#第一种方式(推荐),使用tf.nn.relu()
tensor = tf.random.uniform([3,3], minval=-10, maxval=10, dtype=tf.int32)
print(tensor)
print("=====tf.nn.relu(tensor):\n", tf.nn.relu(tensor).numpy())

#第二种方式,使用maximum
print("=====tf.maximum(tensor, 0) simulates relu:\n", tf.maximum(tensor, 0).numpy())

#根据范数来限值,clip_by_norm
#参考资料:https://blog.csdn.net/wn87947/article/details/82345537
#应用场景一般是针对梯度进行限值处理,通过范数clip会保持梯度方向不变
tensor = tf.convert_to_tensor([[3,2],[3,2]], dtype=tf.float32)
print(tensor)
print("Tensor Norm:", tf.norm(tensor).numpy())
clipped = tf.clip_by_norm(tensor, 3)
print("=====tf.clip_by_norm(tensor, 3):", clipped)
print("     Norm:", tf.norm(clipped))

运行结果:

相关推荐
夏天是冰红茶2 小时前
DINO原理详解
人工智能·深度学习·机器学习
吴佳浩5 小时前
Python入门指南(六) - 搭建你的第一个YOLO检测API
人工智能·后端·python
SHIPKING3935 小时前
【AI应用开发设计指南】基于163邮箱SMTP服务实现验证登录
人工智能
yong99906 小时前
基于SIFT特征提取与匹配的MATLAB图像拼接
人工智能·计算机视觉·matlab
知秋一叶1236 小时前
Miloco 深度打通 Home Assistant,实现设备级精准控制
人工智能·智能家居
superman超哥6 小时前
仓颉语言中基本数据类型的深度剖析与工程实践
c语言·开发语言·python·算法·仓颉
春日见6 小时前
在虚拟机上面无法正启动机械臂的控制launch文件
linux·运维·服务器·人工智能·驱动开发·ubuntu
Learner__Q6 小时前
每天五分钟:滑动窗口-LeetCode高频题解析_day3
python·算法·leetcode
————A7 小时前
强化学习----->轨迹、回报、折扣因子和回合
人工智能·python
CareyWYR7 小时前
每周AI论文速递(251215-251219)
人工智能