在MATLAB中实现和绘制感知器 [英] Implementing and ploting a perceptron in MATLAB

查看:241
本文介绍了在MATLAB中实现和绘制感知器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看多伦多感知器MATLAB代码中的代码

I´m reviewing a code from Toronto perceptron MATLAB code

代码是

function [w] = perceptron(X,Y,w_init)

w = w_init;
for iteration = 1 : 100  %<- in practice, use some stopping criterion!
  for ii = 1 : size(X,2)         %cycle through training set
    if sign(w'*X(:,ii)) ~= Y(ii) %wrong decision?
      w = w + X(:,ii) * Y(ii);   %then add (or subtract) this point to w
    end
  end
  sum(sign(w'*X)~=Y)/size(X,2)   %show misclassification rate
end

因此,我正在阅读如何将此函数应用于数据矩阵X和目标Y,但是我不知道如何使用此函数,它会返回权重向量,因此可以进行分类.

So I was reading how to apply this function to data matrix X, and target Y, but, do not know how to use this function, I understand, it returns a vector of weights, so it can classify.

请给我一个例子,并解释一下吗?

Could you please give an example, and explain it??

我尝试过

X=[0 0; 0 1; 1 1]
Y=[1 0; 2 1]
w=[1 1 1]
Result = perceptron( X, Y, w )

??? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> perceptron at 15
            if sign(w'*X(:,ii)) ~= Y(ii) 

    Result = perceptron( X, Y, w' )

??? Error using ==> ne
Matrix dimensions must agree.

Error in ==> perceptron at 19
        sum(sign(w'*X)~=Y) / size(X,2);     

谢谢

谢谢您的回答,我又得到了一个答案.如果我更改Y = [0,1],该算法会发生什么?

Thank you for the anwers, I got one more, If I change the Y = [0, 1], what happens to the algorithm?.

那么,使用感知器的代码,Y = [0,1]时,任何输入数据都将不起作用吗?

-----------------------------编辑-------------- ----------

还有一个问题,如果我想绘制将两类划分的线,我知道我们可以得到与权重有关的求解线性方程组的线,但是, 我该怎么办?,我正在尝试类似

One more question, if I want to plot the line that divides the 2 classes, I know we can get that the line solving linear equation system that has to do with weights, but how, what could I do?, I'm trying something like

% the initial weights
w_init = [ 1 1 1]';  
% the weights returned from perceptron    
wtag   = perceptron(X,Y,w_init,15);

% concatenate both
Line = [wtag,w_init] 

% solve the linear system, am I correct doing this?
rref(Line')

% plot???

推荐答案

您应该首先了解每个输入的含义:

You should first understand what is the meaning of each of the inputs:

  • X是示例的输入矩阵,大小为M x N,其中M是特征向量的维数,N是样本数.由于用于预测的感知器模型为Y=w*X+b,因此您必须在X中提供一个额外的维,该维是恒定的,通常设置为1,因此b项是内置"到X中的.在下面的X示例中,我将所有示例中X的最后一个条目设置为1.
  • YX中每个样本的正确分类(您希望感知器学习的分类),因此它应该是N维行向量-每个输入示例一个输出.由于感知器是二进制分类器,因此它应仅具有2个不同的可能值.在代码中,您会看到它检查了预测的符号,这告诉您Y的允许值应为-1,+1(而不是例如0,1).
  • w是您要学习的权重向量.
  • X is the input matrix of examples, of size M x N, where M is the dimension of the feature vector, and N the number of samples. Since the perceptron model for prediction is Y=w*X+b, you have to supply one extra dimension in X which is constant, usually set to 1, so the b term is "built-in" into X. In the example below for X, I set the last entry of X to be 1 in all samples.
  • Y is the correct classification for each sample from X (the classification you want the perceptron to learn), so it should be a N dimensional row vector - one output for each input example. Since the perceptron is a binary classifier, it should have only 2 distinct possible values. Looking in the code, you see that it checks for the sign of the prediction, which tells you that the allowed values of Y should be -1,+1 (and not 0,1 for example).
  • w is the weight vector you are trying to learn.

因此,尝试使用以下命令调用该函数:

So, try to call the function with:

X=[0 0; 0 1; 1 1];
Y=[1 -1];
w=[.5; .5; .5];

编辑

使用以下代码调用感知器算法,并以图形方式查看结果:

Use the following code to call the perceptron alg and see the results graphically:

% input samples
X1=[rand(1,100);rand(1,100);ones(1,100)];   % class '+1'
X2=[rand(1,100);1+rand(1,100);ones(1,100)]; % class '-1'
X=[X1,X2];

% output class [-1,+1];
Y=[-ones(1,100),ones(1,100)];

% init weigth vector
w=[.5 .5 .5]';

% call perceptron
wtag=perceptron(X,Y,w);
% predict
ytag=wtag'*X;


% plot prediction over origianl data
figure;hold on
plot(X1(1,:),X1(2,:),'b.')
plot(X2(1,:),X2(2,:),'r.')

plot(X(1,ytag<0),X(2,ytag<0),'bo')
plot(X(1,ytag>0),X(2,ytag>0),'ro')
legend('class -1','class +1','pred -1','pred +1')

这篇关于在MATLAB中实现和绘制感知器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆