输出范围
不同的应用,不同的场景输出范围是不同的。
- y ∈ R d y \in R^d y∈Rd 整个实数范围。
- y i ∈ [ 0 , 1 ] , i = 0 , 1 , . . . , y d − 1 y_i \in [0,1],i = 0,1,...,y_d-1 yi∈[0,1],i=0,1,...,yd−1 多分类每类的概率为零一之间。
- y i ∈ [ 0 , 1 ] , ∑ i = 0 y d y i = 1 , i = 0 , 1 , . . . , y d − 1 y_i \in [0,1], \sum_{i=0}^{y_d}y_i=1, i = 0,1,...,y_d-1 yi∈[0,1],∑i=0ydyi=1,i=0,1,...,yd−1 多分类每类的概率为零一之间且和为一。
- y i ∈ [ − 1 , 1 ] , i = 0 , 1 , . . . , y d − 1 y_i \in [-1,1],i = 0,1,...,y_d-1 yi∈[−1,1],i=0,1,...,yd−1
实数范围
- 线性回归
- 用 MSE 的简单分类问题
- 其他比较一般的预测
-
o
u
t
=
r
e
l
u
(
X
@
W
+
b
)
out = relu(X@W+b)
out=relu(X@W+b)
- logits (最后一层不激活)
零一之间
- 二分类
- y > 0.5 , y>0.5, y>0.5, 正类。
- y < 0.5 , y<0.5, y<0.5, 负类。
- 图片生成
- rgb [ 0 , 255 ] − [ 0 , 1 ] [0,255]-[0,1] [0,255]−[0,1]。
BIGGAN 生成的图片
如何压缩值到零一范围之内:
- o u t = r e l u ( X @ W + b ) out = relu(X@W+b) out=relu(X@W+b)
- sigmoid
- o u t ′ = σ ( o u t ) out' = \sigma(out) out′=σ(out)
tf.sigmoid
a = tf.linspace(-6., 6, 10)
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(-6.0, shape=(), dtype=float32) tf.Tensor(6.0, shape=(), dtype=float32)
a = tf.sigmoid(a)
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(0.002472639, shape=(), dtype=float32) tf.Tensor(0.9975274, shape=(), dtype=float32)
a = tf.random.normal([1, 28*28])*5
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(-16.22344, shape=(), dtype=float32) tf.Tensor(16.260736, shape=(), dtype=float32)
a = tf.sigmoid(a)
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(5.9604645e-08, shape=(), dtype=float32) tf.Tensor(0.99999994, shape=(), dtype=float32)
零一之间 和为一
- sigmoid
a = tf.linspace(-2., 2, 5)
tf.reduce_sum(tf.sigmoid(a)) # tf.Tensor(2.5, shape=(), dtype=float32)
- softmax 大者更大
a = tf.linspace(-2., 2, 5)
tf.reduce_sum(tf.sigmoid(a)) # tf.Tensor(2.5, shape=(), dtype=float32)
tf.reduce_sum(tf.nn.softmax(a)) # tf.Tensor(1.0, shape=(), dtype=float32)
分类实例
logits = tf.random.uniform([1, 10], minval=-2, maxval=2)
# tf.Tensor(
# [[ 0.39318037 -1.1405406 -1.4796648 -1.059619 0.00410414 0.21543264
# 0.9332652 -1.734467 -0.23943186 1.796689 ]], shape=(1, 10), dtype=float32)
prob = tf.nn.softmax(logits, axis=1)
# tf.Tensor(
# [[0.02795015 0.0291838 0.08102272 0.01900888 0.0333832 0.07500502
# 0.23073502 0.07967106 0.31747448 0.10656565]], shape=(1, 10), dtype=float32)
tf.reduce_sum(prob) # tf.Tensor(1.0, shape=(), dtype=float32)
负一一之间
- tanh
t a n h ( x ) = s i n h ( x ) / c o s h ( x ) = ( e x − e − x ) / ( e x + e − x ) tanh(x) = sinh(x)/cosh(x) = (e^x - e^{-x})/(e^x+e^{-x}) tanh(x)=sinh(x)/cosh(x)=(ex−e−x)/(ex+e−x)
a = tf.linspace(-6., 6, 10)
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(-6.0, shape=(), dtype=float32) tf.Tensor(6.0, shape=(), dtype=float32)
a = tf.tanh(a)
tf.reduce_min(a), tf.reduce_max(a)
# tf.Tensor(-0.9999877, shape=(), dtype=float32) tf.Tensor(0.9999877, shape=(), dtype=float32)