TensorFlow學習筆記之三(神經網絡的優化)

使用激活函數去線性化

TensorFlow遊樂場:http://playground.tensorflow.org

對於上一篇的網絡TensorFlow筆記之二可以抽象爲如下形式,

[y]=[a1,1a1,2a1,3][w1,1(2)w2,1(2)w2,1(2)]=[x1x2][w1,1(1)w1,2(1)w1,3(1)w2,1(1)w2,2(1)w2,3(1)][w1,1(2)w2,1(2)w2,1(2)] \begin{gathered} \begin{bmatrix} y \end{bmatrix} \end{gathered}=\begin{gathered} \begin{bmatrix} a_{1,1} & a_{1,2} & a_{1,3} \end{bmatrix} \end{gathered} *\begin{gathered} \begin{bmatrix} w^{(2)}_{1,1}\\w^{(2)}_{2,1}\\w^{(2)}_{2,1} \end{bmatrix} \end{gathered}= \begin{gathered} \begin{bmatrix} x_{1} & x_{2} \end{bmatrix} \end{gathered} *\begin{gathered} \begin{bmatrix} w^{(1)}_{1,1} & w^{(1)}_{1,2} & w^{(1)}_{1,3} \\ w^{(1)}_{2,1} & w^{(1)}_{2,2} & w^{(1)}_{2,3} \end{bmatrix} \end{gathered}* \begin{gathered} \begin{bmatrix} w^{(2)}_{1,1}\\w^{(2)}_{2,1}\\w^{(2)}_{2,1} \end{bmatrix} \end{gathered}
其中,令:
W=W(1)W(2)=[w1,1(1)w1,2(1)w1,3(1)w2,1(1)w2,2(1)w2,3(1)][w1,1(2)w2,1(2)w2,1(2)]=[w1w2] W^{'}=W^{(1)}W^{(2)}= \begin{gathered} \begin{bmatrix} w^{(1)}_{1,1} & w^{(1)}_{1,2} & w^{(1)}_{1,3} \\ w^{(1)}_{2,1} & w^{(1)}_{2,2} & w^{(1)}_{2,3} \end{bmatrix} \end{gathered}* \begin{gathered} \begin{bmatrix} w^{(2)}_{1,1}\\w^{(2)}_{2,1}\\w^{(2)}_{2,1} \end{bmatrix} \end{gathered}= \begin{gathered} \begin{bmatrix} w^{'}_{1}\\w^{'}_{2} \end{bmatrix} \end{gathered}
這樣,就可以得出
[y]=[x1x2][w1w2]=w1x1+w2x2 \begin{gathered} \begin{bmatrix} y \end{bmatrix} \end{gathered}= \begin{gathered} \begin{bmatrix} x_{1} & x_{2} \end{bmatrix} \end{gathered} * \begin{gathered} \begin{bmatrix} w^{'}_{1}\\w^{'}_{2} \end{bmatrix} \end{gathered}=w^{'}_{1}x_{1}+w^{'}_{2}x_{2}
可以看出,該網絡雖然有兩層,但是它在本質上與單層神經網絡沒有區別,因爲它可以被一個單層網絡表示。由此可以推斷,如果只是每個神經元只是單純的線性變換,多層的全連接網絡與單層的全連接網絡的表達能力沒有區別(通過矩陣運算,最終都會變成一個矩陣)。它們最終都是y是關於x1和x2的一個線性模型,而且線性模型解決問題的能力是有限的,這就是線性模型的最大侷限性。

實驗證明線性模型的侷限性

本次實驗在TensorFlow遊樂場中進行。

情景模擬:現在根據x1(零件長度與平均長度的差)和x2(零件質量與平均質量的差)來判斷一個零件是否合格(二分類問題)。因此,當一個零件的長度和質量越接近平均值(x1和x2接近零),那麼這個零件越可能合格。那麼它可能呈現如下分佈
在這裏插入圖片描述
圖中藍色的點代表合格的零件(x1和x2接近零)。黃色的點代表不合格的(x1或x2偏大)。
將激活函數選擇線性模型。訓練100輪後
在這裏插入圖片描述
發現並不能很好的將零件區分開來。

將激活函數選擇ReLu,訓練100輪。
在這裏插入圖片描述
可以發現模型很好的將零件區分開來了。

常用激活函數

在這裏插入圖片描述

神經網絡複雜度:用網絡層數和網絡參數的個數表示

在這裏插入圖片描述
由於輸入層只接受數據,不做計算故輸入層不算層數。

損失函數(loss):預測值(y)與已知答案(y_)的差距

網絡優化目標:loss最小。
常用loss:

  1. mse(Mean Squared Error) 均方誤差
  2. 自定義
  3. ce(Cross Entropy)交叉熵

均方誤差mse

在這裏插入圖片描述
當預測值(y)與已知答案(y_)越接近–>均方誤差MSE(y_, y)越接近0–>損失函數的值越小–模型預測效果越好

預測酸奶模型

# 預測

import tensorflow as tf
import numpy as np

BATCH_SIZE = 8
seed = 23455

# 基於seed產生隨機數
rng = np.random.RandomState(seed=seed)
X = rng.rand(32, 2)
Y_ = [[x1+x2+(rng.rand()/10.0-0.05)] for (x1, x2) in X]
Y = [[int(x0 + x1 <1)] for (x0, x1) in X]



# 1、定義神經網絡的輸入、參數和輸出,定義前向傳播過程
x = tf.placeholder(tf.float32,shape=(None, 2))  # 知道每組有兩個特徵變量,但是不知道多少組,用None佔位
y_ = tf.placeholder(tf.float32,shape=(None, 1)) # 存放真實的結果值,合格爲1,

w1 = tf.Variable(tf.random_normal([2, 1], stddev=1, seed=1))


y = tf.matmul(x, w1)

# 2、定義損失函數以及反向傳播方法
loss = tf.reduce_mean(tf.square(y-y_)) # 使用均方誤差計算loss
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss) # 學習率爲0.001,並讓損失函數讓減小的方向優化

# 3、生成會話,訓練STEPS輪
with tf.Session() as sess:

    # 3.1、初始化參數值
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    print("w1:\n", sess.run(w1))
    print("\n")

    # 3.2、訓練模型
    STEPS = 30000
    for i in range(STEPS):

        # 3.2.1 每輪確定讀取數據集的遊標
        start = (i*BATCH_SIZE) % 32
        end = start + BATCH_SIZE

        # 3.2.2 喂入數據,開始訓練
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y_[start:end]})


        # 3.2.3 每500輪輸出一次loss值
        if i % 500 == 0:
            print("每500輪輸出一次w1的值\n")
            print(sess.run(w1), "\n")
            total_loss = sess.run(loss, feed_dict={x: X, y_: Y})
            print("After % dtraining step(s), cross entropy on all data is % g" % (i, total_loss))

    print("w1:\n", sess.run(w1))

交叉熵ce(Cross Entropy):表徵兩個概率分佈之間的距離

H(y,y)=Σylogy H(y_,y) = -Σy_-*log y

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章