登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

易拉罐的博客

心静自然凉

 
 
 

日志

 
 

转 利用神经网络进行分类  

2012-04-06 18:14:33|  分类: 神经网络 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

一个经过训练的2输入神经元对5个输入向量进行分类(2)。结合一个例子给出步骤。

实验内容(说明:这里的许多参数都可以更改,希望大家能对这个程序的参数进行修改;同时也欢迎大家提问)

步骤一:

两个长度为5的向量构成输入样本矩阵P,行向量T为指导向量。利用PLOTPV画出这个向量的图像。例如:

P = [-0.5 -0.5 +0.3 -0.1 -4; -0.5 +0.5 -0.5 +1.0 5];

T = [1 1 0 0 1];

plotpv(P,T);//plotpv函数利用感知器的输入向量和监督向量来画输入向量的图像

注意:在上面的式子中,4输入向量比5输入向量有更小的数量级,这个感知器必须把P中的5个输入向量分成两类(依赖于T)。

步骤二 建立神经网络

MATLAB提供函数newp来创建一个指定的感知器。第一个参数指定了期望的两个输入向量的取值范围,第二个参数指定了只有一个神经元。

net = newp([-40 1;-1 50],1);

注意:这个神经元的激励函数是hardlim函数,也就是阶越函数。取01两个值。Hardlim三函数,也就是阶越函数。取-11两个值。

步骤三 添加神经元的初始化值到分类图

初始化的权值被设为0,因此任何输入都会给出同样的输出,并且分类线不会出现在这个图中,不用害怕,我们会继续训练这个神经网。

hold on

linehandle = plotpc(net.IW{1},net.b{1});//plotpc函数用来画神经网络的分类线

步骤四 训练感知器

Matlab提供了adapt函数来训练感知器,adapt函数返回一个新的能更好的执行分类、网络的输出、和误差的神经网络,这个划线函数允许网络从3个角度去调整,画分类线一直到误差为0为止。

E = 1;//E为误差

net.adaptParam.passes = 3;

while (sse(E))//sse函数是用来判定误差E的函数

[net,Y,E] = adapt(net,P,T);//利用输入样本调节神经网net

linehandle = plotpc(net.IW{1},net.b{1},linehandle);//画出调整以后的分类线

drawnow;//延迟一段时间

end

注意:这将会花费感知器的许多时间来训练。这对这样一个简单问题来说时间是非常长的。追究其原因在于outlier vector,尽管需要很长的训练时间,这个感知器仍然适当的学习并且被用于划分别的输入。

步骤模拟sim

SIM函数能被用来划分任何别的输入向量,例如划分一个输入向量[0.7; 1.2].这个新点的图像为红色,他将用来显示这个感知器如何把这个新点从最初的训练集取分开来。

p = [0.7; 1.2];

a = sim(net,p);//利用模拟函数sim计算出新输入p的神经网络的输出

plotpv(p,a);

circle = findobj(gca,'type','line');

set(circle,'Color','red');

打开Hold,以便于以前的图像不被删除。增加训练装置和分类线在图中。

hold on;

plotpv(P,T);

plotpc(net.IW{1},net.b{1});

hold off;

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

最后放大感兴趣的区域。这个感知器正确的区分了我们的新点(用红色表示)作为”zero”(用圆圈表示),而不是”one”(+号表示),尽管需要比较长的训练时间,这个感知器仍然适当的进行了学习。想知道在outlier vectors的情况下如何减少训练时间,需要做实验一的优化实验"Normalized Perceptron Rule"

练习1 熟悉并理解plotpv,plotpc函数

The code below defines and plots the inputs and targets for a perceptron:

p = [0 0 1 1; 0 1 0 1];

t = [0 0 0 1];

plotpv(p,t)

The following code creates a perceptron with inputs ranging over the values in P, assigns values to its weights and biases, and plots the resulting classification line.

net = newp(minmax(p),1);

net.iw{1,1} = [-1.2 -0.5];

net.b{1} = 1;

plotpc(net.iw{1,1},net.b{1})

newp函数解释

NEWP Create a perceptron.

Syntax
net = newp
net = newp(pr,s,tf,lf)

Description
Perceptrons are used to solve simple (i.e. linearly
separable) classification problems.

NET = NEWP creates a new network with a dialog box.

NET = NEWP(PR,S,TF,LF) takes these inputs,
PR - Rx2 matrix of min and max values for R input elements.
S - Number of neurons.
TF - Transfer function, default = 'hardlim'.
LF - Learning function, default = 'learnp'.
Returns a new perceptron.

The transfer function TF can be HARDLIM or HARDLIMS.
The learning function LF can be LEARNP or LEARNPN.

Examples

This code creates a perceptron layer with one 2-element
input (ranges [0 1] and [-2 2]) and one neuron. (Supplying
only two arguments to NEWP results in the default perceptron
learning function LEARNP being used.)

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

Now we define a problem, an OR gate, with a set of four
2-element input vectors P and the corresponding four
1-element targets T.

P = [0 0 1 1; 0 1 0 1];
T = [0 1 1 1];

Here we simulate the network's output, train for a
maximum of 20 epochs, and then simulate it again.

Y = sim(net,P)
net.trainParam.epochs = 20;
net = train(net,P,T);
Y = sim(net,P)

Notes

Perceptrons can classify linearly separable classes in a
finite amount of time. If input vectors have a large variance
in their lengths, the LEARNPN can be faster than LEARNP.

Properties

Perceptrons consist of a single layer with the DOTPROD
weight function, the NETSUM net input function, and the specified
transfer function.

The layer has a weight from the input and a bias.

Weights and biases are initialized with INITZERO.

Adaption and training are done with TRAINS and TRAINC,
which both update weight and bias values with the specified
learning function. Performance is measured with MAE.

 

 

 

实验一的优化 Normalized Perceptron Rule

 

 

一个经过训练的2输入神经元对5个输入向量进行分类(2)。结合一个例子给出步骤。

实验内容:

步骤一:

两个长度为5的向量构成输入样本矩阵P,行向量T为指导向量。利用PLOTPV画出这个向量的图像。例如:

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];

plotpv(P,T);

注意:在上面的式子中,4输入向量比5输入向量有更小的数量级,这个感知器必须把P中的5个输入向量分成两类(依赖于T.

步骤二 建立神经网络

MATLAB提供函数newp来创建一个指定的感知器。第一个参数指定了期望的两个输入向量的取值范围,第二个参数指定了只有一个神经元。第三个参数指定了激励函数,Learnpn对输入向量大小的变化比learnp(默认的)不敏感。

net = newp([-40 1;-1 50],1,'hardlim','learnpn');

第三步 添加神经元的初始化值到分类图

初始化的权值被设为0,因此任何输入都会给出同样的输出,并且分类线不会出现在这个图中,不用害怕,我们会继续训练这个神经网。

hold on

linehandle = plotpc(net.IW{1},net.b{1});

第四步 训练感知器

Matlab提供了adapt函数来训练感知器,adapt函数返回一个新的能更好的执行分类、网络的输出、和误差的神经网络,这个划线函数允许网络从3个角度去调整,画分类线一直到误差为0为止。

注意:这将会花费感知器的许多时间来训练。这对这样一个简单问题来说时间是非常长的。追究其原因在于outlier vector,尽管需要很长的训练时间,这个感知器仍然适当的学习并且被用于划分别的输入。

E = 1;

net.adaptParam.passes = 3;

while (sse(E))

[net,Y,E] = adapt(net,P,T);

linehandle = plotpc(net.IW{1},net.b{1},linehandle);

drawnow;

end

注意:用learnp进行训练需要3个时间单位,而用learnpn解决同样的问题需要32个时间单位,因此,当输入向量的大小发生很大变化时,learnpnlearnp要用的普遍。

5 模拟sim

SIM函数能被用来划分任何别的输入向量,例如划分一个输入向量[0.7; 1.2].这个新点的图像为红色,他将用来显示这个感知器如何把这个新点从最初的训练集取分开来。

p = [0.7; 1.2];

a = sim(net,p);

plotpv(p,a);

circle = findobj(gca,'type','line');

set(circle,'Color','red');

打开Hold,以便于以前的图像不被删除。增加训练装置和分类线在图中。

hold on;

plotpv(P,T);

plotpc(net.IW{1},net.b{1});

hold off;

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

最后放大感兴趣的区域

这个感知器正确的区分了我们的新点(用红色表示)作为”zero”(用圆圈表示),而不是”one”(+号表示),这个感知器在较短的时间内适当的学习。

线性不可分向量

一个2输入神经元对模为5的输入向量不能进行分类。因为他们是线性不可分的。

P = [ -0.5 -0.5 +0.3 -0.1 -0.8;-0.5 +0.5 -0.5 +1.0 +0.0 ];

T = [1 1 0 0 0];

plotpv(P,T);

net = newp([-40 1;-1 50],1);

hold on

plotpv(P,T);

linehandle=plotpc(net.IW{1},net.b{1});

net.adaptParam.passes = 3;

linehandle=plotpc(net.IW{1},net.b{1});

for a = 1:25

[net,Y,E] = adapt(net,P,T);

linehandle = plotpc(net.IW{1},net.b{1},linehandle); drawnow;

end;

注意:误差为0的情况从来不会出现,尽管进行了训练,这个感知器没有变成一个可以接受的分类器。仅仅能进行线性可分数据是感知器的基本限制。

 

  评论这张
 
阅读(810)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018