TensorFlow2的tf.sigmoid()函数、tf.nn.softmax()函数和tf.tanh()函数

1、tf.sigmoid函数

应用sigmoid函数可以将输出压缩至0~1的范围

计算公式为
f ( x ) = 1 1 + e − x f(x) = \frac{1}{1 + e^{-x}} f(x)=1+ex1

CSDN图标

tf.sigmoid()的函数的用法为:

tf.sigmoid
(
    x,
    name=None
)

参数说明:

  • x :类型为float16, float32, float64, complex64, or complex128的tensor
  • name: 操作的名称(可选)。

示例:

import tensorflow as tf

a = tf.linspace(-6.0, 6, 10 )
# Out[5]:
# <tf.Tensor: shape=(10,), dtype=float32, numpy=
# array([-6.       , -4.6666665, -3.3333333, -2.       , -0.6666665,
#         0.666667 ,  2.       ,  3.333334 ,  4.666667 ,  6.       ],
#       dtype=float32)>


tf.sigmoid(a)
# Out[6]:
# <tf.Tensor: shape=(10,), dtype=float32, numpy=
# array([0.00247262, 0.00931596, 0.0344452 , 0.11920292, 0.33924368,
#        0.6607564 , 0.880797  , 0.96555483, 0.99068403, 0.9975274 ],
#       dtype=float32)>

x = tf.random.normal([1, 28, 28]) * 5
tf.reduce_min(x), tf.reduce_max(x)
# Out[8]:
# (<tf.Tensor: shape=(), dtype=float32, numpy=-16.22868>,
#  <tf.Tensor: shape=(), dtype=float32, numpy=15.571212>)

x = tf.sigmoid(x)
tf.reduce_min(x), tf.reduce_max(x)
# Out[10]:
# (<tf.Tensor: shape=(), dtype=float32, numpy=8.95311e-08>,
#  <tf.Tensor: shape=(), dtype=float32, numpy=0.9999999>)

注:对于分类来讲,sigmoid并不能完全达到想要的功能,比如:对于一个10分类,它的结果范围为0~9,并且想要 ∑ i ϵ [ 0 ~ 9 ] p ( y = i ∣ x ) = 1 \sum_{i\epsilon [0~9]} p(y = i | x) = 1 iϵ[09]py=ix=1,而sigmoid只能保证单个的点,不能保证所有的点的和为1,要想实现这个功能,见下面的softmax

2、tf.nn.softmax函数

CSDN图标

注,一般将没有加激活函数的称为Logits,加了softmax后称为Probabilities,经过softmax后,有把最大值放大的过程,相当于把强的变得更强,把弱的变得更弱。

用法:

tf.nn.softmax
(
    logits,
    axis=None,
    name=None,
    dim=None
)

参数说明:

  • logits:一个非空的Tensor。必须是下列类型之一:half, float32,float64。
  • axis:将在其上执行维度softmax。默认值为-1,表示最后一个维度。
  • name:操作的名称(可选)。
  • dim:axis的已弃用的别名。

此函数执行相当于:
s o f t m a x = t f . e x p ( l o g i t s ) t f . r e d u c e _ s u m ( t f . e x p ( l o g i t s ) , a x i s ) softmax = \frac{tf.exp(logits)}{ tf.reduce\_sum(tf.exp(logits), axis)} softmax=tf.reduce_sum(tf.exp(logits),axis)tf.exp(logits)

a = tf.linspace(-2., 2., 5)
tf.nn.softmax(a)
# Out[13]:
# <tf.Tensor: shape=(5,), dtype=float32, numpy=
# array([0.01165623, 0.03168492, 0.08612854, 0.23412167, 0.6364086 ],
#       dtype=float32)>
logits = tf.random.uniform([1, 10], minval = -2, maxval = 2)
# Out[15]:
# <tf.Tensor: shape=(1, 10), dtype=float32, numpy=
# array([[-1.2757506 ,  1.3469572 , -0.6447406 , -0.6145048 , -0.9433541 ,
#          1.1180091 ,  0.956902  , -1.2451305 ,  0.70545626,  1.6877956 ]],
#       dtype=float32)>

prob = tf.nn.softmax(logits, axis = 1)

tf.reduce_sum(prob, axis = 1)
# Out[17]: <tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>

3、tf.tanh函数

把值压缩到 -1~1 之间

CSDN图标
tf.math.tanh
(
    x,
    name=None
)

示例:

a
# Out[18]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([-2., -1.,  0.,  1.,  2.], dtype=float32)>

tf.tanh(a)
# Out[19]:
# <tf.Tensor: shape=(5,), dtype=float32, numpy=
# array([-0.9640276, -0.7615942,  0.       ,  0.7615942,  0.9640276],
#       dtype=float32)>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值