BP神经网络 - 数字识别

手写数字数据:

1797行数据,每一行64个灰度值,代表一个8*8像素的手写数字图片,如下图数字0:

BP神经网络 - 数字识别_第1张图片

数据加载如下:

from sklearn.datasets import load_digits

digits = load_digits()
X = digits.data #手写数字的数据(1797,64)
label = digits.target #标签值

 

手写数字识别的实现:

import numpy as np
from sklearn.datasets import load_digits
from sklearn.preprocessing import LabelBinarizer
from sklearn.cross_validation import train_test_split

def sigmoid(x):
    return 1/(1+np.exp(-x))

def dsigmoid(x):
    return x*(1-x)

class NeuralNetwork:
    def __init__(self,layers):#构造方法在类实例化时会自动调用,(64,100,10)
       
        #输入层和隐藏层之间权值的初始化,范围(-1,1)
        #random()返回(0,1)之间的实数
        self.V = np.random.random((layers[0]+1,layers[1]+1))*2-1
        #隐藏层和输出层之间权值的初始化,范围(-1,1) 
        self.W = np.random.random((layers[1]+1,layers[2]))*2-1
        
    def train(self,X,label,lr=0.11,epochs=10000):
        #为训练数据添加偏置
        #构造出一个X.shape[0]行,X.shape[1]+1列的矩阵,用1填充
        temp = np.ones([X.shape[0],X.shape[1]+1])
        #把temp矩阵中所有行,1到倒数第2列用X替换,此时temp矩阵即为添加偏置后的矩阵
        temp[:,0:-1] = X
        X = temp
        
        for n in range(epochs+1):
            i = np.random.randint(X.shape[0])#随机选取一行数据
            x = X[i]
            x = np.atleast_2d(x)#转为二维数据
            
            OH = sigmoid(np.dot(x,self.V)) #隐藏层输出
            OO = sigmoid(np.dot(OH,self.W)) #输出层的输出
            
            OO_delta = (label[i]-OO)*dsigmoid(OO) #输出层的误差信号
            OH_delta = OO_delta.dot(self.W.T)*dsigmoid(OH) #隐藏层的误差信号
            
            #权值的更新
            self.W += lr*OH.T.dot(OO_delta)
            self.V += lr*x.T.dot(OH_delta)
            
            #每训练1000次预测一次准确率
            if n%500==0:
                predictions = [] #存储预测结果
                for j in range(X_test.shape[0]):
                    o = self.predict(X_test[j]) #测试的输出结果
                    predictions.append(np.argmax(o))#获取预测结果,argmax()获取最大数位置
                accuracy = np.mean(np.equal(predictions,label_test))
                print ('epoch',n,'accuracy',accuracy)
    
    def predict(self,x):
        #为测试数据添加偏置
        temp = np.ones(x.shape[0]+1)
        temp[0:-1] = x
        x = temp
        x = np.atleast_2d(x)
        
        OH = sigmoid(np.dot(x,self.V))
        OO = sigmoid(np.dot(OH,self.W))
        return OO
        
digits = load_digits()
X = digits.data
label = digits.target

# 输入数据归一化,将数据[0,255]缩小到[0,1]
# 归一化的原因:
# 当输入过大,根据sigmoid函数,x越大,图像的导数(梯度)会逼近0,影响梯度下降法对权值调整的效果
X -= X.min()
X /= X.max()

BPN = NeuralNetwork([64,100,10])#创建实例,初始化权值

#分割数据1/4为测试数据,3/4为训练数据
X_train,X_test,label_train,label_test = train_test_split(X,label)

#标签二值化,0->1000000000,1->0100000000,2->0010000000,...
labels_train = LabelBinarizer().fit_transform(label_train)
labels_test = LabelBinarizer().fit_transform(label_test)

print('start')
BPN.train(X_train,labels_train,epochs=20000)
print('end')

运行结果:

start
epoch 0 accuracy 0.051111111111111114
epoch 500 accuracy 0.37333333333333335
epoch 1000 accuracy 0.43555555555555553
epoch 1500 accuracy 0.5622222222222222
epoch 2000 accuracy 0.68
epoch 2500 accuracy 0.7711111111111111
epoch 3000 accuracy 0.8111111111111111
epoch 3500 accuracy 0.9222222222222223
epoch 4000 accuracy 0.9177777777777778
epoch 4500 accuracy 0.9266666666666666
epoch 5000 accuracy 0.9422222222222222
epoch 5500 accuracy 0.9466666666666667
epoch 6000 accuracy 0.9444444444444444
epoch 6500 accuracy 0.9422222222222222
epoch 7000 accuracy 0.9511111111111111
epoch 7500 accuracy 0.9533333333333334
epoch 8000 accuracy 0.9466666666666667
epoch 8500 accuracy 0.9488888888888889
epoch 9000 accuracy 0.9555555555555556
epoch 9500 accuracy 0.9533333333333334
epoch 10000 accuracy 0.9577777777777777
epoch 10500 accuracy 0.9533333333333334
epoch 11000 accuracy 0.9488888888888889
epoch 11500 accuracy 0.9488888888888889
epoch 12000 accuracy 0.96
epoch 12500 accuracy 0.9511111111111111
epoch 13000 accuracy 0.9488888888888889
epoch 13500 accuracy 0.9577777777777777
epoch 14000 accuracy 0.9577777777777777
epoch 14500 accuracy 0.9555555555555556
epoch 15000 accuracy 0.96
epoch 15500 accuracy 0.9555555555555556
epoch 16000 accuracy 0.9555555555555556
epoch 16500 accuracy 0.9622222222222222
epoch 17000 accuracy 0.96
epoch 17500 accuracy 0.9622222222222222
epoch 18000 accuracy 0.9622222222222222
epoch 18500 accuracy 0.9577777777777777
epoch 19000 accuracy 0.9622222222222222
epoch 19500 accuracy 0.9644444444444444
epoch 20000 accuracy 0.9666666666666667
end

 

  详细 X
  没有英汉互译结果
   请尝试网页搜索

你可能感兴趣的:(python3,神经网络)