如何解决深度神经网络,反向传播一层隐藏层
我正在尝试使用一些综合生成的数据使该网络学习,我正在使用该功能 F(x)=3x 我最初实现了 ReLu 函数,但为了方便起见,我使用了恒等函数作为激活。
import numpy as np
import matplotlib.pyplot as plt
def F(x):
return -3*x
#First we generate our data set X and Y
X=np.random.rand(10,1)
Y=[]
for x in X:
y=F(x)
y=y+np.random.uniform(-0.1,0.1)
Y.append(y)
#Forward propagation
def forward(X,W1,W2,b1,b2):
z1=np.dot(W1,X)+b1
a1=z1
z2=np.dot(W2,a1)+b2
a2=z2
return(z1,a1,z2,a2)
#define Back propagation
def back(X,Y,b2):
gw1=np.zeros(np.shape(W1))
gw2=np.zeros(np.shape(W2))
gb1=np.zeros(np.shape(b1))
gb2=np.zeros(np.shape(b2))
salidas=[]
for i in range(len(X)):
z1,a2=forward(X[i],b2)
#d2 is added to a list (salidas) to calculate the error
d2=a2-y[i]
salidas.append(d2)
d1=np.dot(np.transpose(W2),d2)
gw1=gw1 + np.outer(d1,X[i])
gw2=gw2 + np.outer(d2,a1)
gb1=gb1 + d1
gb2=gb2 + d2
return np.array(gw1),np.array(gw2),np.array(gb1),np.array(gb2),np.array(salidas)
#Now error
#this take 'salidas' and gets the quadratic error
def error(C):
m=len(C)
e=(1/(2*m))*np.linalg.norm(C)**2
return e
#finally,we define the neural network,which takes as an argument,the data set X,number of epochs and learning rate eta.
def red(X,epocas,eta):
#parameters start randomly,with values between zero and one
W1=np.random.rand(3,1)
W2=np.random.rand(1,3)
b1=np.random.rand(3)
b2=np.random.rand(1)
for i in range(epocas):
#the BP gives us the gradients to update the weights and biases
gw1,gw2,gb1,gb2,salidas=back(X,b2)
print('error is ',error(salidas))
#we update the values
W1=W1-eta/(2*len(x))*gw1
W2=W2-eta/(2*len(x))*gw2
b1=b1-eta/(2*len(x))*gb1
b2=b2-eta/(2*len(x))*gb2
现在我们测试网络,但我们注意到它没有学习,实际上每次误差都在增加
X=np.random.rand(10,0.1)
Y.append(y)
red(X,100,0.5)
我该怎么做才能让它发挥作用?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。