准备

这个过程非常简单,就是用到了很多的矩阵运算。

  • 训练数据集下载地址,
  • 测试数据集下载地址,

数据格式

.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()

效果图展示

神经网络代码

  1. 对象
import numpy
import scipy.specialclass NeuralNetWork:# 初始化def __init__(self, inputnodfes, hiddennodes, outputnodes, learningrate):self.innodes = inputnodfesself.hnodes = hiddennodesself.onodes = outputnodesself.lr = learningrateself.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).Ttargets = numpy.array(targets_list, ndmin=2).Thidden_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_outputshidden_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).Thidden_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
  1. 训练和测试代码
import numpyfrom neuralNetwork import NeuralNetWorkinput_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.01targets = numpy.zeros(output_nodes) + 0.01targets[int(all_values[0])] = 0.99b.train(inputs, targets)passprint("训练完成")
# 使用测试数据测试准确性
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.01outputs = b.query(inputs)label = numpy.argmax(outputs)if label == correct_label:scorecard.append(1)else:scorecard.append(0)passpassscorecard_array = numpy.asarray(scorecard)
print("准确率", scorecard_array.sum() / scorecard_array.size)

结果

最后运行的结果显示准确率达到94%

简单python代码实现三层神经网络识别手写数字相关推荐

  1. 用三层神经网络识别手写数字

    前两章讲了搭建简单的三层神经网络和用三层神经网络识别mnist数据集.识别mnist数据集时有人已经把数字整理成像素值,我们只需要把像素值的大小调整下就可以当做输入值传入神经网络中,但是如果给我们一张 ...

  2. 四、用简单神经网络识别手写数字(内含代码详解及订正)

    本博客主要内容为图书<神经网络与深度学习>和National Taiwan University (NTU)林轩田老师的<Machine Learning>的学习笔记,因此在全 ...

  3. Python神经网络识别手写数字-MNIST数据集

    Python神经网络识别手写数字-MNIST数据集 一.手写数字集-MNIST 二.数据预处理 输入数据处理 输出数据处理 三.神经网络的结构选择 四.训练网络 测试网络 测试正确率的函数 五.完整的 ...

  4. 利用python实现简单的人工神经网络识别手写数字

    利用 Python 搭建起了一个简单的神经网络模型,并完成识别手写数字. 1.前置工作 1.1 环境配置 这里使用scikit-learn库内建的手写数字字符集作为本文的数据集.scikit-lear ...

  5. python手机代码识别数字_利用python构建神经网络识别手写数字(附源代码)

    一.运行环境配置 本次实验的运行环境win10(bit64),采用python环境为3.7.6,安装Python环境推荐使用Anaconda.Anaconda是一个免费开源的Python和R语言的发行 ...

  6. opencv(python)使用ann神经网络识别手写数字

    opencv中也提供了一种类似于Keras的神经网络,即为ann,这种神经网络的使用方法与Keras的很接近. 关于mnist数据的解析,读者可以自己从网上下载相应压缩文件,用python自己编写解析 ...

  7. BP神经网络识别手写数字项目解析及代码

    这两天在学习人工神经网络,用传统神经网络结构做了一个识别手写数字的小项目作为练手.点滴收获与思考,想跟大家分享一下,欢迎指教,共同进步. 平常说的BP神经网络指传统的人工神经网络,相比于卷积神经网络( ...

  8. opencv(python)使用knn最近邻算法识别手写数字

    knn最近邻算法是一种分类以及回归算法,算法原理是一个样本与样本集中k个样本最相似,如果这k个样本的大多数也属于同一个类别,则该样本也属于这一类.关于knn算法的详细原理读者可以在网上找一些资料了解下 ...

  9. 第1章使用神经网络识别手写数字

    人类视觉系统是世界奇观之一.考虑以下手写数字序列: 大多数人毫不费力地将这些数字识别为504192.这很容易就是欺骗性的.在我们大脑的每个半球,人类有一个主要的视觉皮层,也被称为V1,包含1.4亿个神 ...

最新文章

  1. 《Sibelius 脚本程序设计》连载(十四) - 2.1 注释、语句、语句块
  2. 这五部关于海洋的纪录片,每一帧都犹如壁纸!
  3. 一条数据的HBase之旅,简明HBase入门教程1:开篇
  4. Alphabet旗下自动驾驶部门Waymo将在匹兹堡设立办公室
  5. 为什么每次开机第一次启动程序会很慢?
  6. IPv6的地址表达形式
  7. 移动APP之专项测试
  8. 光猫(吉比特h2-2)超级用户名与密码
  9. freeSSL申请证书加阿里云域名解析加nginx配置
  10. 华为鸿蒙系统支持什么手机_华为鸿蒙系统支持的手机型号_鸿蒙系统支持华为哪几款手机...
  11. 使用JAVA编程实现多人聊天室(多线程实践)
  12. DNS有哪两种域名解析方式?简述这两种方式区别和特点。
  13. 【百科】详解阿里云技术核心——飞天
  14. 使用Python解数学方程
  15. 【文献阅读2020】 像素级自适应学习的超分辨率Pixel-Level Self-Paced Learning For Super-Resolution
  16. 逻辑推理题:谁是凶手
  17. [精选] 统计在线人数,用php 如何来实现 ?
  18. 万字长文回顾智能驾驶进化史
  19. android开发:Theme.Light.NoTitleBar和Theme.Light.NoTitleBar.Fullscreen的区别
  20. 胡扯推荐算法(协同)及其dome实现

热门文章

  1. 上海交大计算机考研招生名额,上海交大考研录取名额 考研报考人数
  2. AndroidX Media3之ExoPlayer简单使用(1)
  3. E:大疆M300二次开发PSDKV2.1.0。无法识别无人机型号。一直出现 aircraft type 0
  4. 软件架构模式导读(转)
  5. 数据分析-matplotlib-绘制股价图
  6. sniffer经典指南 二
  7. shell 变量保护 引号的作用
  8. synchronized 锁的底层原理
  9. 解决 Ajax:Ensure CORS response header values are valid 跨域问题
  10. 【UE4】关于时间计数异常缓慢的问题