BP算法程序,数据到我博客下载:
import random
import pandas as pd
import numpy as np
import csv
import math
from numpy import random
def seed(d,q,l): # d代输入层神经元个数,q代表隐层神经元的个数,l代表输出层元素的个数,该函数用来初始化权重和阈值
v=np.array([[random.random() for i in range(q)] for j in range(d)]) #初始化输入层到隐层权重
#print(v)
w=np.array([[random.random() for i in range(l)] for j in range(q)]) #初始化隐层到输出层权重
hyu=np.array([random.random() for i in range(q)])#初始化化隐藏层阈值
oyu=np.array([random.random() for i in range(l)]) #初始化输出层阈值
return v,w,hyu,oyu
def forward(indata,hyu,oyu):#需要传递进去样本属性值indata
a=np.dot(indata,v)#隐层输入
#print(a.shape)
h=sigmoid(a-hyu)
h=np.array(h)
b=np.dot(h,w)
forwardy=sigmoid(b-oyu)
return forwardy,b,h
def sigmoid(X):
for i in range(len(X)):
X[i]=1/(1+math.exp(-X[i]))
return X