# 第五章 神經網絡

標準BP

# coding: utf-8
# 標準的BP 我們沒運行一條數據改變更新一次參數,在一次數據集的遍歷只把誤差累計起來,各參數的導數只用每次求得的導數更新,不用累計!
import pandas as pd
from pandas import *
import numpy as np
from numpy import *
x = pd.read_csv(r"C:\Users\zmy\Desktop\titanic\xigua4.csv",header=None,nrows=8)
x = x.transpose()
x = array(x)
result = pd.read_csv(r"C:\Users\zmy\Desktop\titanic\xigua4.csv",header=None,skiprows=8,nrows=1)
result = result.transpose()
result = array(result)
result = result - 1

#bp算法
m,n = shape(x)
t = 1
v = np.random.rand(n,n+1) # 輸入層與隱含層的權值
w = np.random.rand(n+1, t)# 隱含層和輸出層的權值
thy = np.random.rand(n+1)# 隱含層的閾值
tho = np.random.rand(t)# 輸出層的閾值
out = np.zeros((m,t))# 輸出層的輸出值
bn = np.zeros(n+1)#隱含層的輸出值
gj = np.zeros(t)
eh = np.zeros(n+1)
xk = 1

kn = 0 # 迭代次數
sn = 0 #
old_ey = 0
ii = 5
while(1):
    ii -= 1
    kn = kn + 1
    ey = 0
    for i in range(0,m):
        #計算隱含層輸出
        for j in range(0, n+1):
            ca = 0
            for h in range(0, n):
                ca = ca + v[h][j] * x[i][h]
            bn[j] = 1/(1+exp(-ca+thy[j]))
        # 計算輸出層輸出
        for h1 in range(0,t):
            ba = 0
            for h2 in range(0,n+1):
                ba = ba + w[h2][h1] * bn[h2]
            out[i][h1] = 1 / (1+ exp(-ba + tho[h1]))
        # 計算累積誤差
        for h1 in range(0,t):
            ey = ey + pow((out[i][h1] - result[i]), 2)/2
            # print 'ey', ey
        # 計算gj
        for h1 in range(0,t):
            gj[h1] =  out[i][h1]*(1-out[i][h1])*(result[i] - out[i][h1])
            # print out[i][h1],result[i]
        # 計算eh
        for h1 in range(0,n+1):
            for h2 in range(0, t):
                eh[h1] = bn[h1] * (1 - bn[h1]) * w[h1][h2]*gj[h2]
        # 更新w
        for h2 in range(0, t):
            for h1 in range(0,n+1):
                w[h1][h2] = w[h1][h2] + xk * gj[h2] * bn[h1]
        #更新輸出閾值
        for h1 in range(0,t):
            tho[h1] = tho[h1] - xk * gj[h1]
        # 更新輸入層與隱含層的權值
        for h2 in range(0, n + 1):
            for h1 in range(0, n):
                v[h1][h2] = v[h1][h2] + xk * eh[h2] * x[i][h1]
        #更新隱含層閾值
        for h1 in range(0,n+1):
            thy[h1] = thy[h1] - xk * eh[h1]
    if(abs(ey-old_ey) < 0.0001):
        # print abs(ey-old_ey)
        sn = sn + 1
        if(sn == 100):
            break

    else:
        old_ey = ey
        # ey = 0
        sn = 0


for i in range(0,m):
    for j in range(0,t):
        print i,out[i][j], result[i]

結果爲: (行標,預測值,實際值)

0 0.00433807645401 [0]
1 0.00532600009637 [0]
2 0.00427358256359 [0]
3 0.0208781402544 [0]
4 0.0189059379881 [0]
5 0.989732982598 [1]
6 0.990855525385 [1]
7 0.968537961141 [1]
8 0.998602511829 [1]
9 0.998036011905 [1]
10 0.015951504667 [0]
11 0.0151684814171 [0]
12 0.0116095552438 [0]
13 0.998160689389 [1]
14 0.998935912 [1]
15 0.990103694861 [1]
16 0.988335990994 [1]

累計BP

    # coding: utf-8
    # 累計BP 相對於標準BP是每次把所有的數據都運行完,把所有的誤差都累計起來在更新參數
    import pandas as pd
    import numpy as np
    from pandas import *
    from numpy import *

    x = pd.read_csv(r'C:\Users\zmy\Desktop\titanic\xigua4.csv', header=None, nrows=8)
    y = pd.read_csv(r'C:\Users\zmy\Desktop\titanic\xigua4.csv', header= None, skiprows=8,nrows=1)
    y = y-1
    x = x.transpose()
    x = array(x)
    y = array(y)
    y = y.transpose()

    Eta = 1 # 學習率
    t = 1 # 輸出
    m, n = shape(x) # 數據集的行與列
    w = np.random.rand(n+1, n) # 隱含層與輸出層的權重
    v = np.random.rand(n, n+1) # 輸入層與隱含層的權重
    Zta = np.random.rand(t) # 輸出層閾值
    Gamma = np.random.rand(n+1) # 隱含層閾值
    bn = zeros((m,n+1)) # 隱含層輸出
    yk = zeros((m,t)) # 輸出層輸出
    Alpha = zeros(n)

    gj = zeros(m)
    eh = zeros((m,n+1))

    k = 0
    sn = 0
    old_ey = 0
    while(1):
        k += 1
        ey = 0
        for i in range(0, m):
            for h1 in range(0, n+1):
                temp = 0
                for h2 in range(0, n):
                    temp = temp + v[h2][h1] * x[i][h2]
                bn[i][h1] = 1 / (1+ exp(-temp+ Gamma[h1]))

            for h1 in range(0, t):
                temp = 0
                for h2 in range(0, n+1):
                    temp += w[h2][h1] * bn[i][h2]
                yk[i][h1] = 1 / (1 + exp(-temp + Zta[h1]))
            # 計算累計誤差
            for h1 in range(0,t):
                ey += pow(yk[i][h1] - y[i], 2) / 2

        for h1 in range(0, m):
            gj[h1] = yk[h1][0] * (1 - yk[h1][0]) * (y[h1] - yk[h1][0])
        for i in range(0, m):
            for h1 in range(n+1):
                temp = 0
                for h2 in range(0, t):
                    temp += w[h1][h2] * gj[i]
                eh[i][h1] = bn[i][h1] * (1 - bn[i][h1]) * temp
        w1 = zeros((n+1, t))
        v1 = zeros((n,n+1))
        Zta1 = zeros(t)
        Gamma1 = zeros(n+1)
        # 計算四個參數的導數
        for i in range(0, m):
            for h1 in range(0, t):
                Zta1[h1] += (-1) * gj[i] * Eta
                for h2 in range(0, n+1):
                    w1[h2][h1] += Eta * gj[i] * bn[i][h2]

            for h1 in range(0, n+1):
                Gamma1[h1] += Eta * (-1) * eh[i][h1]
                for h2 in range(0, n):
                    v1[h2][h1] += Eta * eh[i][h1] * x[i][h2]
        # 更新參數
        v = v + v1
        w = w + w1
        Gamma = Gamma + Gamma1
        Zta = Zta + Zta1
        if (abs(old_ey-ey) < 0.0001) :
            sn += 1
            if sn == 100:
                break
        else:
            old_ey = ey
            sn = 0


    for i in range(0,m):
        for j in range(0,t):
            print i,yk[i][j], y[i]

結果輸出

0 9.28395603439e-05 [0]
1 0.00408553765608 [0]
2 0.00750763695368 [0]
3 0.0285684849738 [0]
4 0.00591863864577 [0]
5 0.985047169485 [1]
6 0.991935228407 [1]
7 0.968524769653 [1]
8 0.996291194651 [1]
9 0.996365288109 [1]
10 0.00064978194805 [0]
11 0.0158218829283 [0]
12 0.0127356648444 [0]
13 0.996965938838 [1]
14 0.999528551401 [1]
15 0.999273125275 [1]
16 0.991180793173 [1]

發佈了36 篇原創文章 · 獲贊 12 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章