# 反向传播

其中σ(x)是sigmoid函数，σ(x)的导数 = σ(x)·[1-σ(x)]

# 代码实现

``````import numpy as np

class net:
def __init__(self,input_nodes,hidden_nodes,output_nodes,learning_rate):
self.input_nodes = input_nodes        #输入的节点数
self.hidden_nodes = hidden_nodes      #隐藏层的节点数
self.output_nodes = output_nodes      #输出的节点数
self.learning_rate = learning_rate    #学习率

self.activation_function = lambda x:1/(1+np.exp(-x))        #sigmoid激活函数

self.w = np.random.randn(hidden_nodes,input_nodes)*0.01     #隐藏层的权重矩阵
self.v = np.random.randn(output_nodes,hidden_nodes)*0.01    #输出层的权重矩阵
self.b1 = np.array(np.zeros(hidden_nodes),ndmin=2).T        #隐藏层的偏置
self.b2 = np.array(np.zeros(output_nodes),ndmin=2).T        #输出层的偏置
def train(self,inputs,targets):
inputs = np.asfarray(np.array(inputs,ndmin=2).T)            #将输入和真值矩阵转置一下
targets = np.asfarray(np.array(targets,ndmin=2).T)

hidden_level_input = np.dot(self.w,inputs)+self.b1          #WX+A = P
hidden_level_output = self.activation_function(hidden_level_input)   #σ(P) = Q

output_level_input = np.dot(self.v,hidden_level_output)+self.b2     #VQ+B = O
output_level_output = self.activation_function(output_level_input)  #σ(O) = Y

loss = 1/2*(np.sum(np.square(targets-output_level_output)))         #平方损失

# 计算梯度
G = (targets-output_level_output)*output_level_output*(1-output_level_output) #(T-Y)xY(1-Y) = G
E = hidden_level_output*(1-hidden_level_output)*np.dot(self.v.T,G)            #GxVxQ(1-Q)
self.v+=self.learning_rate*np.dot(G,hidden_level_output.T)
self.w+=self.learning_rate*np.dot(E,inputs.T)
self.b1+=self.learning_rate*E
self.b2+=self.learning_rate*G``````

THE END