tensorflow 2.0 神经网络与全连接层 之 输出方式

版权声明:本文为博主([email protected])原创文章,未经博主允许不得转载。 https://blog.csdn.net/z_feng12489/article/details/89882120

输出范围

不同的应用,不同的场景输出范围是不同的。

  1. y R d y \in R^d 整个实数范围。
  2. y i [ 0 , 1 ] , i = 0 , 1 , . . . , y d 1 y_i \in [0,1],i = 0,1,...,y_d-1 多分类每类的概率为零一之间。
  3. 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 多分类每类的概率为零一之间且和为一。
  4. y i [ 1 , 1 ] , i = 0 , 1 , . . . , y d 1 y_i \in [-1,1],i = 0,1,...,y_d-1

实数范围

  1. 线性回归
  2. 用 MSE 的简单分类问题
  3. 其他比较一般的预测
  4. o u t = r e l u ( X @ W + b ) out = relu(X@W+b)
    • logits (最后一层不激活)

零一之间

  1. 二分类
    • y > 0.5 , y>0.5, 正类。
    • y < 0.5 , y<0.5, 负类。
  2. 图片生成
    • rgb [ 0 , 255 ] [ 0 , 1 ] [0,255]-[0,1]

BIGGAN 生成的图片
在这里插入图片描述
如何压缩值到零一范围之内:

  1. o u t = r e l u ( X @ W + b ) out = relu(X@W+b)
  2. sigmoid
  3. o u t = σ ( o u t ) out' = \sigma(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})
    在这里插入图片描述
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)

猜你喜欢

转载自blog.csdn.net/z_feng12489/article/details/89882120