用python创建的神经网络--mnist手写数字识别率达到98%

周末根据Tariq Rashid大神的指导,没有使用tensorflow等框架,用python编写了一个三层神经网络,并应用再mnist手写库识别上,经过多方面参数调优,识别率竟然达到了98%。  调优比较难,经验感觉特别宝贵,为避免时间长了忘记,记录整理如下。

目录

一、加载所需要的库

二、定义神经网络类

三、创建神经网络对象并用MNIST训练集训练

四、用测试集测试准确率

五、参数调优过程记录

六、测试下自己绘制的字体图片识别效果

七、特别优化:补充旋转图像的模型训练

具体过程记录

一、加载所需要的库

# Code for a 3-layer neural network, and code for learning the MNIST dataset
# Zhouxw@ebscn.com,2018.8  Studying to write neural network by python
# license is GPLv2

import numpy
# scipy.special for the sigmoid function expit()
import scipy.special
import matplotlib.pyplot
# ensure the plots are inside this jupyter notebook, not an external window
%matplotlib inline

# helper to load data from PNG image files
import imageio
# glob helps select multiple files using patterns
import glob

二、定义神经网络类

# neural network class definition (3 layers)
class 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
        # learning rate
        self.lr = learningrate
        
        # link weight matrices ,wih and who
        # weithg 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.hnodes,-0.5), (self.hnodes,self.inodes) )  )
        self.who = (numpy.random.normal(0.0, pow(self.onodes,-0.5), (self.onodes,self.hnodes) )  )
        
        # 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 = num
  • 59
    点赞
  • 669
    收藏
    觉得还不错? 一键收藏
  • 64
    评论
Mnist数字识别是一个经典的机器学习问题,通过训练模型来识别数字的应用。Python是一种流行的编程语言,通过Python开发图形用户界面(GUI)可以让用户更加友好地与程序交互。结合这两个方面,可以使用Python一个GUI界面用于mnist数字识别。 首先,我们可以使用Python中的Tkinter库或者PyQt等库来创建一个GUI界面,用户可以在界面上进行数字的输入。接着,我们将训练好的mnist数字识别模型加载到程序中,当用户在界面上绘制数字后,程序将自动识别用户输入的数字并显示在界面上。同时,还可以添加一些按钮用于清除输入或者重新识别等功能,增强用户体验。 在编代码时,我们需要将mnist数据集作为训练集,使用深度学习框架如TensorFlow或者PyTorch来训练一个数字识别模型。训练好的模型可以保存为.h5或者.pb文件,在GUI程序中加载该模型进行预测。另外,为了提高识别准确,可以使用数据增强、模型融合等技术进行优化。 此外,为了保证程序的性能,可以利用Python的并发编程或者多线程技术对图像识别过程进行加速。最后,通过打包工具将Python程序打包成可执行文件,用户可以直接双击运行程序,无需配置Python环境。 总的来说,通过Python GUI实现mnist数字识别可以让用户方便地进行数字识别,结合机器学习和图形界面编程的知识,可以开发出一款功能强大、易用的数字识别应用。
评论 64
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值