人工神经网络是有一系列简单的单元相互紧密联系构成的,每个单元有一定数量的实数输入和唯一的实数输出。神经网络的一个重要的用途就是接受和处理传感器产生的复杂的输入并进行自适应性的学习,是一种模式匹配算法,通常用于解决分类和回归问题。

  常用的人工神经网络算法包括:感知机神经网络(Perceptron Neural Nerwork)、反向传播网络(Back Propagation,BP)、HopField网络、自组织映射网络(Self-Organizing Map,SOM)、学习矢量量化网络(Learning Vector Quantization,LVQ)

1、感知机模型

  感知机是一种线性分类器,它用于二类分类问题。它将一个实例分类为正类(取值+1)和负类(-1)。其物理意义:它是将输入空间(特征空间)划分为正负两类的分离超平面。

  输入:线性可分训练数据集T,学习率η

  输出:感知机参数w,b

  算法步骤:

    1)选取初始值w0和b0

    2)在训练数据集中选取数据(xi,yi)

    3)若y1(w.xi+b)<=0(即该实例为误分类点)则更新参数:w=w+η.yi.xi   b=b+η.yi

    4)在训练数据集中重复选取数据来更新w,b直到训练数据集中没有误分类点为止

  实验代码:

  1. from matplotlib import pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. from sklearn.datasets import load_iris
  5. from sklearn.neural_network import MLPClassifier
  6.  
  7. def creat_data(n):
  8. np.random.seed(1)
  9. x_11=np.random.randint(0,100,(n,1))
  10. x_12=np.random.randint(0,100,(n,1,))
  11. x_13 = 20+np.random.randint(0, 10, (n, 1,))
  12. x_21 = np.random.randint(0, 100, (n, 1))
  13. x_22 = np.random.randint(0, 100, (n, 1))
  14. x_23 = 10-np.random.randint(0, 10, (n, 1,))
  15.  
  16. # print(x_11)
  17. # print(x_12)
  18. # print(x_13)
  19. # print(x_21)
  20. # print(x_22)
  21. # print(x_23)
  22.  
  23. # rotate 45 degrees along the X axis
  24. new_x_12=x_12*np.sqrt(2)/2-x_13*np.sqrt(2)/2
  25. new_x_13 = x_12 * np.sqrt(2) / 2 + x_13 * np.sqrt(2) / 2
  26. new_x_22=x_22*np.sqrt(2)/2-x_23*np.sqrt(2)/2
  27. new_x_23 = x_22 * np.sqrt(2) / 2 + x_23 * np.sqrt(2) / 2
  28.  
  29. # print(new_x_12)
  30. # print(new_x_13)
  31. # print(new_x_22)
  32. # print(new_x_23)
  33.  
  34. plus_samples=np.hstack([x_11,new_x_12,new_x_13,np.ones((n,1))])
  35. minus_samples=np.hstack([x_11,new_x_22,new_x_23,-np.ones((n,1))])
  36. samples=np.vstack([plus_samples,minus_samples])
  37. # print(samples)
  38. np.random.shuffle(samples)
  39.  
  40. # print(plus_samples)
  41. # print(minus_samples)
  42. # print(samples)
  43.  
  44. return samples
  45.  
  46. def plot_samples(ax,samples):
  47. Y=samples[:,-1]
  48. Y=samples[:,-1]
  49. # print(Y)
  50. position_p=Y==1 ##the position of positve class
  51. position_m=Y==-1 ##the position of minus class
  52. # print(position_p)
  53. # print(position_m)
  54. ax.scatter(samples[position_p,0],samples[position_p,1],samples[position_p,2],marker='+',label="+",color='b')
  55. ax.scatter(samples[position_m,0],samples[position_m,1],samples[position_m,2],marker='^',label='-',color='y')
  56.  
  57. def perceptron(train_data,eta,w_0,b_0):
  58. x=train_data[:,:-1] #x data
  59. y=train_data[:,-1] #corresponding classification
  60. length=train_data.shape[0] #the size of sample==the row number of the train_data
  61. w=w_0
  62. b=b_0
  63. step_num=0
  64. while True:
  65. i=0
  66. while(i<length): #traverse all sample points in a sample set
  67. step_num+=1
  68. x_i=x[i].reshape((x.shape[1],1))
  69. y_i=y[i]
  70. if y_i*(np.dot(np.transpose(w),x_i)+b)<=0: #the point is misclassified
  71. w=w+eta*y_i*x_i #gradient descent
  72. b=b+eta*y_i
  73. break;#perform the next round of screening
  74. else: #the point is not a misclassification point select the next sample point
  75. i=i+1
  76. if(i==length):
  77. break
  78. return (w,b,step_num)
  79.  
  80. def creat_hyperplane(x,y,w,b):
  81. return (-w[0][0]*x-w[1][0]*y-b)/w[2][0] #w0*x+w1*y+w2*z+b=0
  82.  
  83. data=creat_data(100)
  84. eta,w_0,b_0=0.1,np.ones((3,1),dtype=float),1
  85. w,b,num=perceptron(data,eta,w_0,b_0)
  86.  
  87. fig=plt.figure()
  88. plt.suptitle("perceptron")
  89. ax=Axes3D(fig)
  90. #draw samplt point
  91. plot_samples(ax,data)
  92. #draw hyperplane
  93. x=np.linspace(-30,100,100)
  94. y=np.linspace(-30,100,100)
  95. x,y=np.meshgrid(x,y)
  96. z=creat_hyperplane(x,y,w,b)
  97. ax.plot_surface(x,y,z,rstride=1,cstride=1,color='g',alpha=0.2)
  98.  
  99. ax.legend(loc='best')
  100. plt.show()

  实验结果:

  注:算法中,最外层循环只有在全部分类正确的这种情况下退出;内层循环从前到后遍历所有的样本点。一旦发现某个样本点是误分类点,就更新w和b,然后重新从头开始遍历所有的样本点。感知机算法的对偶形式(参考原著)其处理效果与原始算法相差不大,但是从其输出的α数组值可以看出:大多数的样本点对于最终解并没有贡献。分离超平面的位置是由少部分重要的样本点决定的。而感知机学习算法的对偶形式能够找出这些重要的样本点。这就是支持向量机的原理。

  一开始我在create data时,公式写错了,导致所得到的数据线性不可分,此时发现算法根本无法完成迭代。可见感知机算法只能够用于线性可分数据集。  

2、神经网络

(1)从感知机到神经网络:M-P神经元模型

  1)每个神经元接收到来自相邻神经元传递过来的输入信号

  2)这些输入信号通过带权重的连接进行传递

  3)神经元接收到的总输入值将与神经元的阈值进行比较,然后通过“激活函数”处理以产生神经元输出

  4)理论上的激活函数为阶跃函数

                        1,x>=0

                        f(x)=

                        0,x<0

(2)多层前馈神经网络

  通常神经网络的结构为:1)每层神经元与下一层神经元全部互连  2)同层神经元之间不存在连接  3)跨层神经元之间也不存在连接

  多层前馈神经网络具有以下特点:1)隐含层和输出层神经元都是拥有激活函数的功能神经元  2)输入层接收外界输入信号,不进行激活函数处理  3)最终结果由输出层神经元给出

  神经网络的学习就是根据训练数据集来调整神经元之间的连接权重,以及每个功能神经元的阈值。

  多层前馈神经网络的学习通常采用误差逆传播算法(error  BackPropgation,BP):该算法是训练多层神经网络的经典算法,其从原理上就是普通的梯度下降法求最小值问题。它关键的地方在于两个:1)导数的链式法则  2)sigmoid激活函数的性质:sigmoid函数求导的结果等于自变量的乘积形式

  多层前馈网络若包含足够多神经元的隐含层,则它就能够以任意精度逼近任意复杂度的连续函数

  实验代码:

  1. from matplotlib import pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. from sklearn.datasets import load_iris
  5. from sklearn.neural_network import MLPClassifier
  6.  
  7. def creat_data_no_linear(n):
  8. np.random.seed(1)
  9. x_11=np.random.randint(0,100,(n,1))
  10. x_12=10+np.random.randint(-5,5,(n,1,))
  11.  
  12. x_21 = np.random.randint(0, 100, (n, 1))
  13. x_22 = 20+np.random.randint(0, 10, (n, 1))
  14.  
  15. x_31 = np.random.randint(0, 100, (int(n/10),1))
  16. x_32 = 20+np.random.randint(0, 10, (int(n/10), 1))
  17.  
  18. # print(x_11)
  19. # print(x_12)
  20. # print(x_13)
  21. # print(x_21)
  22. # print(x_22)
  23. # print(x_23)
  24.  
  25. # rotate 45 degrees along the X axis
  26. new_x_11 = x_11 * np.sqrt(2) / 2 - x_12 * np.sqrt(2) / 2
  27. new_x_12=x_11*np.sqrt(2)/2+x_12*np.sqrt(2)/2
  28. new_x_21 = x_21 * np.sqrt(2) / 2 - x_22 * np.sqrt(2) / 2
  29. new_x_22=x_21*np.sqrt(2)/2+x_22*np.sqrt(2)/2
  30. new_x_31 = x_31 * np.sqrt(2) / 2 - x_32 * np.sqrt(2) / 2
  31. new_x_32 = x_31 * np.sqrt(2) / 2 + x_32 * np.sqrt(2) / 2
  32.  
  33. # print(new_x_12)
  34. # print(new_x_13)
  35. # print(new_x_22)
  36. # print(new_x_23)
  37.  
  38. plus_samples=np.hstack([new_x_11,new_x_12,np.ones((n,1))])
  39. minus_samples=np.hstack([new_x_21,new_x_22,-np.ones((n,1))])
  40. err_samples=np.hstack([new_x_31,new_x_32,np.ones((int(n/10),1))])
  41. samples=np.vstack([plus_samples,minus_samples,err_samples])
  42. # print(samples)
  43. np.random.shuffle(samples)
  44.  
  45. # print(plus_samples)
  46. # print(minus_samples)
  47. # print(samples)
  48.  
  49. return samples
  50.  
  51. def plot_samples_2d(ax,samples):
  52. Y=samples[:,-1]
  53. # print(Y)
  54. position_p=Y==1 ##the position of positve class
  55. position_m=Y==-1 ##the position of minus class
  56. # print(position_p)
  57. # print(position_m)
  58. ax.scatter(samples[position_p,0],samples[position_p,1],marker='+',label="+",color='b')
  59. ax.scatter(samples[position_m,0],samples[position_m,1],marker='^',label='-',color='y')
  60.  
  61. def predict_with_MLPClassifier(ax,train_data):
  62. train_x=train_data[:,:-1]
  63. train_y=train_data[:,-1]
  64. clf=MLPClassifier(activation='logistic',max_iter=1000)
  65. clf.fit(train_x,train_y)
  66. print(clf.score(train_x,train_y))
  67.  
  68. x_min,x_max=train_x[:,0].min()-1,train_x[:,0].max()+2
  69. y_min,y_max=train_x[:,1].min()-1,train_x[:,1].max()+2
  70. plot_step=1
  71.  
  72. xx,yy=np.meshgrid(np.arange(x_min,x_max,plot_step),np.arange(y_min,y_max,plot_step))
  73. Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
  74. Z=Z.reshape(xx.shape)
  75. ax.contourf(xx,yy,Z,cmap=plt.cm.Paired)
  76.  
  77. fig=plt.figure()
  78. ax=fig.add_subplot(1,1,1)
  79. data=creat_data_no_linear(500)
  80. predict_with_MLPClassifier(ax,data)
  81. plot_samples_2d(ax,data)
  82. ax.legend(loc='best')
  83. plt.show()

  实验结果:

  生成的实验数据

  样例(对鸢尾花进行分类)代码:

  1. from matplotlib import pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. from sklearn.datasets import load_iris
  5. from sklearn.neural_network import MLPClassifier
  6.  
  7. def load_data():
  8. iris=load_iris()
  9. X=iris.data[:,0:2] #choose the first two features
  10. Y=iris.target
  11. data=np.hstack((X,Y.reshape(Y.size,1)))
  12. np.random.seed(0)
  13. np.random.shuffle(data)
  14. X=data[:,:-1]
  15. Y=data[:,-1]
  16. x_train=X[:-30]
  17. x_test=X[-30:]
  18. y_train=Y[:-30]
  19. y_test=Y[-30:]
  20.  
  21. return x_train,x_test,y_train,y_test
  22.  
  23. def neural_network_sample(*data):
  24. x_train,x_test,y_train,y_test=data
  25. cls=MLPClassifier(activation='logistic',max_iter=10000,hidden_layer_sizes=(30,))
  26. cls.fit(x_train,y_train)
  27. print("the train score:%.f"%cls.score(x_train,y_train))
  28. print("the test score:%.f"%cls.score(x_test,y_test))
  29.  
  30. x_train,x_test,y_train,y_test=load_data()
  31.  
  32. neural_network_sample(x_train,x_test,y_train,y_test)

  实验结果:

  这里的score居然等于1。。。书上给的数据是0.8,不知道是因为算法改进了还是怎么回事。

  注:在进行编码实现时遇到了一些小插曲,就是Anaconda的sklearn并不是完整的,当导入MLPClassifier时会出错,因为其中0.17的版本的scikit-learn中并不包括这个库。所以接下来要做的就是更新Anaconda的相关库。这时候问题就来了,更新时,网速极慢,只有6k/s,并且十几秒后就会报错,错误原因是无法连接某些网址。起初我以为是网速的问题,尝试了各种方式,调整了网络通各个端口去试都没有用,甚至想到了换网线和换网络通账号,结果还是不行。后来想到会不会是要翻墙,于是参照着这个网址:http://blog.sina.com.cn/s/blog_920b83770102xjxp.html,翻了一下,发现并不能访问youtube,这让我感到很难受。抱着试试的态度我又重新敲起了更新的指令,这次居然奇迹般的可以了!!!后来一想youtube之所以打不开,可能是浏览器的设置问题,vpn本身是可以用的。有种天道酬勤的感觉!

  不过这也让我很好的反思了一下。遇到问题时一定要认真分析可能的原因,而不是盲目的去猜,甚至愚蠢的去多次尝试之前并没有生效的方法,这样会浪费太多的时间。解决问题一定是从可能性最大的错误着手,并且要加以分析,否则不仅时间浪费了,自信心也受到了打击,人也会弄的疲劳不堪。

python大战机器学习——人工神经网络的更多相关文章

  1. 吴裕雄 python 机器学习——人工神经网络感知机学习算法的应用

    import numpy as np from matplotlib import pyplot as plt from sklearn import neighbors, datasets from ...

  2. 吴裕雄 python 机器学习——人工神经网络与原始感知机模型

    import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ...

  3. python大战机器学习——集成学习

    集成学习是通过构建并结合多个学习器来完成学习任务.其工作流程为: 1)先产生一组“个体学习器”.在分类问题中,个体学习器也称为基类分类器 2)再使用某种策略将它们结合起来. 通常使用一种或者多种已有的 ...

  4. python大战机器学习——模型评估、选择与验证

    1.损失函数和风险函数 (1)损失函数:常见的有 0-1损失函数  绝对损失函数  平方损失函数  对数损失函数 (2)风险函数:损失函数的期望      经验风险:模型在数据集T上的平均损失 根据大 ...

  5. python大战机器学习——数据预处理

    数据预处理的常用流程: 1)去除唯一属性 2)处理缺失值 3)属性编码 4)数据标准化.正则化 5)特征选择 6)主成分分析 1.去除唯一属性 如id属性,是唯一属性,直接去除就好 2.处理缺失值 ( ...

  6. python大战机器学习——半监督学习

    半监督学习:综合利用有类标的数据和没有类标的数据,来生成合适的分类函数.它是一类可以自动地利用未标记的数据来提升学习性能的算法 1.生成式半监督学习 优点:方法简单,容易实现.通常在有标记数据极少时, ...

  7. python大战机器学习——支持向量机

    支持向量机(Support Vector Machine,SVM)的基本模型是定义在特征空间上间隔最大的线性分类器.它是一种二类分类模型,当采用了核技巧之后,支持向量机可以用于非线性分类. 1)线性可 ...

  8. python大战机器学习——聚类和EM算法

    注:本文中涉及到的公式一律省略(公式不好敲出来),若想了解公式的具体实现,请参考原著. 1.基本概念 (1)聚类的思想: 将数据集划分为若干个不想交的子集(称为一个簇cluster),每个簇潜在地对应 ...

  9. python大战机器学习——数据降维

    注:因为公式敲起来太麻烦,因此本文中的公式没有呈现出来,想要知道具体的计算公式,请参考原书中内容 降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中 1.主成分分析(PCA) 将n ...

随机推荐

  1. JS事件派发器EventEmitter

    原文地址:http://zhangyiheng.com/blog/articles/js_event_mitter.html 需求 随着Browser客户端JS越来越复杂,MVC(Client端)设计 ...

  2. Python实现结对编程项目

    Github (李昆乘)(陈俊豪) 开发流程 PSP2.1 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 ...

  3. oracle数据库复习(1)

    数据库中的专业术语: 表:在数据库中存放数据所用的表 视图:数据库中的虚拟表.在视图中存放的是从数据表中查询出来的纪录 存储过程:存储过程是由SQL语句和控制流语句组成的代码块.存储过程在开发软件时, ...

  4. bzoj 3456 城市规划 —— 分治FFT / 多项式求逆 / 指数型生成函数(多项式求ln)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3456 首先考虑DP做法,正难则反,考虑所有情况减去不连通的情况: 而不连通的情况就是那个经典 ...

  5. 普通项目转为maven项目及相关操作说明

    普通项目转为maven项目及相关操作说明 1 原项目简述 如图,一般的项目大致包括三类路径:src,源码路径:test,单元测试路径:lib第三方类包路径. 示例项目中,BaseDao类依赖于mysq ...

  6. 如何分析一个QT类

    作者:gnuhpc  出处:http://www.cnblogs.com/gnuhpc/ 我们以QLineEdit这个类为例来看看如何学习分析一个QT类. 1.Public Types: 这是一个在这 ...

  7. c# winform DataGridView 单元格的屏幕位置

    首先取得DataGridView的坐标位置:int dgvX = dataGridView1.Location.X;int dgvY = dataGridView1.Location.Y;然后取得选中 ...

  8. Oracle查看表空间和表空间中的对象

    select * from user_tables;--查询所有用户表 select username,default_tablespace from user_users;--查询当前表空间sele ...

  9. RHEL6安装JDK7

    一.安装准备 1.操作系统:redhat-server-6.1-x86_64 下载地址: http://www.verycd.com/files/d39b97540497d24175340915244 ...

  10. 【机器学习】决策树C4.5、ID3

    一.算法流程 step1:计算信息熵 step2: 划分数据集 step3: 创建决策树 step4: 利用决策树分类 二.信息熵Entropy.信息增益Gain 重点:选择一个属性进行分支.注意信息 ...