经过一段时间的学习,终于要迎来这个激动人心的时刻了,我们要开始写我们自己的神经网络了!
不过在此之前还是先回顾一下之前所学到的知识。
回顾与复习
Python是一门比较简单的语言,本次实战就是用Python来写的,因此先要在电脑上安装好Python。
我们从最简单的感知机开始,渐渐地推广到多层的感知机。在引入了激活函数后,实现了从感知机到神经网络质的飞跃。然后我们开始了解神经网络的结构,深入地了解输出层是如何设计的,在此基础上又理解了信息是如何在神经网络中正向传递的。最后,我们了解了一些微积分的知识,学会了随机梯度下降法,明白了神经网络是如何学习数据的。
至此,神经网络的基本内容也就学习完毕了!
实战内容
MNIST数据集,这一个也被称为神经网络的“Hello World!”
该数据集的具体使用方法请看这里。
神经网络的设计
在开始敲代码之前,不妨还是冷静一下下。
来看看这个神经网络该如何设计,每一层该又多少个结点。
Python的实现
为了方便调用,也为了代码更加的简洁,不妨将神经网络写成一个类类型,并将其独立成一个模块,方便导入其他代码。
import numpy as np
from numpy.core.fromnumeric import ndim
#用于矩阵计算
import scipy.special
#用于计算激活函数S函数
import matplotlib.pyplot as plt
#用于画图
class neuralNetwork:
def __init__(self , input_node , output_node , hidden_node , learning_rate):
self.input_nodes = input_node
self.output_nodes = output_node
self.hidden_nodes = hidden_node
self.rate = learning_rate
self.weight_input_hidden = np.random.normal(0.0 , pow(self.input_nodes , -0.5) , (self.hidden_nodes , self.input_nodes))
self.weight_hidden_output = np.random.normal(0.0 , pow(self.hidden_nodes , -0.5) , (self.output_nodes , self.hidden_nodes))
self.active_function = lambda x:scipy.special.expit(x)
pass
def query(self , input_value):
inputs = np.array(input_value , ndmin = 2).T
#必须先将输入的一维数组转成二维数组之后才能进行转置
hidden_inputs = np.dot(self.weight_input_hidden , inputs)
hidden_outputs = self.active_function(hidden_inputs)
outputs = np.dot(self.weight_hidden_output , hidden_outputs)
return self.active_function(outputs)
pass
def train(self , input_value , output_value):
inputs = np.array(input_value , ndmin=2).T
results = np.array(output_value , ndmin=2).T
hidden_inputs = np.dot(self.weight_input_hidden , inputs)
hidden_outputs = self.active_function(hidden_inputs)
outputs = np.dot(self.weight_hidden_output , hidden_outputs)
outputs = self.active_function(outputs)
#计算当前神经网络的输出
output_error = results - outputs
hidden_error = np.dot(self.weight_hidden_output.T , output_error)
self.weight_hidden_output += np.dot(output_error * outputs * (1.0 - outputs) , np.array(hidden_outputs , ndmin=2).T) * self.rate
self.weight_input_hidden += np.dot(hidden_error * hidden_outputs * (1.0 - hidden_outputs) , np.array(inputs , ndmin=2).T) * self.rate
pass
这个neuralNetwork
类可以作为一个简单的三层神经网络的万能模板。
完整的源代码:下载
因此在进行MNIST实战中,我们仅仅需要声明一句NN = neuralNetwork(784 , 10 , 100 , 0.0005)
即可。
对于训练部分,下面仅给出一次训练的代码
for i in range(60000):
targets = np.zeros(10) + 0.01
targets[int(train_labels[i])] += 0.98
inputs = train_images[i].flatten()
inputs = inputs / 255.0 * 0.99 + 0.01
NN.train(inputs , targets)
pass
errors = check_all()
print("当前错误率为", 1.0 * errors / 100 , "%")
对于计算错误率部分,代码如下
def check_all():
errors = 0
for i in range(10000):
inputs = test_images[i].flatten()
inputs = inputs / 255.0 * 0.99 + 0.01
results = NN.query(inputs)
if np.argmax(results) != test_labels[i]:
#print(np.argmax(results))
#print(test_labels[i])
#plt.imshow(test_images[i], cmap='gray')
#plt.pause(0.000001)
#plt.show()
errors = errors + 1
print("当前错误率为", 1.0 * errors / 100 , "%")
return errors
pass