简单python代码实现三层神经网络识别手写数字
程序员文章站
2022-03-10 15:32:31
...
准备
这个过程非常简单,就是用到了很多的矩阵运算。
数据格式
.csv格式数据的每一行都是一个28*28像素的手写数字图片,每一行的第一个像素是数字的值,从第二个数字开始时像素值
import matplotlib.pyplot
import pylab
import numpy
# 读入训练数据
training_data_file = open("G:\神经网络数据\mnist_train.csv", "r")
training_data_list = training_data_file.readlines()
training_data_file.close()
# 图片展示
aist = training_data_list[1].split(",")
aist = numpy.asfarray(aist[1:]).reshape((28, 28))
matplotlib.pyplot.imshow(aist, interpolation="nearest")
pylab.show()
效果图展示
神经网络代码
- 对象
import numpy
import scipy.special
class NeuralNetWork:
# 初始化
def __init__(self, inputnodfes, hiddennodes, outputnodes, learningrate):
self.innodes = inputnodfes
self.hnodes = hiddennodes
self.onodes = outputnodes
self.lr = learningrate
self.wih = numpy.random.normal(0.0, pow(self.innodes, -0.5), (self.hnodes, self.innodes))
self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))
# 抑制函数
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)
final_inputs = numpy.dot(self.who, hidden_outputs)
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_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),
numpy.transpose(inputs))
pass
# 测试
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)
final_inputs = numpy.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
- 训练和测试代码
import numpy
from neuralNetwork import NeuralNetWork
input_nodes = 784
hidden_nodes = 100
output_nodes = 10
learning_rate = 0.2
# 读入训练数据
training_data_file = open("G:\神经网络数据\mnist_train.csv", "r")
training_data_list = training_data_file.readlines()
training_data_file.close()
# 初始化神经网络
b = NeuralNetWork(input_nodes, hidden_nodes, output_nodes, learning_rate)
for record in training_data_list:
all_values = record.split(',')
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.9) + 0.01
targets = numpy.zeros(output_nodes) + 0.01
targets[int(all_values[0])] = 0.99
b.train(inputs, targets)
pass
print("训练完成")
# 使用测试数据测试准确性
test_data_file = open("G:\神经网络数据\mnist_test.csv", "r")
test_data_list = test_data_file.readlines()
test_data_file.close()
# 使用算法逐项对比测试数据是否准确,然后统计
scorecard = []
for record in test_data_list:
all_values = record.split(',')
correct_label = int(all_values[0])
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
outputs = b.query(inputs)
label = numpy.argmax(outputs)
if label == correct_label:
scorecard.append(1)
else:
scorecard.append(0)
pass
pass
scorecard_array = numpy.asarray(scorecard)
print("准确率", scorecard_array.sum() / scorecard_array.size)
结果
最后运行的结果显示准确率达到94%
推荐阅读