1、tf.sigmoid函数
应用sigmoid函数可以将输出压缩至0~1的范围
计算公式为
f
(
x
)
=
1
1
+
e
−
x
f(x) = \frac{1}{1 + e^{-x}}
f(x)=1+e−x1

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ϵ[0~9]p(y=i∣x)=1,而sigmoid只能保证单个的点,不能保证所有的点的和为1,要想实现这个功能,见下面的softmax
2、tf.nn.softmax函数

注,一般将没有加激活函数的称为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 之间

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)>