11、张量限幅

  • clip_by_value 根据具体数值进行裁剪
  • relu  相当于tf.maxinum( )
  • clip_by_norm 根据总体的范数来裁剪张量
  • gradient clipping 

1、clip_by_value 根据具体数值进行裁剪

(1)下限幅:max(0,x) ,if  x < 0,result = 0 ,即x最后的输出值是大于等于0的

    上限幅:min(0,x) ,if  x > 0,result = 0,即x最后的输出值是小于等于0的

1 a = tf.range(10)  #tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
2 b1 = tf.maximum(3,a) #这两个参数的顺序可以是任意的,
3 print(b1)  #tf.Tensor([3 3 3 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
4 
5 b2 = tf.minimum(a,7)
6 print(b2)  #tf.Tensor([0 1 2 3 4 5 6 7 7 7], shape=(10,), dtype=int32)
7 
8 b3 = tf.clip_by_value(a,2,8) #对值的上下限进行限定
9 print(b3) #tf.Tensor([2 2 2 3 4 5 6 7 8 8], shape=(10,), dtype=int32)

(2)tf.nn.relu(a) 等价于 tf.maxinum(a,0)

1 a = tf.range(10)  #tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
2 a = a - 5 #tf.Tensor([-5 -4 -3 -2 -1  0  1  2  3  4], shape=(10,), dtype=int32)
3 
4 b1 = tf.nn.relu(a)
5 b2 = tf.maximum(a,0)
6 print(b1,"\n",b2)

输出:

tf.Tensor([0 0 0 0 0 0 1 2 3 4], shape=(10,), dtype=int32) 
tf.Tensor([0 0 0 0 0 0 1 2 3 4], shape=(10,), dtype=int32)

2、clip_by_norm 根据范数进行裁剪,

3、Gradient clipping 

猜你喜欢

转载自www.cnblogs.com/pengzhonglian/p/11936009.html