your_first_nn 发表于 2018-08-14 | 阅读次数: 字数统计: 123 | 阅读时长 ≈ 1 123456789101112131415161718192021222324252627import numpy as np class NeuralNetwork(object): def __init__(self, x, y): self.input = x self.weight1 = np.random.rand(self.input.shape[1],4) self.weight2 = np.random.rand(4,1) self.y = y self.output = np.zeros(self.y.shape) def feedforward(self): self.layer1 = sigmoid(np.dot(self.input, self.weight1)) self.layer2 = sigmoid(np.dot(self.layer1, self.weight2)) def backprop(self): d_weight2 = np.dot(self.layer1.T, (2*(self.y - self.output))) d_weight1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output)*sigmoid_derivative(self.output), self.weight2.T)*sigmoid_derivative(self.layer1))) self.weight1 += d_weight1 self.weight2 += d_weight2 def sigmoid_derivative(x): return x*(1-x) def sigmoid(z): return 1.0/(1.0+math.exp(-z))