三层BP神经网络应用-OptDigits数据集

测试结果:

源码:

# coding=gbk

import numpy as np
import random

#激活函数sigmoid
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

#设置学习率
eta = 0.001

#设置学习次数
times = 1000

#计算正确率
def accuasy_score(test_true, test_predict):
    return sum(test_true == test_predict)/len(test_true)

#导入数据
data_train = np.loadtxt('optdigits.tra',delimiter=',',dtype=float,usecols=np.arange(0,64))
data_train_index = np.loadtxt('optdigits.tra',delimiter=',',dtype=int,usecols=64)
data_test = np.loadtxt('optdigits.tes',delimiter=',',dtype=float,usecols=np.arange(0,64))
data_test_index = np.loadtxt('optdigits.tes',delimiter=',',dtype=int,usecols=64)

#原数据中有很多0对其处理
data_train = data_train / 16 * 0.99 + 0.01
data_test = data_test /16 * 0.99 + 0.01

oneHot = np.identity(10)
for i in range(oneHot.shape[0]):
    for j in range(oneHot.shape[1]):
        if(oneHot[i][j] == 1):
            oneHot[i][j] = 0.99
        else:
            oneHot[i][j] = 0.01
#处理之后用来训练的训练集真实下标
data_train_index_true = oneHot[data_train_index]

#定义初始随机权重矩阵
W1 = np.random.normal(0.0, 1, (64,128))
W2 = np.random.normal(0.0, 1, (128,10))

#开始前向传播过程
for i in range(times):
    out1 = np.dot(data_train, W1)
    act1 = sigmoid(out1)
    #act1为隐藏层的输出,作为输出层的输入
    out2 = np.dot(act1, W2)
    act2 = sigmoid(out2)
    #误差的计算
    error = data_train_index_true - act2
    hErr = np.dot(error, W2.T)
    #后向传播的过程
    W2_ = np.dot(act1.T, -error*act2*(1-act2))
    W1_ = np.dot(data_train.T, -hErr*act1*(1-act1))
    W2 -= W2_*eta
    W1 -= W1_*eta

#预测测试集中数据的类别
o1 = np.dot(data_test, W1)
a1 = sigmoid(o1)
o2 = np.dot(a1, W2)
a2 = sigmoid(o2)

#最终的输出结果
rs = []
for i in range(a2.shape[0]):
    rs.append(np.argmax(a2[i]))
rs = np.array(rs)

print("正确率是"+ str(round((accuasy_score(data_test_index, rs)*100),2))+"%")

 

THE END
分享
二维码
海报
三层BP神经网络应用-OptDigits数据集
测试结果: 源码: # coding=gbk import numpy as np import random #激活函数sigmoid def sigmoid(x): return 1 / (1 + np.exp(-x)) #设置……