一、认识BP神经网络
BP神经网络是最基础的神经网络,其输出结果采用前向传播,误差采用反向(Back Propagation)传播方式进行。在我看来BP神经网络就是一个”万能的模型+误差修正函数“,每次根据训练得到的结果与预想结果进行误差分析,进而修改权值和阈值,一步一步得到能输出和预想结果一致的模型。举一个例子:比如某厂商生产一种产品,投放到市场之后得到了消费者的反馈,根据消费者的反馈,厂商对产品进一步升级,优化,从而生产出让消费者更满意的产品。这就是BP神经网络的核心。 看这样一个问题,假如我手里有一笔钱,N个亿吧,我把它分别投给5个公司,分别占比 M1,M2,M3,M4,M5(M1到M5均为百分比 %)。而每个公司的回报率是不一样的,分别为 A1, A2, A3, A4, A5,(A1到A5也均为百分比 %)那么我的收益应该是多少?这个问题看起来应该是够简单了,你可能提笔就能搞定 收益 = NM1A1 + NM2A2+NM3A3+NM4A4+NM5A5 。这个完全没错,但是体现不出水平,我们可以把它转化成一个网络模型来进行说明。如下图:
————————————————
?
上面的问题是不是莫名其妙的就被整理成了一个三层的网络,N1到N5表示每个公司获得的钱,R表示最终的收益。R = NM1A1 + NM2A2+NM3A3+NM4A4+NM5A5 。我们可以把 N 作为输入层 ,R作为输出层,N1到N5则整体作为隐藏层,共三层。而M1到M5则可以理解为输入层到隐藏层的权重,A1到A5为隐藏层到输出层的权重。
这里提到了四个重要的概念 输入层(input) , 隐藏层 (hidden),输出层(output)和权重(weight) 。而所有的网络都可以理解为由这三层和各层之间的权重组成的网络,只是隐藏层的层数和节点数会多很多。
输入层:信息的输入端,上图中 输入层 只有 1 个节点(一个圈圈),实际的网络中可能有很多个
隐藏层:信息的处理端,用于模拟一个计算的过程,上图中,隐藏层只有一层,节点数为 5 个。
输出层:信息的输出端,也就是我们要的结果,上图中,R 就是输出层的唯一一个节点,实际上可能有很多个输出节点。
权重:连接每层信息之间的参数,上图中只是通过乘机的方式来体现。
在上面的网络中,我们的计算过程比较直接,用每一层的数值乘以对应的权重。这一过程中,权重是恒定的,设定好的,因此,是将 输入层N 的 信息 ,单向传播到 输出层R 的过程,并没有反向传播信息,因此它不是神经网络,只是一个普通的网络。
而神经网络是一个信息可以反向传播的网络,而最早的Bp网络就是这一思想的体现。先不急着看Bp网络的结构,看到这儿你可能会好奇,反向传播是什么意思。再来举一个通俗的例子,猜数字: 当我提前设定一个数值 50,让你来猜,我会告诉你猜的数字是高了还是低了。你每次猜的数字相当于一次信息正向传播给我的结果,而我给你的提示就是反向传播的信息,往复多次,你就可以猜到我设定的数值 50 。 这就是典型的反向传播,即根据输出的结果来反向的调整模型,只是在实际应用中的Bp网络更为复杂和数学,但是思想很类似。 二、
2.1BP神经网络结构与原理
由于 BP 神经网络参数超级多,如果不先定义好变量,后面非常难理解,故针对上述图形,定义如下:?
?
?
?
?2.2BP神经网络完整流程
?
三、BP神经网络的实现
(1)BP 神经网络的第一种实现
采用误差平方和作为损失函数,基于反向传播算法推导,可得最终的 4 个方程式,如下:
?前向计算
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation) + b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
反向传播
# backward pass 计算最后一层的误差
delta = self.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# 计算从倒数第二层至第二层的误差
for l in range(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l + 1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l - 1].transpose())
(2)BP神经网络第二种实现
交叉熵代价函数:
(1) 引入交叉熵代价函数目的是解决一些实例在刚开始训练时学习得非常慢的问题,其 主要针对激活函数为 Sigmod 函数 (2) 如果采用一种不会出现饱和状态的激活函数,那么可以继续使用误差平方和作为损 失函数 (3) 如果在输出神经元是 S 型神经元时 , 交叉熵 一 般都是更好的选择 (4) 输出神经元是线性的那么二次代价函数不再会导致学习速度下降的问题。在此情形 下,二次代价函数就是一种合适的选择 (5) 交叉熵无法改善隐藏层中神经元发生的学习缓慢 (6) 交叉熵损失函数只对网络输出“ 明显背离预期” 时发生的学习缓慢有改善效果 (7) 应用交叉熵损失并不能改善或避免神经元饱和 ,而是当输出层神经元发生饱和时, 能够避免其学习缓慢的问题。 实现代码:
#### Libraries
# Standard library
import json
import random
import sys
# Third-party libraries
import numpy as np
#### Define the quadratic and cross-entropy cost functions
import mnist_loader
class QuadraticCost(object): # 误差平方和代价函数
@staticmethod
def fn(a, y):
return 0.5*np.linalg.norm(a-y)**2
@staticmethod
def delta(z, a, y):
"""Return the error delta from the output layer."""
return (a-y) * sigmoid_prime(z)
class CrossEntropyCost(object): # 交叉熵代价函数
@staticmethod
def fn(a, y):
return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a))) # 使用0代替nan 一个较大值代替inf
@staticmethod
def delta(z, a, y):
return (a-y)
#### Main Network class
class Network(object):
def __init__(self, sizes, cost=CrossEntropyCost): #采用交叉熵代价函数
self.num_layers = len(sizes)
self.sizes = sizes
self.default_weight_initializer()
self.cost=cost
def default_weight_initializer(self): # 推荐的权重初始化方式
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]] # 偏差初始化方式不变
self.weights = [np.random.randn(y, x)/np.sqrt(x) # 将方差减少,避免饱和
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
def large_weight_initializer(self): # 不推荐的初始化方式
self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(self.sizes[:-1], self.sizes[1:])]
def feedforward(self, a):
"""Return the output of the network if ``a`` is input."""
for b, w in zip(self.biases, self.weights):
a = sigmoid(np.dot(w, a)+b)
return a
def SGD(self, training_data, epochs, mini_batch_size, eta,
lmbda = 0.0,
evaluation_data=None,
monitor_evaluation_cost=False,
monitor_evaluation_accuracy=False,
monitor_training_cost=False,
monitor_training_accuracy=False,
early_stopping_n = 0):
# early stopping functionality:
best_accuracy=1
training_data = list(training_data)
n = len(training_data)
if evaluation_data:
evaluation_data = list(evaluation_data)
n_data = len(evaluation_data)
# early stopping functionality:
best_accuracy=0
no_accuracy_change=0
evaluation_cost, evaluation_accuracy = [], []
training_cost, training_accuracy = [], []
for j in range(epochs):
random.shuffle(training_data)
mini_batches = [
training_data[k:k+mini_batch_size]
for k in range(0, n, mini_batch_size)]
for mini_batch in mini_batches:
self.update_mini_batch(
mini_batch, eta, lmbda, len(training_data))
print("Epoch %s training complete" % j)
if monitor_training_cost:
cost = self.total_cost(training_data, lmbda)
training_cost.append(cost)
print("Cost on training data: {}".format(cost))
if monitor_training_accuracy:
accuracy = self.accuracy(training_data, convert=True)
training_accuracy.append(accuracy)
print("Accuracy on training data: {} / {}".format(accuracy, n))
if monitor_evaluation_cost:
cost = self.total_cost(evaluation_data, lmbda, convert=True)
evaluation_cost.append(cost)
print("Cost on evaluation data: {}".format(cost))
if monitor_evaluation_accuracy:
accuracy = self.accuracy(evaluation_data)
evaluation_accuracy.append(accuracy)
print("Accuracy on evaluation data: {} / {}".format(self.accuracy(evaluation_data), n_data))
# Early stopping:
if early_stopping_n > 0: # 如果采用了早停止策略,则当准确率超过设定的次数依然没有改变,则停止训练
if accuracy > best_accuracy:
best_accuracy = accuracy
no_accuracy_change = 0
print("Early-stopping: Best so far {}".format(best_accuracy))
else:
no_accuracy_change += 1
if (no_accuracy_change == early_stopping_n):
print("Early-stopping: No accuracy change in last epochs: {}".format(early_stopping_n))
return evaluation_cost, evaluation_accuracy, training_cost, training_accuracy
return evaluation_cost, evaluation_accuracy, \
training_cost, training_accuracy
def update_mini_batch(self, mini_batch, eta, lmbda, n):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [(1-eta*(lmbda/n))*w-(eta/len(mini_batch))*nw # 带L2范数的权重更新
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]
def backprop(self, x, y):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = (self.cost).delta(zs[-1], activations[-1], y) # 这里和以前不一样,其他地方一样
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
# Note that the variable l in the loop below is used a little
# differently to the notation in Chapter 2 of the book. Here,
# l = 1 means the last layer of neurons, l = 2 is the
# second-last layer, and so on. It's a renumbering of the
# scheme in the book, used here to take advantage of the fact
# that Python can use negative indices in lists.
for l in range(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)
def accuracy(self, data, convert=False):
if convert: # 训练集使用-由于训练集的输出是独热码,而其他数据不是
results = [(np.argmax(self.feedforward(x)), np.argmax(y))
for (x, y) in data]
else: # 验证集合测试集使用
results = [(np.argmax(self.feedforward(x)), y)
for (x, y) in data]
result_accuracy = sum(int(x == y) for (x, y) in results)
return result_accuracy
def total_cost(self, data, lmbda, convert=False):
cost = 0.0
for x, y in data:
a = self.feedforward(x)
if convert: y = vectorized_result(y) # 测试集和验证集需要向量化
cost += self.cost.fn(a, y)/len(data) # 带L2范数的代价函数
cost += 0.5*(lmbda/len(data))*sum(np.linalg.norm(w)**2 for w in self.weights) # '**' - to the power of.
return cost
def save(self, filename):
data = {"sizes": self.sizes,
"weights": [w.tolist() for w in self.weights],
"biases": [b.tolist() for b in self.biases],
"cost": str(self.cost.__name__)}
f = open(filename, "w")
json.dump(data, f)
f.close()
#### Loading a Network
def load(filename):
f = open(filename, "r")
data = json.load(f)
f.close()
cost = getattr(sys.modules[__name__], data["cost"])
net = Network(data["sizes"], cost=cost)
net.weights = [np.array(w) for w in data["weights"]]
net.biases = [np.array(b) for b in data["biases"]]
return net
#### Miscellaneous functions
def vectorized_result(j):
e = np.zeros((10, 1))
e[j] = 1.0
return e
def sigmoid(z):
"""The sigmoid function."""
return 1.0/(1.0+np.exp(-z))
def sigmoid_prime(z):
"""Derivative of the sigmoid function."""
return sigmoid(z)*(1-sigmoid(z))
if __name__ == '__main__':
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
training_data = list(training_data)
net = Network([784, 30, 10], cost=CrossEntropyCost)
# net.large_weight_initializer()
net.SGD(training_data, 30, 10, 0.1, lmbda=5.0, evaluation_data=validation_data,
monitor_evaluation_accuracy=True)
|