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

输出范围

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

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

实数范围

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

零一之间

  1. 二分类
    • y > 0.5 , y>0.5, y>0.5, 正类。
    • y &lt; 0.5 , y&lt;0.5, y<0.5, 负类。
  2. 图片生成
    • rgb [ 0 , 255 ] − [ 0 , 1 ] [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) out=relu(X@W+b)
  2. sigmoid
  3. o u t ′ = σ ( o u t ) out&#x27; = \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)=(exex)/(ex+ex)
    在这里插入图片描述
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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TransientYear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值