本文完全利用numpy实现一个简单的BP神经网络,由于是做regression而不是classification,因此在这里输出层选取的激励函数就是f(x)=x。BP神经网络的具体原理此处不再介绍。
import numpy as np
class NeuralNetwork(object):
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
# Set number of nodes in input, hidden and output layers.设定输入层、隐藏层和输出层的node数目
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
# Initialize weights,初始化权重和学习速率
self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5,
( self.hidden_nodes, self.input_nodes))
self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5,
(self.output_nodes, self.hidden_nodes))
self.lr = learning_rate
# 隐藏层的激励函数为sigmoid函数,Activation function is the sigmoid function
self.activation_function = (lambda x: 1/(1 np.exp(-x)))
def train(self, inputs_list, targets_list):