Tensorflow 张量运算2:数据限幅

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)
'''
发布了93 篇原创文章 · 获赞 2 · 访问量 3036

猜你喜欢

转载自blog.csdn.net/qq_40041064/article/details/104795261