我自己根据《神经网络与深度学习实战》这本书的第五章写了一个python的程序,来识别mnist数据集,但是最后预报的结果只有0.092,书上却有0.94,我不清楚哪里有问题,希望大佬能指导一下,萌新入坑!
下面是代码
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator,FormatStrFormatter
import matplotlib.pyplot as plt
from numpy import *
from six.moves import xrange
import numpy
import scipy.special
class NeuralNetWork:
def __init__(self,inputnodes,hiddenodes,outputnodes,learningRate):
#初始化网络,设置输入层,中间层,输出层的节点数
self.input_nodes = inputnodes
self.hidden_nodes = hiddenodes
self.output_nodes = outputnodes
self.lr = learningRate
#初始化权重矩阵,有两个权重矩阵,一个是wih,表示输入层和中间层节点间链路权重形成的矩阵
#一个是who,表示中间层和输出层间链路权重形成的矩阵
self.wih = numpy.random.rand(self.hidden_nodes,self.input_nodes) - 0.5
self.who = numpy.random.rand(self.output_nodes,self.hidden_nodes) - 0.5
pass
def fit(self,inputs_list,targets_list):
#1.根据输入的训练数据更新节点的链路权重
#把inputs_list,targets_list转换成numpy支持的二维矩阵
#.T表示做矩阵的转置
inputs = numpy.array(inputs_list,ndmin=2).T
targets = numpy.array(targets_list,ndmin=2).T
#计算信号经过输入层后产生的信号量
hidden_inputs = numpy.dot(self.wih,inputs)
#中间层神经元对输入的信号做激活函数后得到输出信号
#sigmoid激活函数对应于scipy.special.expit(x)
sigmoid = lambda x:scipy.special.expit(x)
hidden_outputs = sigmoid(hidden_inputs)
#输出层接受来自中间层的信号量
final_inputs = numpy.dot(self.who,hidden_outputs)
#输出层对信号量做激活函数后得到最终输出信号
final_outputs = sigmoid(final_inputs)
#2.计算误差
output_errors = targets - final_outputs
#3.反向传播误差
hidden_errors = numpy.dot(self.who.T,output_errors)
#4.根据误差计算链路权重的更新量,然后把更新量加到原来的链路权重上
self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)),numpy.transpose(hidden_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),numpy.transpose(inputs))
pass
def evaluate(self,inputs):
#输入新数据,网络会给出对新数据的判断结果,inputs对应输入信号量
hidden_inputs = numpy.dot(self.wih,inputs)
#sigmoid激活函数对应scipy.special.expit(x)
sigmoid = lambda x:scipy.special.expit(x)
hidden_outputs = sigmoid(hidden_inputs)
#计算输出层神经元接收到的信号
final_inputs = numpy.dot(self.who,hidden_outputs)
#调用sigmoid函数计算最外层神经元输出信号
final_outputs = sigmoid(final_inputs)
print(final_outputs)
pass
input_nodes = 784
hidden_nodes = 100
output_nodes = 10
leraning_rate = 0.3
n = NeuralNetWork(input_nodes,hidden_nodes,output_nodes,leraning_rate)
from keras.datasets import mnist
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
from keras.utils import to_categorical
#将图片像素点值转换到取件[0,1]
train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32') / 25
test_images = test_images.reshape((10000,28*28))
test_images = test_images.astype('float32') / 255
#将标注转换为向量格式
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
#将训练图片与图片对应的标签配对后输入网络训练
for train_image,train_label in zip(train_images,train_labels):
n.fit(train_image,train_label)
scores = []
#使用测试数据检验网络训练结果没计算网络判断准确率
for test_image,test_label in zip(test_images,test_labels):
output = n.evaluate(test_image)
evaluate_label = numpy.argmax(output)
correct_label = numpy.argmax(test_label)
#预测正确添加1错误添加0,最后1的个数除以数值长度就得到准确率
if evaluate_label == correct_label:
scores.append(1)
else:
scores.append(0)
scores_array = numpy.asarray(scores)
print("performace = ",scores_array.sum() / scores_array.size)