吴裕雄--天生自然TensorFlow2教程:张量限幅

import tensorflow as tf

a = tf.range(10)
a
# a中小于2的元素值为2
tf.maximum(a, 2)
# a中大于8的元素值为8
tf.minimum(a, 8)
# a中的元素值限制在[2,8]区间内
tf.clip_by_value(a, 2, 8)
a = a - 5
a
tf.nn.relu(a)
tf.maximum(a, 0)
缩放时不改变梯度方向
a = tf.random.normal([2, 2], mean=10)
a
tf.norm(a)
# 等比例的放缩a, norm为15
aa = tf.clip_by_norm(a, 15)
aa
tf.norm(aa)
Gradient Exploding or vanishing
set lr=1
new_grads,total_norm = tf.clip_by_global_norm(grads,25)
裁剪所有向量,但是所有向量的梯度方向都不变化

 

你可能感兴趣的:(吴裕雄--天生自然TensorFlow2教程:张量限幅)