import numpy as np def relu(x): return np.where(x > 0, x, 0) def sigmoid(x): return 1 / (1 + np.exp(-x)) class NeuralNetwork(): def __init__(self): self.params = {} self.params['W1'] = np.random.rand(2, 3) self.params['b1'] = np.random.rand(1, 3) self.params['W2'] = np.random.rand(3, 2) self.params['b2'] = np.random.rand(1, 2) self.params['W3'] = np.random.rand(2, 1) self.params['b3'] = np.random.rand(1, 1) def forward(self, x): a1 = np.dot(x, self.params['W1']) + self.params['b1'] z1 = relu(a1) a2 = np.dot(z1, self.params['W2']) + self.params['b2'] z2 = relu(a2) a3 = np.dot(z2, self.params['W3']) + self.params['b3'] return np.where(sigmoid(a3) > 0.5, 1, 0) if __name__ == "__main__": nn = NeuralNetwork() print(nn.forward(np.array([3, 2])))
在下一篇文章当中我们将会来探讨神经网络的训练方法,也就是大名鼎鼎的反向传播算法,看看它是在神经网络当中如何运作的。