版权声明:本文为博主([email protected])原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_feng12489/article/details/89882120
输出范围
不同的应用,不同的场景输出范围是不同的。
- 整个实数范围。
- 多分类每类的概率为零一之间。
- 多分类每类的概率为零一之间且和为一。
实数范围
- 线性回归
- 用 MSE 的简单分类问题
- 其他比较一般的预测
-
- logits (最后一层不激活)
零一之间
- 二分类
- 正类。
- 负类。
- 图片生成
- rgb 。
BIGGAN 生成的图片
如何压缩值到零一范围之内:
- sigmoid
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
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)