import numpy import scipy.special class neuralNetwork: def __init__(self, inputnodes, hiddennodes, hidden2nodes, outputnodes, learningrate): self.inodes = inputnodes self.hnodes = hiddennodes self.hnodes2 = hidden2nodes self.onodes = outputnodes self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes)) self.wih2 = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes2, self.hnodes)) self.who = numpy.random.normal(0.0, pow(self.hnodes2, -0.5), (self.onodes, self.hnodes2)) self.lr = learningrate self.activation_function = lambda x: scipy.special.expit(x) pass def train(self, inputs_list, targets_list): inputs = numpy.array(inputs_list, ndmin=2).T targets = numpy.array(targets_list, ndmin=2).T hidden_inputs = numpy.dot(self.wih, inputs) hidden_outputs = self.activation_function(hidden_inputs) hidden_inputs2 = numpy.dot(self.wih2, hidden_outputs) hidden_outputs2 = self.activation_function(hidden_inputs2) final_inputs = numpy.dot(self.who, hidden_outputs2) final_outputs = self.activation_function(final_inputs) output_errors = targets - final_outputs hidden_errors = numpy.dot(self.who.T, output_errors) self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs2)) self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs)) self.wih2 += self.lr * numpy.dot((hidden_errors * hidden_outputs2 * (1.0 - hidden_outputs2)), numpy.transpose(hidden_outputs)) return final_outputs def query(self, inputs_list): inputs = numpy.array(inputs_list, ndmin=2).T hidden_inputs = numpy.dot(self.wih, inputs) hidden_outputs = self.activation_function(hidden_inputs) hidden_inputs2 = numpy.dot(self.wih2, hidden_outputs) hidden_outputs2 = self.activation_function(hidden_inputs2) final_inputs = numpy.dot(self.who, hidden_outputs2) final_outputs = self.activation_function(final_inputs) return final_outputs def wsave(self): numpy.save('w2.npy',self.wih) numpy.save('w1.npy',self.who) numpy.save('w3.npy',self.wih2) input_nodes = 784 # 图片大小 hidden_nodes = 200 # 读取数量 hidden2_nodes = 200 # 读取数量 output_nodes = 10 # 输出大小 learning_rate = 0.1 n = neuralNetwork(input_nodes,hidden_nodes, hidden2_nodes,output_nodes, learning_rate) training_data_file = open("mnist_train.csv", 'r') training_data_list = training_data_file.readlines() training_data_file.close() epochs = 1 for e in range(epochs): for record in training_data_list: all_values = record.split(',') inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 targets = numpy.zeros(output_nodes) + 0.01 targets[int(all_values[0])] = 0.99 data = n.train(inputs, targets) pass pass n.wsave() -------------------------------------------------------------------------- import numpy import scipy.special import cv2 data1 = numpy.load('w1.npy',fix_imports=True) data2 = numpy.load('w2.npy',fix_imports=True) data3 = numpy.load('w3.npy',fix_imports=True) activation_function = lambda x: scipy.special.expit(x) def query(inputs_list): inputs = numpy.array(inputs_list, ndmin=2).T hidden_inputs = numpy.dot(data2, inputs) hidden_outputs = activation_function(hidden_inputs) hidden_inputs2 = numpy.dot(data3, hidden_outputs) hidden_outputs2 = activation_function(hidden_inputs2) final_inputs = numpy.dot(data1, hidden_outputs2) final_outputs = activation_function(final_inputs) return final_outputs img = cv2.imread('my_own_images/2828_my_own_6.png',0) img_data = 255.0 - img.reshape(784) img_data = (img_data / 255.0 * 0.99) + 0.01 outputs = query(img_data) label = numpy.argmax(outputs) print("数字是: ", label)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)