from math import exp import numpy as np from random import * '''
设定1个输入层,2个隐藏层,1个输出层,共4层神经网络 输入:x 输出:y 隐藏层和输出层的神经元个数拟定为2,4,2 ''' #激活函数以及导数 def
sigmod(vec,tag): #vec为一个向量,tag标识是否要求导(后向反馈需要用到求导) res=[] for ele in vec:
tmp=1/(1+exp(-ele)) if tag==0: res.append(tmp) elif tag==1:
res.append(tmp*(1-tmp)) return np.array(res) #构造测试数据 def CreateDataSet():
dataset = np.array([ [0,0], [0,1], [1,0], [1,1] ])
labels=np.array(['a','b','b','a']) return dataset,labels
xdata,label=CreateDataSet() #将y变量由文本转为数值型 label_set=sorted(set(label))
label_set_len=len(label_set) ydata=[] for i in range(len(label)): tmp=[0,0]
pos=label_set.index(label[i]) tmp[pos]=1 ydata.append(tmp)
ydata=np.array(ydata) #这里拿一个样本模拟下整个迭代过程 x = xdata[1, :] y = ydata[1, :]
#最大迭代次数和初始化迭代次数 maxiter=50000 iter_counts=0 #初始化向量 w1 = np.array([[randint(1,
100) / 10000 for i in range(2)] for i in range(2)]) w2 = np.array([[randint(1,
100) / 10000 for i in range(2)] for i in range(4)]) w3 = np.array([[randint(1,
100) / 10000 for i in range(4)] for i in range(2)]) while(iter_counts<maxiter
): iter_counts+=1 #前向传播 a=w1.dot(x) b=sigmod(a,0) c=w2.dot(b) d=sigmod(c,0)
e=w3.dot(d) g=sigmod(e,0) error=sum(abs(y-g)) #后向反馈 delta_3=-(y-g)*sigmod(e,1)
for i in range(2): w3[i,:]=w3[i,:]-0.01*d*delta_3[i] #完成w3的更新
delta_2=delta_3.dot(w3)*sigmod(c,1) #这个是一个核心公式 for i in range(4):
w2[i,:]=w2[i,:]-0.01*b*delta_2[i] delta_1=delta_2.dot(w2)*sigmod(a,1) for i in
range(2): w1[i,:]=w1[i,:]-0.01*x*delta_1[i] if iter_counts==2 or
iter_counts==100 or iter_counts ==1000: print(error) if(error<0.05):
print(iter_counts) break 输出:1.0000715993145035 0.9404592328521744
0.5725032719370472 46211
热门工具 换一换