贴个代码,数据集是mnist,可以直接去官网下载。
#main.py import os from load_file import load_mnist from neuralNetwork import neuralNetwork if __name__ == '__main__': train_images_data, train_labels = load_mnist(os.getcwd(), kind='train') test_images_data, test_labels = load_mnist(os.getcwd(), kind='t10k') learningrate = 0.1 ls = [28*28, 30, 10] Network = neuralNetwork(2, ls, learningrate) for i in range(10): if i%5 == 0: Network.learningrate /= 2 for j in range(len(train_images_data)): print("第" + str(i) + "轮" + "-------正在进行训练-------" + str(j)) Network.update(train_images_data[j], train_labels[j]) count = 0 for i in range(len(test_images_data)): print('-------正在进行测试--------') if Network.test(test_images_data[i], test_labels[i]): count += 1 print(count / len(test_labels)) #neuralNetwork.py import numpy as np from activateFunction import activate_function class neuralNetwork: def __init__(self, numNeuralLayers, numNeurons_perLayer, learningrate): self.numBeuralLayers = numNeuralLayers self.numNeurons_perLayer = numNeurons_perLayer self.learningrate = learningrate self.W = [] for i in range(self.numBeuralLayers): #高斯分布进行初始化 self.W.append(np.random.randn(numNeurons_perLayer[i + 1], numNeurons_perLayer[i]) / np.sqrt(numNeurons_perLayer[i])) def update(self, inputs, targets): inputs = np.array(inputs, ndmin = 2).T targets = np.array(targets, ndmin = 2).T outputs = [] outputs.append(inputs) #前向传播 for i in range(self.numBeuralLayers): a = np.dot(self.W[i], outputs[i]) z = activate_function(a) outputs.append(z) #计算损失 self.loss = [] for i in range(self.numBeuralLayers): if i == 0: self.loss.append(targets - outputs[-1]) else: self.loss.append(np.dot(self.W[self.numBeuralLayers - i].T, self.loss[i - 1])) #反向传播 for i in range(self.numBeuralLayers): self.W[self.numBeuralLayers - i - 1] += self.learningrate * np.dot(self.loss[i] * outputs[self.numBeuralLayers - i] * (1 -outputs[self.numBeuralLayers -i]), outputs[self.numBeuralLayers - i - 1].T) #测试 def test(self, test_inputs, test_targets): inputs = test_inputs.T for i in range(self.numBeuralLayers): a = np.dot(self.W[i], inputs) z = activate_function(a) inputs = z return list(inputs).index(max(list(inputs))) == list(test_targets).index(1) #activateFunction.py import numpy as np def activate_function(x): #sigmoid return 1.0 / (1.0 + np.exp(-x)) #load_file.py import os import struct import numpy as np def load_mnist(path, kind = 'train'): labels_path = os.path.join(path, '%s-labels.idx1-ubyte'% kind) images_path = os.path.join(path, '%s-images.idx3-ubyte'% kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II',lbpath.read(8)) labels = np.fromfile(lbpath, dtype = np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII', imgpath.read(16)) images = np.fromfile(imgpath,dtype = np.uint8).reshape(len(labels), 784) ls = [] for i in range(labels.size): temp = np.zeros(10) temp[labels[i]] = 1 ls.append(temp.T) labels = np.array(ls) return (images / 2550), labels