BP神经网络实现

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# BP-ANN.py
# Created on: 2014-06-12 09:49:56.00000
# Description:
# ---------------------------------------------------------------------------

import os
import math
import time
import datetime

#输入层n个神经元
n = 4
#隐含层p个神经元
p = 6
#输出层q个神经元
q = 1

#输入向量
trainsample = 60
x = [[0 for i in range(0, n)] for j in range(trainsample)]
x = [[0.27500,0.60000,0.14085,0.07143],[0.22500,0.43333,0.14085,0.07143],[0.17500,0.50000,0.12676,0.07143],[0.15000,0.46667,0.15493,0.07143],[0.25000,0.63333,0.14085,0.07143],[0.35000,0.73333,0.18310,0.14286],[0.15000,0.56667,0.14085,0.10714],[0.25000,0.56667,0.15493,0.07143],[0.10000,0.40000,0.14085,0.07143],[0.22500,0.46667,0.15493,0.03571],[0.35000,0.66667,0.15493,0.07143],[0.20000,0.56667,0.16901,0.07143],[0.20000,0.43333,0.14085,0.03571],[0.07500,0.43333,0.09859,0.03571],[0.45000,0.76667,0.11268,0.07143],[0.42500,0.90000,0.15493,0.14286],[0.35000,0.73333,0.12676,0.14286],[0.27500,0.60000,0.14085,0.10714],[0.42500,0.70000,0.18310,0.10714],[0.27500,0.70000,0.15493,0.10714],[0.75000,0.50000,0.60563,0.50000],[0.60000,0.50000,0.57746,0.53571],[0.72500,0.46667,0.63380,0.53571],[0.37500,0.20000,0.50704,0.46429],[0.62500,0.36667,0.59155,0.53571],[0.42500,0.36667,0.57746,0.46429],[0.57500,0.53333,0.60563,0.57143],[0.22500,0.23333,0.40845,0.35714],[0.65000,0.40000,0.59155,0.46429],[0.30000,0.33333,0.49296,0.50000],[0.25000,0.10000,0.43662,0.35714],[0.47500,0.43333,0.53521,0.53571],[0.50000,0.16667,0.50704,0.35714],[0.52500,0.40000,0.60563,0.50000],[0.40000,0.40000,0.45070,0.46429],[0.67500,0.46667,0.56338,0.50000],[0.40000,0.43333,0.57746,0.53571],[0.45000,0.33333,0.52113,0.35714],[0.55000,0.16667,0.57746,0.53571],[0.40000,0.26667,0.49296,0.39286],[0.57500,0.53333,0.78873,0.89286],[0.45000,0.33333,0.66197,0.67857],[0.77500,0.43333,0.77465,0.75000],[0.57500,0.40000,0.73239,0.64286],[0.62500,0.43333,0.76056,0.78571],[0.90000,0.43333,0.87324,0.75000],[0.22500,0.26667,0.57746,0.60714],[0.82500,0.40000,0.83099,0.64286],[0.67500,0.26667,0.76056,0.64286],[0.80000,0.63333,0.80282,0.89286],[0.62500,0.50000,0.66197,0.71429],[0.60000,0.33333,0.69014,0.67857],[0.70000,0.43333,0.71831,0.75000],[0.42500,0.26667,0.64789,0.71429],[0.45000,0.36667,0.66197,0.85714],[0.60000,0.50000,0.69014,0.82143],[0.62500,0.43333,0.71831,0.64286],[0.92500,0.70000,0.88732,0.78571],[0.92500,0.30000,0.91549,0.82143],[0.50000,0.16667,0.64789,0.53571]]

d = [0 for i in range(0, trainsample)]
d = [0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7]

#隐含层(p个隐含层神经元, 每个隐含层神经元有对应输入层神经元有一个系数)
hi = [0 for i in range(0, p)]
ho = [0 for i in range(0, p)]
b1 = [0.3 for i in range(0, p)]
Whi = [[0.5 for i in range(0, n)] for j in range(p)]
pp = [0 for i in range(0, p)]
rateWhi = 0.05

#输出层(q个输出层神经元, 每个输出层神经元有对应隐含层神经元有一个系数)
yi = [0 for i in range(0, q)]
yo = [1 for i in range(0, q)]
b2 = [0.3 for i in range(0, q)]
Woh = [[0.5 for i in range(0, p)] for j in range(q)]
qq = [0 for i in range(0, q)]
rateWoh = 0.05
#误差函数
e=1.0


def f1(x):
    y = 1.0/(1.0+math.exp(-1.0*x))
    return y

def f2(x):
    y = f1(x)
    y = y * (1.0 - y)
    return y
    
def train():
    e = 0.0
    for trainIndex in range(0, trainsample):  # trainsample
        #print(x[trainIndex])
        #隐含层 输入 输出
        for i in range(0, p):
            hi[i] = 0
            for j in range(0, n):
                hi[i] = hi[i] + Whi[i][j] * x[trainIndex][j]
            ho[i] = f1(hi[i]+b1[i])
        #输出层 输入 输出
        for i in range(0, q):
            yi[i] = 0
            for j in range(0, p):
                yi[i] = yi[i] + Woh[i][j] * ho[j]
            yo[i] = f1(yi[i]+b2[i])
        #print(hi)
        #误差函数对输出层的各神经元的偏导数
        for i in range(0, q):
            qq[i] = 0
            qq[i] = (d[trainIndex] - yo[i]) * yo[i] * (1 - yo[i])# * ho[i]
            for k in range(0, p):
                Woh[i][k] = Woh[i][k] + rateWoh * qq[i] * ho[k]

        for i in range(0, p):
            pp[i] = 0
            for j in range(0, q):
                pp[i] = pp[i] + qq[j] * Woh[j][i]
            pp[i] = pp[i] * ho[i] * (1 - ho[i])
            for k in range(0, n):
                Whi[i][k] = Whi[i][k] + rateWhi * pp[i] * x[trainIndex][k]
        
        for i in range(0, q):    
            e = e + 0.5 * math.pow((d[trainIndex] - yo[i]), 2)
        
        #
        for i in range(0, q):
            b2[i] = b2[i] + rateWoh * qq[i]
        for i in range(0, p):
            b1[i] = b1[i] + rateWhi * pp[i]
    return e

def recognize(vv):
    #隐含层 输入 输出
    for i in range(0, p):
        hi[i] = 0
        for j in range(0, n):
            hi[i] = hi[i] + Whi[i][j] * vv[j]
        ho[i] = f1(hi[i]+b1[i])
    #输出层 输入 输出
    for i in range(0, q):
        yi[i] = 0
        for j in range(0, p):
            yi[i] = yi[i] + Woh[i][j] * ho[j]
        yo[i] = f1(yi[i]+b2[i])
    print('-------------------------------------------------------')
    print(yo)

    
times = 0
while e > 0.001:
    #计算各层输入和输出
    e = train()
    times = times + 1
    print(str(times)+"   " + str(e))
    if times > 10000:
        break
recognize([0.7, 0.5, 0.774647887, 0.821428571])
recognize([0.4,0.433333333,0.521126761,0.464285714])                                        
recognize([0.25,0.5,0.112676056,0.071428571])
posted @ 2016-08-18 11:45  ParamousGIS  阅读(289)  评论(0编辑  收藏  举报