[转载]机器学习(4):BP神经网络原理及其python实现

原文链接:http://www.cnblogs.com/cv-pr/p/7118548.html

BP神经网络是深度学习的重要基础,它是深度学习的重要前行算法之一,因此理解BP神经网络原理以及实现技巧非常有必要。接下来,我们对原理和实现展开讨论。

1.原理

有空再慢慢补上,请先参考老外一篇不错的文章:A Step by Step Backpropagation Example

激活函数参考:深度学习常用激活函数之— Sigmoid & ReLU & Softmax

浅显易懂的初始化:CS231n课程笔记翻译:神经网络笔记 2

有效的Trick:神经网络训练中的Tricks之高效BP(反向传播算法)

通过简单演示BPNN的计算过程:一文弄懂神经网络中的反向传播法——BackPropagation

2.实现----Batch随机梯度法

这里实现了层数可定义的BP神经网络,可通过参数net_struct进行定义网络结果,如定义只有输出层,没有隐藏层的网络结构,激活函数为”sigmoid",学习率,可如下定义

net_struct = [[10,"sigmoid",0.01]]#网络结构

如定义一层隐藏层为100个神经元,再接一层隐藏层为50个神经元,输出层为10个神经元的网络结构,如下

net_struct = [[100,"sigmoid",0.01],[50,"sigmoid",0.01],[10,"sigmoid",0.01]]#网络结构

码农最爱的实现如下:

# # encoding=utf8

'''

Created on 2017-7-3

@author: Administrator

'''

import random

import pandas as pd

import numpy as np

from matplotlib import pyplot as plt

from sklearn.model_selection import train_test_split as ttsplit

class LossFun:

def __init__(self, lf_type="least_square"):

self.name = "loss function"

self.type = lf_type

def cal(self, t, z):

loss = 0

if self.type == "least_square":

loss = self.least_square(t, z)

return loss

def cal_deriv(self, t, z):

delta = 0

if self.type == "least_square":

delta = self.least_square_deriv(t, z)

return delta

def least_square(self, t, z):

zsize = z.shape

sample_num = zsize[1]

return np.sum(0.5 * (t - z) * (t - z) * t) / sample_num

def least_square_deriv(self, t, z):

return z - t

class ActivationFun:

'''

激活函数

'''

def __init__(self, atype="sigmoid"):

self.name = "activation function library"

self.type = atype;

def cal(self, a):

z = 0

if self.type == "sigmoid":

z = self.sigmoid(a)

elif self.type == "relu":

z = self.relu(a)

return z

def cal_deriv(self, a):

z = 0

if self.type == "sigmoid":

z = self.sigmoid_deriv(a)

elif self.type == "relu":

z = self.relu_deriv(a)

return z

def sigmoid(self, a):

return 1 / (1 + np.exp(-a))

def sigmoid_deriv(self, a):

fa = self.sigmoid(a)

return fa * (1 - fa)

def relu(self, a):

idx = a <= 0

a[idx] = 0.1 * a[idx]

return a  # np.maximum(a, 0.0)

def relu_deriv(self, a):

# print a

a[a > 0] = 1.0

a[a <= 0] = 0.1

# print a

return a

class Layer:

'''

神经网络层

'''

def __init__(self, num_neural, af_type="sigmoid", learn_rate=0.5):

self.af_type = af_type  # active function type

self.learn_rate = learn_rate

self.num_neural = num_neural

self.dim = None

self.W = None

self.a = None

self.X = None

self.z = None

self.delta = None

self.theta = None

self.act_fun = ActivationFun(self.af_type)

def fp(self, X):

'''

Foward Propagation

'''

self.X = X

xsize = X.shape

self.dim = xsize[0]

self.num = xsize[1]

if self.W == None:

# self.W = np.random.random((self.dim, self.num_neural))-0.5

# self.W = np.random.uniform(-1,1,size=(self.dim,self.num_neural))

if(self.af_type == "sigmoid"):

self.W = np.random.normal(0, 1, size=(self.dim, self.num_neural)) / np.sqrt(self.num)

elif(self.af_type == "relu"):

self.W = np.random.normal(0, 1, size=(self.dim, self.num_neural)) * np.sqrt(2.0 / self.num)

if self.theta == None:

# self.theta = np.random.random((self.num_neural, 1))-0.5

# self.theta = np.random.uniform(-1,1,size=(self.num_neural,1))

if(self.af_type == "sigmoid"):

self.theta = np.random.normal(0, 1, size=(self.num_neural, 1)) / np.sqrt(self.num)

elif(self.af_type == "relu"):

self.theta = np.random.normal(0, 1, size=(self.num_neural, 1)) * np.sqrt(2.0 / self.num)

# calculate the foreward a

self.a = (self.W.T).dot(self.X)

###calculate the foreward z####

self.z = self.act_fun.cal(self.a)

return self.z

def bp(self, delta):

'''

Back Propagation

'''

self.delta = delta * self.act_fun.cal_deriv(self.a)

self.theta = np.array([np.mean(self.theta - self.learn_rate * self.delta, 1)]).T  # 求所有样本的theta均值

dW = self.X.dot(self.delta.T) / self.num

self.W = self.W - self.learn_rate * dW

delta_out = self.W.dot(self.delta);

return delta_out

class BpNet:

'''

BP神经网络

'''

def __init__(self, net_struct, stop_crit, max_iter, batch_size=10):

self.name = "net work"

self.net_struct = net_struct

if len(self.net_struct) == 0:

print "no layer is specified!"

return

self.stop_crit = stop_crit

self.max_iter = max_iter

self.batch_size = batch_size

self.layers = []

self.num_layers = 0;

# 创建网络

self.create_net(net_struct)

self.loss_fun = LossFun("least_square");

def create_net(self, net_struct):

'''

创建网络

'''

self.num_layers = len(net_struct)

for i in range(self.num_layers):

self.layers.append(Layer(net_struct[i][0], net_struct[i][1], net_struct[i][2]))

def train(self, X, t, Xtest=None, ttest=None):

'''

训练网络

'''

eva_acc_list = []

eva_loss_list = []

xshape = X.shape;

num = xshape[0]

dim = xshape[1]

for k in range(self.max_iter):

# i = random.randint(0,num-1)

idxs = random.sample(range(num), self.batch_size)

xi = np.array([X[idxs, :]]).T[:, :, 0]

ti = np.array([t[idxs, :]]).T[:, :, 0]

# 前向计算

zi = self.fp(xi)

# 偏差计算

delta_i = self.loss_fun.cal_deriv(ti, zi)

# 反馈计算

self.bp(delta_i)

# 评估精度

if Xtest != None:

if k % 100 == 0:

[eva_acc, eva_loss] = self.test(Xtest, ttest)

eva_acc_list.append(eva_acc)

eva_loss_list.append(eva_loss)

print "%4d,%4f,%4f" % (k, eva_acc, eva_loss)

else:

print "%4d" % (k)

return [eva_acc_list, eva_loss_list]

def test(self, X, t):

'''

测试模型精度

'''

xshape = X.shape;

num = xshape[0]

z = self.fp_eval(X.T)

t = t.T

est_pos = np.argmax(z, 0)

real_pos = np.argmax(t, 0)

corrct_count = np.sum(est_pos == real_pos)

acc = 1.0 * corrct_count / num

loss = self.loss_fun.cal(t, z)

# print "%4f,loss:%4f"%(loss)

return [acc, loss]

def fp(self, X):

'''

前向计算

'''

z = X

for i in range(self.num_layers):

z = self.layers[i].fp(z)

return z

def bp(self, delta):

'''

反馈计算

'''

z = delta

for i in range(self.num_layers - 1, -1, -1):

z = self.layers[i].bp(z)

return z

def fp_eval(self, X):

'''

前向计算

'''

layers = self.layers

z = X

for i in range(self.num_layers):

z = layers[i].fp(z)

return z

def z_score_normalization(x):

mu = np.mean(x)

sigma = np.std(x)

x = (x - mu) / sigma;

return x;

def sigmoid(X, useStatus):

if useStatus:

return 1.0 / (1 + np.exp(-float(X)));

else:

return float(X);

def plot_curve(data, title, lege, xlabel, ylabel):

num = len(data)

idx = range(num)

plt.plot(idx, data, color="r", linewidth=1)

plt.xlabel(xlabel, fontsize="xx-large")

plt.ylabel(ylabel, fontsize="xx-large")

plt.title(title, fontsize="xx-large")

plt.legend([lege], fontsize="xx-large", loc='upper left');

plt.show()

if __name__ == "__main__":

print ('This is main of module "bp_nn.py"')

print("Import data")

raw_data = pd.read_csv('./train.csv', header=0)

data = raw_data.values

imgs = data[0::, 1::]

labels = data[::, 0]

train_features, test_features, train_labels, test_labels = ttsplit(

imgs, labels, test_size=0.33, random_state=23323)

train_features = z_score_normalization(train_features)

test_features = z_score_normalization(test_features)

sample_num = train_labels.shape[0]

tr_labels = np.zeros([sample_num, 10])

for i in range(sample_num):

tr_labels[i][train_labels[i]] = 1

sample_num = test_labels.shape[0]

te_labels = np.zeros([sample_num, 10])

for i in range(sample_num):

te_labels[i][test_labels[i]] = 1

print train_features.shape

print tr_labels.shape

print test_features.shape

print te_labels.shape

stop_crit = 100  # 停止

max_iter = 10000  # 最大迭代次数

batch_size = 100  # 每次训练的样本个数

net_struct = [[100, "relu", 0.01], [10, "sigmoid", 0.1]]  # 网络结构[[batch_size,active function, learning rate]]

# net_struct = [[200,"sigmoid",0.5],[100,"sigmoid",0.5],[10,"sigmoid",0.5]]  网络结构[[batch_size,active function, learning rate]]

bpNNCls = BpNet(net_struct, stop_crit, max_iter, batch_size);

# train model

[acc, loss] = bpNNCls.train(train_features, tr_labels, test_features, te_labels)

# [acc, loss] = bpNNCls.train(train_features, tr_labels)

print("training model finished")

# create test data

plot_curve(acc, "Bp Network Accuracy", "accuracy", "iter", "Accuracy")

plot_curve(loss, "Bp Network Loss", "loss", "iter", "Loss")

# test model

[acc, loss] = bpNNCls.test(test_features, te_labels);

print "test accuracy:%f" % (acc)

实验数据为mnist数据集合,可从以下地址下载:https://github.com/WenDesi/lihang_book_algorithm/blob/master/data/train.csv

a.使用sigmoid激活函数和net_struct = [10,"sigmoid"]的网络结构(可看作是softmax 回归),其校验精度和损失函数的变化,如下图所示:

测试精度达到0.916017,效果还是不错的。但是随机梯度法,依赖于参数的初始化,如果初始化不好,会收敛缓慢,甚至有不理想的结果。

b.使用sigmoid激活函数和net_struct =

[200,"sigmoid",100,"sigmoid",10,"sigmoid"] 的网络结构(一个200的隐藏层,一个100的隐藏层,和一个10的输出层),其校验精度和损失函数的变化,如下图所示:

其校验精度达到0.963636,比softmax要好不少。从损失曲线可以看出,加入隐藏层后,算法收敛要比无隐藏层的稳定。

Make Change - Focus on Computer Vision and Pattern Recognition

版权声明:本文为博主原创文章,未经博主允许不得转载

分类:Deep Learning,MachineLearning

标签:Deep Learning,机器学习,模式识别

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351