2

我正在编写一个程序来在 python 中执行神经网络我正在尝试设置反向传播算法。基本思想是,我查看 5,000 个训练示例并收集错误并找出我需要向哪个方向移动 theta,然后将它们向那个方向移动。有训练示例,然后我使用一个隐藏层,然后是一个输出层。但是,我在这里弄错了梯度/导数/错误,因为我没有正确移动 theta,因为它们需要移动。我今天花了 8 个小时,不知道我做错了什么。谢谢你的帮助!!

x = 401x5000 matrix

y = 10x5000 matrix   # 10 possible output classes, so one column will look like [0, 0, 0, 1, 0... 0] to indicate the output class was 4

theta_1 = 25x401

theta_2 = 10x26


alpha=.01

    sigmoid= lambda theta, x: 1 / (1 + np.exp(-(theta*x)))


        #move thetas in right direction for each iteration
        for iter in range(0,1):
            all_delta_1, all_delta_2 = 0, 0
            #loop through each training example, 1...m    
            for t in range(0,5000):

                hidden_layer = np.matrix(np.concatenate((np.ones((1,1)),sigmoid(theta_1,x[:,t]))))
                output_layer = sigmoid(theta_2,hidden_layer)

                delta_3 = output_layer - y[:,t]
                delta_2= np.multiply((theta_2.T*delta_3),(np.multiply(hidden_layer,(1-hidden_layer))))

                #print type(delta_3), delta_3.shape, type(hidden_layer.T), hidden_layer.T.shape
                all_delta_2 += delta_3*hidden_layer.T
                all_delta_1 += delta_2[1:]*x[:,t].T



            delta_gradient_2 = (all_delta_2 / m)
            delta_gradient_1 = (all_delta_1 / m)
            theta_1 = theta_1- (alpha * delta_gradient_1)
            theta_2 = theta_2- (alpha * delta_gradient_2)
4

1 回答 1

0

看起来您的渐变与未压缩的输出层有关。

尝试更改output_layer = sigmoid(theta_2,hidden_layer)output_layer = theta_2*hidden_layer.

或者重新计算压扁输出的梯度。

于 2012-09-15T16:51:58.093 回答