BP神经网络和感知器基本原理

1、BP神经网络

1.概述

BP神经网络是一种按偏差逆传播算法训练的多层前馈网络,是目前应用最普遍的神经网络模型之一。BP网络能学习和存贮大量的输入-输出模式映射关系,而无需事前揭示描述这种映射关系的数学方程。它的学习规则是使用最速降低法,经过反向传播来不断调整网络的权值和阈值,使网络的偏差平方和最小。BP算法是一种最有效的多层神经网络学习方法,其主要特色是信号前向传递,而偏差后向传播,经过不断调节网络权重值,使得网络的最终输出与指望输出尽量接近,以达到训练的目的。web

2.BP神经元

在这里插入图片描述

图1 BP神经元

如图1,给出了第j个基本BP神经元(节点),它只模仿了生物神经元所具备的三个最基本也是最重要的功能:加权、求和与转移。其中x1、x2、xi…xn分别表明来自神经元一、二、i…n的输入;wj1、wj2、wji、wjn则分别表示神经元一、二、i、n…与第j个神经元的链接强度,即权值;bj为阈值;f(·)为传递函数;yj为第j个神经元的输出。
第j个神经元的净输入值Sj为:
在这里插入图片描述
其中:
在这里插入图片描述
若视x0=1,wj0=bj,即令X及Wj包括x0及wj0,则
在这里插入图片描述
因而节点j的净输入Sj可表示为:
在这里插入图片描述
净输入Sj经过传递函数f(·)后,便获得第j个神经元的输出yj:
在这里插入图片描述
式中f(·)是单调上升函数,并且必须是有界函数,由于细胞传递的信号不可能无限增长,必有一最大值。算法

3.BP神经网络原理

3.1网络结构:前馈过程

在这里插入图片描述
如上图所示,BP神经网络结构包括三层,分别为:输入层、隐藏层、输出层。数组

各变量表示含义:
xi:输入层第i个结点输入值
wij:输入层结点i到隐含层结点j的权重
θj:隐含层第j个结点的阈值
φ(x):隐含层激励函数
wjk:隐含层结点j到输出层结点k的权重
bk:输出层结点k的阈值
f(x):输出层激励函数
y:输出层第k个结点的输出
PS:为方便起见,记i,j,k分别为输入、隐含、输出层的变量下标网络

设网络输入为x,则隐含层第j个节点输入为:
在这里插入图片描述
网络输出层第k个节点输入为:
在这里插入图片描述
所以,网络输出层第k个节点输出为:
在这里插入图片描述ide

3.2训练过程

网络学习的目的就是根据训练数据的偏差E对于各个参数的梯度,求解参数调整量:
在这里插入图片描述
其中输出层权重调整量:
在这里插入图片描述
输出层阈值调整:
在这里插入图片描述svg

4.代码实现

代码Part1: BP神经网络的构建函数

% BP网络
% BP神经网络的构建
net=newff([-1 2;0 5],[3,1],{'tansig','purelin'},'traingd')//生成一个前馈BP网络
net.IW{1}
net.b{1}

p=[1;2];
a=sim(net,p)
net=init(net);
net.IW{1}
net.b{1}
a=sim(net,p)
%net.IW{1}*p+net.b{1}
p2=net.IW{1}*p+net.b{1}
a2=sign(p2)
a3=tansig(a2)
a4=purelin(a3)
net.b{2}
net.b{1}

net.IW{1}
net.IW{2}
0.7616+net.b{2}
a-net.b{2}
(a-net.b{2})/ 0.7616
help purelin

p1=[0;0];
a5=sim(net,p1)
net.b{2}

代码Part2: BP神经网络的构建性能

% BP网络
% BP神经网络的构建
net=newff([-1 2;0 5],[3,1],{'tansig','purelin'},'traingd')
net.IW{1}
net.b{1}
%p=[1;];
p=[1;2];
a=sim(net,p)
net=init(net);
net.IW{1}
net.b{1}
a=sim(net,p)
net.IW{1}*p+net.b{1}
p2=net.IW{1}*p+net.b{1}
a2=sign(p2)
a3=tansig(a2)
a4=purelin(a3)
net.b{2}
net.b{1}

P=[1.2;3;0.5;1.6]
W=[0.3 0.6 0.1 0.8]
net1=newp([0 2;0 2;0 2;0 2],1,'purelin');
net2=newp([0 2;0 2;0 2;0 2],1,'logsig');
net3=newp([0 2;0 2;0 2;0 2],1,'tansig');
net4=newp([0 2;0 2;0 2;0 2],1,'hardlim');

net1.IW{1}
net2.IW{1}
net3.IW{1}
net4.IW{1}
net1.b{1}
net2.b{1}
net3.b{1}
net4.b{1}


net1.IW{1}=W;
net2.IW{1}=W;
net3.IW{1}=W;
net4.IW{1}=W;

a1=sim(net1,P)
a2=sim(net2,P)
a3=sim(net3,P)
a4=sim(net4,P)

init(net1);
net1.b{1}

help tansig

代码Part3: BP神经网络的训练学习

% 训练
p=[-0.1 0.5]
t=[-0.3 0.4]
w_range=-2:0.4:2;
b_range=-2:0.4:2;

ES=errsurf(p,t,w_range,b_range,'logsig');%单输入神经元的偏差曲面
plotes(w_range,b_range,ES)%绘制单输入神经元的偏差曲面
pause(0.5);
hold off;
net=newp([-2,2],1,'logsig');
net.trainparam.epochs=100;
net.trainparam.goal=0.001;
figure(2);
[net,tr]=train(net,p,t);
title('动态逼近')
wight=net.iw{1}
bias=net.b
pause;
close;

代码Part4: BP神经网络的训练ui

% 练
p=[-0.2 0.2 0.3 0.4]
t=[-0.9 -0.2 1.2 2.0]
h1=figure(1);
net=newff([-2,2],[5,1],{'tansig','purelin'},'trainlm');
net.trainparam.epochs=100;
net.trainparam.goal=0.0001;
net=train(net,p,t);
a1=sim(net,p)
pause;
h2=figure(2);
plot(p,t,'*');
title('样本')
title('样本');
xlabel('Input');
ylabel('Output');
pause;
hold on;
ptest1=[0.2 0.1]
ptest2=[0.2 0.1 0.9]
a1=sim(net,ptest1);
a2=sim(net,ptest2);

net.iw{1}
net.iw{2}
net.b{1}
net.b{2}

5.代码解释

①.

net=newff([-1 2;0 5],[3,1],{'tansig','purelin'},'traingd')

这个命令创建了网络对象而且初始化了网络权重和偏置,它的输入是两个元素的向量,第1层有3个神经元,第2层有1个神经元。第1层的转移函数是tansig,输出层的转移函数是purelin。输入向量的第1个元素的范围是- 1到2 ,输入向量的第2个元素的范围是0到5 ,训练函数是traingd。即:
在这里插入图片描述
BP函数的训练方法:
在这里插入图片描述
②.

net.IW{1}
net.b{1}

net.IW{1}表示输入层与隐含层间的链接权值,net.b{1}表示隐含层的阈值。

③.网络仿真

a=sim (net, p)

应用函数sim()能够对生成的感知器网络进行仿真,即给定肯定输入p,求输出a:
其中net为一个已经生成的感知器网络,p为R* 1输入列向量,a为S*1输出向量(S为神经元数目)。
④.网络初始化

net=init (net);

应用函数init()能够对生成的感知器网络进行阈值和权值初始化。主要做用是将神经网络的阈值和权值复原回生成时的初始值。
⑤.

a2=sign(p2)

sign(x):符号函数
当x<0时,sign(x)=-1;
当x=0时,sign(x)=0;
当x>0时,sign(x)=1。
⑥.

net=newp([-2,2],1,'logsig');

做用:构造感知器模型。
句法:net=newp[PR,S,TF,LF]
解释:
PR:Rx2的输入向量最大值和最小值构成的矩阵,即每一行的最大值最小值构成一行;
S:构造的神经元的个数;
TF:激活函数的设置,可设置为hardlim函数或者hardlins函数,默认为hardlim函数;
LF:学习修正函数的设置,可设置为learnp函数或者learnpn函数,默认为learnp函数;
net:生成的感知器网络。
⑦.

net.trainparam.epochs=100;%最大训练次数
net.trainparam.goal=0.001;%训练要求精度
[net,tr]=train(net,p,t);%网络训练

6.运行结果及分析

代码Part1 运行结果:

>> BP

net =

    Neural Network
 
              name: 'Custom Neural Network'
          userdata: (your custom info)
 
    dimensions:
 
         numInputs: 1
         numLayers: 2
        numOutputs: 1
    numInputDelays: 0
    numLayerDelays: 0
 numFeedbackDelays: 0
 numWeightElements: 13
        sampleTime: 1
 
    connections:
 
       biasConnect: [1; 1]
      inputConnect: [1; 0]
      layerConnect: [0 0; 1 0]
     outputConnect: [0 1]
 
    subobjects:
 
             input: Equivalent to inputs{1}
            output: Equivalent to outputs{2}
 
            inputs: {1x1 cell array of 1 input}
            layers: {2x1 cell array of 2 layers}
           outputs: {1x2 cell array of 1 output}
            biases: {2x1 cell array of 2 biases}
      inputWeights: {2x1 cell array of 1 weight}
      layerWeights: {2x2 cell array of 1 weight}
 
    functions:
 
          adaptFcn: 'adaptwb'
        adaptParam: (none)
          derivFcn: 'defaultderiv'
         divideFcn: (none)
       divideParam: (none)
        divideMode: 'sample'
           initFcn: 'initlay'
        performFcn: 'mse'
      performParam: .regularization, .normalization
          plotFcns: {'plotperform', plottrainstate,
                    plotregression}
        plotParams: {1x3 cell array of 3 params}
          trainFcn: 'traingd'
        trainParam: .showWindow, .showCommandLine, .show, .epochs,
                    .time, .goal, .min_grad, .max_fail, .lr
 
    weight and bias values:
 
                IW: {2x1 cell} containing 1 input weight matrix
                LW: {2x2 cell} containing 1 layer weight matrix
                 b: {2x1 cell} containing 2 bias vectors
 
    methods:
 
             adapt: Learn while in continuous use
         configure: Configure inputs & outputs
            gensim: Generate Simulink model
              init: Initialize weights & biases
           perform: Calculate performance
               sim: Evaluate network outputs given inputs
             train: Train network with examples
              view: View diagram
       unconfigure: Unconfigure inputs & outputs
 

ans =

    0.9793    0.7717
    1.5369    0.3008
   -1.0989   -0.7114


ans =

   -4.8438
   -1.5204
   -0.0970


a =

    0.5105


ans =

   -1.6151   -0.0414
    1.3628    0.5217
    1.2726   -0.5981


ans =

    3.3359
   -1.9858
    3.2839


a =

    1.6873


p2 =

    1.6380
    0.4205
    3.3602


a2 =

     1
     1
     1


a3 =

    0.7616
    0.7616
    0.7616


a4 =

    0.7616
    0.7616
    0.7616


ans =

    0.9190


ans =

    3.3359
   -1.9858
    3.2839


ans =

   -1.6151   -0.0414
    1.3628    0.5217
    1.2726   -0.5981


ans =

     []


ans =

    1.6806


ans =

    0.7683


ans =

    1.0088

 purelin Linear transfer function.
 	
  Transfer functions convert a neural network layer's net input into
  its net output.
 	
  A = purelin(N) takes an SxQ matrix of S N-element net input column
  vectors and returns an SxQ matrix A of output vectors equal to N.
 
  Here a layer output is calculate from a single net input vector:
 
    n = [0; 1; -0.5; 0.5];
    a = purelin(n);
 
  Here is a plot of this transfer function:
 
    n = -5:0.01:5;
    plot(n,purelin(n))
    set(gca,'dataaspectratio',[1 1 1],'xgrid','on','ygrid','on')
 
  Here this transfer function is assigned to the ith layer of a network:
 
    net.layers{i}.transferFcn = 'purelin';
 
 	See also poslin, satlin, satlins.

    purelin 的参考页


a5 =

    0.5450


ans =

    0.9190

>>

代码Part2 运行结果:

>> BP

net =

    Neural Network
 
              name: 'Custom Neural Network'
          userdata: (your custom info)
 
    dimensions:
 
         numInputs: 1
         numLayers: 2
        numOutputs: 1
    numInputDelays: 0
    numLayerDelays: 0
 numFeedbackDelays: 0
 numWeightElements: 13
        sampleTime: 1
 
    connections:
 
       biasConnect: [1; 1]
      inputConnect: [1; 0]
      layerConnect: [0 0; 1 0]
     outputConnect: [0 1]
 
    subobjects:
 
             input: Equivalent to inputs{1}
            output: Equivalent to outputs{2}
 
            inputs: {1x1 cell array of 1 input}
            layers: {2x1 cell array of 2 layers}
           outputs: {1x2 cell array of 1 output}
            biases: {2x1 cell array of 2 biases}
      inputWeights: {2x1 cell array of 1 weight}
      layerWeights: {2x2 cell array of 1 weight}
 
    functions:
 
          adaptFcn: 'adaptwb'
        adaptParam: (none)
          derivFcn: 'defaultderiv'
         divideFcn: (none)
       divideParam: (none)
        divideMode: 'sample'
           initFcn: 'initlay'
        performFcn: 'mse'
      performParam: .regularization, .normalization
          plotFcns: {'plotperform', plottrainstate,
                    plotregression}
        plotParams: {1x3 cell array of 3 params}
          trainFcn: 'traingd'
        trainParam: .showWindow, .showCommandLine, .show, .epochs,
                    .time, .goal, .min_grad, .max_fail, .lr
 
    weight and bias values:
 
                IW: {2x1 cell} containing 1 input weight matrix
                LW: {2x2 cell} containing 1 layer weight matrix
                 b: {2x1 cell} containing 2 bias vectors
 
    methods:
 
             adapt: Learn while in continuous use
         configure: Configure inputs & outputs
            gensim: Generate Simulink model
              init: Initialize weights & biases
           perform: Calculate performance
               sim: Evaluate network outputs given inputs
             train: Train network with examples
              view: View diagram
       unconfigure: Unconfigure inputs & outputs
 

ans =

    0.5460    0.9129
   -1.5087    0.3485
    1.3006    0.5761


ans =

   -4.9802
   -0.1168
    0.3344


a =

   -0.6727


ans =

    0.6683   -0.8832
   -1.2254   -0.6327
   -0.9178    0.7985


ans =

   -0.5511
    2.1943
   -3.9622


a =

   -2.0856


ans =

   -1.6491
   -0.2963
   -3.2830


p2 =

   -1.6491
   -0.2963
   -3.2830


a2 =

    -1
    -1
    -1


a3 =

   -0.7616
   -0.7616
   -0.7616


a4 =

   -0.7616
   -0.7616
   -0.7616


ans =

   -0.9311


ans =

   -0.5511
    2.1943
   -3.9622


P =

    1.2000
    3.0000
    0.5000
    1.6000


W =

    0.3000    0.6000    0.1000    0.8000


ans =

     0     0     0     0


ans =

     0     0     0     0


ans =

     0     0     0     0


ans =

     0     0     0     0


ans =

     0


ans =

     0


ans =

     0


ans =

     0


a1 =

    3.4900


a2 =

    0.9704


a3 =

    0.9981


a4 =

     1


ans =

     0

 tansig Symmetric sigmoid transfer function.
 
  Transfer functions convert a neural network layer's net input into
  its net output.
 	
  A = tansig(N) takes an SxQ matrix of S N-element net input column
  vectors and returns an SxQ matrix A of output vectors, where each element
  of N in is squashed from the interval [-inf inf] to the interval [-1 1]
  with an "S-shaped" function.
 
  Here a layer output is calculate from a single net input vector:
 
    n = [0; 1; -0.5; 0.5];
    a = tansig(n);
 
  Here is a plot of this transfer function:
 
    n = -5:0.01:5;
    plot(n,tansig(n))
    set(gca,'dataaspectratio',[1 1 1],'xgrid','on','ygrid','on')
 
  Here this transfer function is assigned to the ith layer of a network:
 
    net.layers{i}.transferFcn = 'tansig';
 
 	See also logsig, elliotsig.

    tansig 的参考页

>>

代码Part3 运行结果:

>> BP

p =

   -0.1000    0.5000


t =

   -0.3000    0.4000


wight =

   15.1653


bias =

  1×1 cell 数组

    {[-8.8694]}

在这里插入图片描述
在这里插入图片描述
最上面的图形显示的是神经网络的结构图,可知有一个隐藏层一个输出层。

第二部分显示的是训练算法,这里为训练函数trainc–循环顺序权重/阈值的训练法;偏差指标为MAE。

第三部分显示训练进度:
Epoch:训练次数;在其右边显示的是最大的训练次数,上面例子中为100;而进度条中显示的是实际训练的次数,上面例子中实际训练次数为100次。
Time:训练时间,也就是本次训练中,使用的时间。上面例子中使用的时间为0:00:01。
Performance:性能指标,本例子中为平均绝对偏差(mae)的最大值。精度条中显示的是当前的平均绝对偏差;进度条右边显示的是设定的平均绝对偏差(若是当前的平均绝对偏差小于设定值,则中止训练),这个指标能够用trainParam.goal参数设定。

第四部分为做图。分别点击按钮能看到偏差变化曲线,分别用于绘制当前神经网络的性能图和训练状态。

性能图:
在这里插入图片描述
代码Part4 运行结果:

>> BP

p =

   -0.2000    0.2000    0.3000    0.4000


t =

   -0.9000   -0.2000    1.2000    2.0000


a1 =

   -0.9000   -0.2000    1.2000    2.0000


ptest1 =

    0.2000    0.1000


ptest2 =

    0.2000    0.1000    0.9000


ans =

    3.5002
   -3.3059
    9.9030
    3.4402
    3.5000


ans =

     []


ans =

   -6.9996
    3.9035
   -2.5951
    3.7285
    6.9998


ans =

    0.6532

>>

在这里插入图片描述
最上面的图形显示的是神经网络的结构图。

第二部分显示的是训练算法,这里为学习率自适应的梯度降低BP算法;偏差指标为MSE。

第三部分显示训练进度:
Epoch:训练次数;在其右边显示的是最大的训练次数,能够设定,上面例子中设为100;而进度条中显示的是实际训练的次数,上面例子中实际训练次数为9次。
Time:训练时间,也就是本次训练中,使用的时间。
Performance:性能指标;本例子中为均方偏差(mse)的最大值。精度条中显示的是当前的均方偏差;进度条右边显示的是设定的均方偏差(若是当前的均方偏差小于设定值,则中止训练),这个指标能够用trainParam.goal参数设定。
Gradiengt:梯度;进度条中显示的当前的梯度值,其右边显示的是设定的梯度值。若是当前的梯度值达到了设定值,则中止训练。

第四部分为做图。分别点击三个按钮能看到偏差变化曲线,分别用于绘制当前神经网络的性能图,训练状态和回归分析。

性能图:
在这里插入图片描述
回归分析:
在这里插入图片描述
在这里插入图片描述

7.BP神经网络的优劣势

优点:
BP神经网络不管在网络理论仍是在性能方面已比较成熟。其突出优势就是具备很强的非线性映射能力和柔性的网络结构。网络的中间层数、各层的神经元个数可根据具体状况任意设定,而且随着结构的差别其性能也有所不一样。
劣势:
①学习速度慢,即便是一个简单的问题,通常也须要几百次甚至上千次的学习才能收敛。
②容易陷入局部极小值。
③网络层数、神经元个数的选择没有、反馈的理论指导。
④网络推广能力有限。

2、感知器

1.算法

输入: 训练数据T={(x1,y1),(x2,y2),…,(xN,yN)},学习率ŋ
输出: w,b, 即感知机模型

  1. w,b权值初始化;
  2. 选取训练数据(xi,yi);
  3. 若是yi(wxi+b)≤0, 则:
    w←w+ŋyixi
    b←b+ŋyi
  4. 转至step2,直至训练数据集中没有误分类点或知足某个终止条件。
    PS: 学习过程可选批量梯度降低

2.代码实现

代码Part1 : 感知器神经网络的构建

% 第一章  感知器
% 1. 感知器神经网络的构建
% 1.1  生成网络
net=newp([0 2],1);%单输入,输入值为[0,2]之间的数
inputweights=net.inputweights{1,1};%第一层的权重为1
biases=net.biases{1};%阈值为1
% 1.2  网络仿真

net=newp([-2 2;-2 2],1);%两个输入,一个神经元,默认二值激活
net.IW{1,1}=[-1 1];%权重,net.IW{i,j}表示第i层网络第j个神经元的权重向量
net.IW{1,1}
net.b{1}=1;
net.b{1}
p1=[1;1],a1=sim(net,p1)
p2=[1;-1],a2=sim(net,p2)
p3={[1;1] [1 ;-1]},a3=sim(net,p3) %两组数据放一块儿
p4=[1 1;1 -1],a4=sim(net,p4)%也能够放在矩阵里面
net.IW{1,1}=[3,4];
net.b{1}=[1];
a1=sim(net,p1)
% 1.3  网络初始化
net=init(net);
wts=net.IW{1,1}
bias=net.b{1}
% 改变权值和阈值为随机数
net.inputweights{1,1}.initFcn='rands';
net.biases{1}.initFcn='rands';
net=init(net);
bias=net.b{1}
wts=net.IW{1,1}
a1=sim(net,p1)

代码Part2: 感知器神经网络的学习和训练

% 2. 感知器神经网络的学习和训练
% 1 网络学习
net=newp([-2 2;-2 2],1);
net.b{1}=[0];
w=[1 -0.8]
net.IW{1,1}=w;
p=[1;2];
t=[1];
a=sim(net,p)
e=t-a
help learnp
dw=learnp(w,p,[],[],[],[],e,[],[],[],[],[])
w=w+dw
net.IW{1,1}=w;
a=sim(net,p)


net = newp([0 1; -2 2],1);
P = [0 0 1 1; 0 1 0 1];
T = [0 1 1 1];
Y = sim(net,P)
net.trainParam.epochs = 20;
net = train(net,P,T);
Y = sim(net,P)

% 2 网络训练
net=init(net);
p1=[2;2];t1=0;p2=[1;-2];t2=1;p3=[-2;2];t3=0;p4=[-1;1];t4=1;
net.trainParam.epochs=1;
net=train(net,p1,t1)
w=net.IW{1,1}
b=net.b{1}
a=sim(net,p1)
net=init(net);
p=[[2;2] [1;-2] [-2;2] [-1;1]];
t=[0 1 0 1];
net.trainParam.epochs=1;
net=train(net,p,t);
a=sim(net,p)
net=init(net);
net.trainParam.epochs=2;
net=train(net,p,t);
a=sim(net,p)
net=init(net);
net.trainParam.epochs=20;
net=train(net,p,t);
a=sim(net,p)

代码Part3 : 二输入感知器分类可视化问题

% 3. 二输入感知器分类可视化问题
P=[-0.5 1 0.5 -0.1;-0.5 1 -0.5 1];
T=[1 1 0 1]
net=newp([-1 1;-1 1],1);
plotpv(P,T);
plotpc(net.IW{1,1},net.b{1});
%hold on;
%plotpv(P,T);
net=adapt(net,P,T);
net.IW{1,1}
net.b{1}
plotpv(P,T);
plotpc(net.IW{1,1},net.b{1})
net.adaptParam.passes=3;
net=adapt(net,P,T);
net.IW{1,1}
net.b{1}
plotpc(net.IW{1},net.b{1})
net.adaptParam.passes=6;
net=adapt(net,P,T)
net.IW{1,1}
net.b{1}
plotpv(P,T);
plotpc(net.IW{1},net.b{1})

plotpc(net.IW{1},net.b{1})
%仿真
a=sim(net,p);
plotpv(p,a)

p=[0.7;1.2]
a=sim(net,p);
plotpv(p,a);
hold on;
plotpv(P,T);
plotpc(net.IW{1},net.b{1})
%感知器可以正确分类,从而网络可行。

代码Part4 : 标准化学习规则训练奇异样本

% 4. 标准化学习规则训练奇异样本
P=[-0.5 -0.5 0.3 -0.1 -40;-0.5 0.5 -0.5 1.0 50]
T=[1 1 0 0 1];
net=newp([-40 1;-1 50],1);
plotpv(P,T);%标出全部点
hold on;
linehandle=plotpc(net.IW{1},net.b{1});%画出分类线
E=1;
net.adaptParam.passes=3;%passes决定在训练过程当中训练值重复的次数。
while (sse(E))
    [net,Y,E]=adapt(net,P,T);
    linehandle=plotpc(net.IW{1},net.b{1},linehandle);
    drawnow;
end;
axis([-2 2 -2 2]);
net.IW{1}
net.b{1}
%另一种网络修正学习(非标准化学习规则learnp)
hold off;
net=init(net);
net.adaptParam.passes=3;
net=adapt(net,P,T);
plotpc(net.IW{1},net.b{1});
axis([-2 2 -2 2]);
net.IW{1}
net.b{1}
%没法正确分类
%标准化学习规则网络训练速度要快!

代码Part5 :训练奇异样本

% 训练奇异样本
% 用标准化感知器学习规则(标准化学习数learnpn)进行分类
net=newp([-40 1;-1 50],1,'hardlim','learnpn');
plotpv(P,T);
linehandle=plotpc(net.IW{1},net.b{1});
e=1;
net.adaptParam.passes=3;
net=init(net);
linehandle=plotpc(net.IW{1},net.b{1});
while (sse(e))
[net,Y,e]=adapt(net,P,T);
linehandle=plotpc(net.IW{1},net.b{1},linehandle);
end;
axis([-2 2 -2 2]);
net.IW{1}%权重
net.b{1}%阈值
%正确分类

 %非标准化感知器学习规则训练奇异样本的结果
net=newp([-40 1;-1 50],1);
net.trainParam.epochs=30;
net=train(net,P,T);
pause;
linehandle=plotpc(net.IW{1},net.b{1});
hold on;
plotpv(P,T);
linehandle=plotpc(net.IW{1},net.b{1});
axis([-2 2 -2 2]);

代码Part6 :设计多个感知器神经元解决分类问题

% 5. 设计多个感知器神经元解决分类问题
p=[1.0 1.2 2.0 -0.8; 2.0 0.9 -0.5 0.7]
t=[1 1 0 1;0 1 1 0]
plotpv(p,t);
hold on;
net=newp([-0.8 1.2; -0.5 2.0],2);
linehandle=plotpc(net.IW{1},net.b{1});
net=newp([-0.8 1.2; -0.5 2.0],2);
linehandle=plotpc(net.IW{1},net.b{1});
e=1;
net=init(net);
while (sse(e))
[net,y,e]=adapt(net,p,t);
linehandle=plotpc(net.IW{1},net.b{1},linehandle);
drawnow;
end;

3.代码解释

(部分代码在上述BP神经网络中作过解释)

①.

net.inputweights{1,1}.initFcn='rands';

对前馈网络来讲,有两种不一样的初始化方式常常被用到:initwb和initnw。initwb函数根据每一层本身的初始化参数(net.inputWeights{i,j}.initFcn)初始化权重矩阵和偏置。前馈网络的初始化权重一般设为rands,它使权重在-1到1之间随机取值。这种方式常常用在转换函数是线性函数时。
②.

dw=learnp(w,p,[],[],[],[],e,[],[],[],[],[])

权值和阀值学习函数:learnp
dW=learnp(W,P ,Z,N,A,T,E,D,gW,gA,LP,LS)
dW:权值或阀值的变化矩阵
W:权值矩阵或阀值向量
P:输入向量
T:目标向量
E:偏差向量
其余能够忽略,设为[ ]
learnpn:归一化学习函数
③.

net.adaptParam.passes=6;
net=adapt(net,P,T)

在训练过程当中重复次数为6,passes决定在训练过程当中训练值重复的次数。
adapt函数:返回自适应的神经网络。
④.

axis([-2 2 -2 2]);

axis([xmin xmax ymin ymax])
设置当前图形的坐标范围,分别为x轴的最小、最大值,y轴的最小最大值。
⑤.
plotpv函数:用于在坐标中绘制给定的样本点及其类别
plotpc函数:用于绘制感知器分界线
⑥.

sse(e)

sse是用于判断神经网络的网络偏差是否小于预订值的函数;
E偏差在while循环中能够用:while(sse(E))来限定网络训练的条件。

4.运行结果及分析

代码Part1(感知器神经网络的构建) 运行结果:

>> S

ans =

    -1     1


ans =

     1


p1 =

     1
     1


a1 =

     1


p2 =

     1
    -1


a2 =

     0


p3 =

  1×2 cell 数组

    {2×1 double}    {2×1 double}


a3 =

  1×2 cell 数组

    {[1]}    {[0]}


p4 =

     1     1
     1    -1


a4 =

     1     0


a1 =

     1


wts =

     0     0


bias =

     0


bias =

    0.6294


wts =

    0.8116   -0.7460


a1 =

     1

>> 
>>

代码Part2(感知器神经网络的学习和训练)运行结果:
在这里插入图片描述
代码Part3 (二输入感知器分类可视化问题) 运行结果:在这里插入图片描述
代码Part4 ( 标准化学习规则训练奇异样本) 运行结果:
在这里插入图片描述
另一种网络修正学习(非标准化学习规则learnp):
在这里插入图片描述
代码Part5 (训练奇异样本) 运行结果:
样本分类图:
在这里插入图片描述
在这里插入图片描述
第一部分的图形显示的是神经网络的结构图,可知有一个隐层一个输出层,这里有2输入,1输出。

第二部分显示的是训练算法,这里为训练函数trainc–循环顺序权重/阈值的训练法;偏差指标为MAE。

第三部分显示训练进度:
Epoch:训练次数;在其右边显示的是最大的训练次数,上面例子中为30;而进度条中显示的是实际训练的次数,上面例子中实际训练次数为30次。
Time:训练时间,也就是本次训练中使用的时间。
Performance:性能指标,本例子中为平均绝对偏差(mae)的最大值。精度条中显示的是当前的平均绝对偏差;进度条右边显示的是设定的平均绝对偏差(若是当前的平均绝对偏差小于设定值,则中止训练),这个指标能够用trainParam.goal参数设定。

第四部分为做图。分别点击按钮能看到偏差变化曲线,分别用于绘制当前神经网络的性能图和训练状态。

性能图:
在这里插入图片描述
代码Part6(设计多个感知器神经元解决分类问题)运行结果:
在这里插入图片描述