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

遵循Tariq Rashid的指导,使用纯Python编写了一个三层神经网络,对MNIST手写数字识别进行训练。经过参数调优,该模型在测试集上的识别率达到了98%。调优过程包括特定的输入输出范围优化,不改变S曲线激活函数。最后,通过添加旋转图像训练,准确率提升至97.9%。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

周末根据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
评论 64
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值