神经网络-反向传播

写了一个两层网络的反向传播过程

import numpy as np

def num_output(num_input, w):
       netput = np.dot(w, num_input)
       output = 1.0/(1.0 + np.exp(netput))
       return output, netput

def delte_of_w2(target, output, refer_output):
       delte = np.dot(-(target-output)*output*(1-output), refer_output.T)
       return delte

def delte_of_w1(w, target, output, refer_output, input):
       delte = np.dot(np.dot(w.T, -(target-output)*output*(1-output)) * refer_output * (1 - refer_output), input.T)
       return delte

if __name__ == '__main__':
       max_loop = 1
       target = np.array([[0.05], [0.1]])
       input1 = np.array([[0.01], [0.99]])
       w_first = np.array([[0.15, 0.2], [0.25, 0.3]])
       w_second = np.array([[0.4, 0.45], [0.5, 0.55]])
       for i in range(max_loop):
               i += 1
               output_of_hidden_layer, netput_of_hidden_layer = num_output(input1, w_first)
               output_of_final, netput_of_final = num_output(netput_of_hidden_layer, w_second)
               w_second += delte_of_w2(target, output_of_final, output_of_hidden_layer)
               w_first += delte_of_w1(w_second, target, output_of_final, output_of_hidden_layer, input1)
       print output_of_final

你可能感兴趣的:(神经网络-反向传播)