博客
关于我
一个简单的神经网络数字识别实现(入门级)
阅读量:352 次
发布时间:2019-03-04

本文共 5761 字,大约阅读时间需要 19 分钟。

原文地址:

https://zhuanlan.zhihu.com/p/42174696

这里不做过多介绍,只用最快的方式实现简单的图像识别

代码如下:

import numpy as np# scipy.special for the sigmoid function expit()import scipy.special# library for plotting arraysimport matplotlib.pyplot# ensure the plots are inside this notebook, not an external window# helper to load data from PNG image filesimport imageio# neural network class definitionclass neuralNetwork:            # initialise the neural network    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):        # set number of nodes in each input, hidden, output layer        self.inodes = inputnodes        self.hnodes = hiddennodes        self.onodes = outputnodes                # link weight matrices, wih and who        # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer        # w11 w21        # w12 w22 etc         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 nodesinput_nodes = 784hidden_nodes = 200output_nodes = 10# learning ratelearning_rate = 0.1# create instance of neural networkn = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)training_data_file = open(r'C:\Users\dell\Desktop\train.csv')training_data_list = training_data_file.readlines()training_data_file.close()# train the neural network# epochs is the number of times the training data set is used for trainingepochs = 100#设置迭代次数for e in range(epochs):    # go through all records in the training data set    for record in training_data_list:        # split the record by the ',' commas        all_values = record.split(',')        # scale and shift the inputs        inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01        # create the target output values (all 0.01, except the desired label which is 0.99)        targets = numpy.zeros(output_nodes) + 0.01        # all_values[0] is the target label for this record        targets[int(all_values[0])] = 0.99        n.train(inputs, targets)# load image data from png files into an arrayprint ("loading ... my_own_images/2828_my_own_image.png")img_array = imageio.imread(r'C:\Users\dell\Desktop\6.png', as_gray=True)# reshape from 28x28 to list of 784 values, invert valuesimg_data  = 255.0 - img_array.reshape(784)#之所以要进行这一步处理是因为要去除背景,使得测试数据与训练数据的像素矩阵一致。    # then scale data to range from 0.01 to 1.0img_data = (img_data / 255.0 * 0.99) + 0.01print("min = ", numpy.min(img_data))print("max = ", numpy.max(img_data))# plot imagematplotlib.pyplot.imshow(img_data.reshape(28,28), cmap='Greys', interpolation='None')# query the networkoutputs = n.query(img_data)print (outputs)# the index of the highest value corresponds to the labellabel = numpy.argmax(outputs)print("network says ", label)#测试测试集的准确率#f=open(r'C:\Users\Administrator.119V3UR3EO4VMWZ\Desktop\test10.txt')#data=f.readlines()#f.close()##real=[]#for i in data:#    real.append(i[0])#y=[]#for i in data:#    i=i.split(',')#    outputs=n.query(numpy.asfarray(i[1:]))#    y.append(numpy.argmax(outputs))#正确率为70%

运行方式,首先将

training_data_file = open(r'C:\Users\dell\Desktop\train.csv')

改为自己存储训练数据的路径,训练数据的获取

通过epochs设置训练数据的迭代次数,

img_array = imageio.imread(r'C:\Users\dell\Desktop\6.png', as_gray=True)

将路径改为为测试图片的路径。图片格式要求:28×28.可通过windows自带的画图软件实现设置。

在Jupyter Notebook上运行效果如下:
在这里插入图片描述
我用的简化版数据。准确率算不上高
VSCode运行效果如下:
在这里插入图片描述
如果想要入门理解的话推荐观看
b站有搬运的,可以自行观看。

转载地址:http://aazh.baihongyu.com/

你可能感兴趣的文章
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
查看>>
NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
查看>>
NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
查看>>
NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
查看>>
NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
查看>>