import tensorflow as tf
x = tf.range(9)
'''输出
tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
'''
# tf.maximum()实现数据的下限幅
tf.maximum(x,2)
'''输出
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([2, 2, 2, 3, 4, 5, 6, 7, 8])>
'''
# tf.minimum()实现数据的上限幅
tf.minimum(x,7)
'''输出
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([2, 2, 2, 3, 4, 5, 6, 7, 7])>
'''
# tf.clip_by_value()实现上下限幅
tf.clip_by_value(x,2,7)
'''输出
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([2, 2, 2, 3, 4, 5, 6, 7, 7])>
'''
那么ReLU激活函数可以实现为:
import tensorflow as tf
x = tf.random.uniform([2,3],minval=-1,maxval=1,dtype=tf.float32)
print(x)
def relu(x):
return tf.maximum(x,0.)
relu(x)
'''输出
import tensorflow as tf
x = tf.random.uniform([2,3],minval=-1,maxval=1,dtype=tf.float32)
print(x)
def relu(x):
return tf.maximum(x,0.)
relu(x)
'''