首页 > 解决方案 > 深度神经网络,反向传播一个隐藏层

问题描述

我正在尝试创建一个带有隐藏层的神经网络。 在此处输入图像描述

我试图让这个网络学习一些综合生成的数据,我使用函数 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,W1,W2,b1,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,a1,z2,a2=forward(X[i],W1,W2,b1,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, Y, number of epochs and learning rate eta.

def red(X,Y,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,Y,W1,W2,b1,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,1)
Y=[]
for x in X:
    y=F(x)
    y=y+np.random.uniform(-0.1,0.1)
    Y.append(y)
    
red(X,Y,100,0.5)

我该怎么做才能让它发挥作用?

谢谢

标签: pythonnumpydeep-learningneural-networkbackpropagation

解决方案


推荐阅读