import numpy as np
import matplotlib.pyplot as plt
definit_model(X, Y, layers):
model ={}
nodes =[]
nodes.append(X.shape[0])for l in layers:
nodes.append(l)
nodes.append(Y.shape[0])
model['depth']=len(layers)+1for n inrange(model['depth']):
model['W'+str(n +1)]= np.random.rand(nodes[n+1], nodes[n])
model['B'+str(n +1)]= np.random.rand(nodes[n+1],1)return model
defsigmoid(x):return1.0/(1+ np.exp(-x))defforward_propagation(X, Y, model):
result = X.copy()
model['A0']= result.copy()for i inrange(model['depth']):
W = model['W'+str(i+1)]
B = model['B'+str(i+1)]
result = np.dot(W, result)+ B
model['Z'+str(i+1)]= result.copy()
result = sigmoid(result)
model['A'+str(i+1)]= result.copy()return result
defcross_entropy(Y, result):return-Y * np.log(result)-(1- Y)* np.log(1- result)defcost(Y, result):return np.sum(cross_entropy(Y, result))/ Y.shape[1]defbackward_propagation(Y, model, learning_rate):
L = model['depth']
m = Y.shape[1]
delta = model['A'+str(L)]- Y
model['dW'+str(L)]= np.dot(delta, model['A'+str(L-1)].T)/ m
model['dB'+str(L)]= np.sum(delta, axis =1, keepdims=True)/ m
for l inrange(L -1,0,-1):
delta =(np.dot(model['W'+str(l+1)].T, delta)* model['A'+str(l)]*(1- model['A'+str(l)]))
model['dW'+str(l)]= np.dot(delta, model['A'+str(l-1)].T)/ m
model['dB'+str(l)]= np.sum(delta, axis=1, keepdims=True)/ m
for l inrange(model['depth'],0,-1):
model['W'+str(l)]-= learning_rate * model['dW'+str(l)]
model['B'+str(l)]-= learning_rate * model['dB'+str(l)]deftrain(X, Y, model, learning_rate =0.05, times =50, show_cost_history =True):
cost_history =[]
result =0for t inrange(times):
result = forward_propagation(X, Y, model)
backward_propagation(Y, model, learning_rate)
cost_history.append(cost(Y, result))if show_cost_history:
plt.plot(cost_history)
plt.show()if __name__ =='__main__':
m =1000
X = np.random.rand(2, m)
Y = np.zeros((1, m))for i inrange(m):if(X[0, i]<0.5)and(X[1, i]<0.5):
Y[0, i]=1
model = init_model(X, Y,[2])
train(X, Y, model)