import numpy
# scipy.special for the sigmoid function expit()
import scipy.special
# neural network class definition
class neuralNetwork:
# initialise the neural network
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
self.inodes = inputnodes #输入层节点数
self.hnodes = hiddennodes #隐藏层(中间层)节点数
self.onodes = outputnodes #输出层节点数
#随机生成输入层 、隐藏层、输出层两两之间的权重矩阵
self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
# learning rate 学习率
self.lr = learningrate
# activation function is the sigmoid function激活函数
self.activation_function = lambda x: scipy.special.expit(x)
pass
# train the neural network 训练神经网络
def train(self, inputs_list, targets_list):
# convert inputs list to 2d array 将输入的列表
inputs = numpy.array(inputs_list, ndmin=2).T
targets = numpy.array(targets_list, ndmin=2).T
# calculate signals into hidden layer 隐藏层输入输出
hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer 输出层输出输入
final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
final_outputs = self.activation_function(final_inputs)
# output layer error is the (target - actual)
output_errors = targets - final_outputs #误差
# hidden layer error is the output_errors, split by weights, recombined at hidden nodes 反向传播计算隐藏层输出误差
hidden_errors = numpy.dot(self.who.T, output_errors)
# update the weights for the links between the hidden and output layers 反向传播调整隐藏层和输出层参数
self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))
# update the weights for the links between the input and hidden layers 反向传播调整输入层和隐藏层权值参数
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))
pass
# query the neural network 计算输出 将输入的列表与权值矩阵做运算
def query(self, inputs_list):
# convert inputs list to 2d array
inputs = numpy.array(inputs_list, ndmin=2).T
# calculate signals into hidden layer
hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
final_outputs = self.activation_function(final_inputs)
return final_outputs
# number of input, hidden and output nodes
def getParameter(self):
print("input_nodes={},hidden_nodes={},output_nodes={}".format(self.inodes,self.hnodes,self.onodes))
print("权值矩阵1:",self.wih,'\n权值矩阵2:',self.who)
print("学习率:{},激活函数:{}".format(self.lr,self.activation_function))
print()
pass
if __name__ == "__main__":
input_nodes = 3
hidden_nodes = 3
output_nodes = 3
# learning rate is 0.3
learning_rate = 0.3
# create instance of neural network
three_layer_neural_network = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
three_layer_neural_network.getParameter()
print("训练后:")
three_layer_neural_network.train([1,2,3],[1,4,9])
three_layer_neural_network.getParameter()
three_layer_neural_network_2=neuralNetwork(4,3,5,0.6)
three_layer_neural_network_2.getParameter()
3*3神经网络
猜你喜欢
转载自blog.csdn.net/yu1336199790/article/details/103583814
今日推荐
周排行