周末根据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