我回顾了
多伦多 Perceptron
MATLAB代码中的代码
该代码是
function [w] = perceptron(X,Y,w_init) w = w_init; for iteration = 1 : 100 % 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],该算法会发生什么?
那么,任何这样的感知器代码对Y = [0,1]的输入数据都无效吗?
- - - - - - - - - - - - - - -编辑 - - - - - - - - - - ----
还有一个问题,如果我想绘制
划分两类的
线 ,我知道我们可以得到与权重有关的线性方程组求解线,但是我该怎么办?喜欢
% 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???
回答:
您首先应该了解每个输入的含义:
- X是示例的输入矩阵,大小为M x N,其中M是特征向量的维数,N是样本数。由于用于预测的感知器模型为Y=w*X+b ,因此您必须在X提供一个额外的维,该维是恒定的,通常设置为1 ,因此b项是“内置”到X 。在下面的X的示例中,我将所有样本中X的最后一个条目设置为1 。
- Y是X每个样本的正确分类(您希望感知器学习的分类),因此它应该是N维行向量-每个输入示例一个输出。由于感知器是二进制分类器,因此它应仅具有2个不同的可能值。查看代码,您会看到它检查了预测的符号,这告诉您Y的允许值应为-1,+1 (例如,而不是0,1 )。
- w是您要学习的权重向量。
因此,尝试使用以下命令调用该函数:
X=[0 0; 0 1; 1 1]; Y=[1 -1]; w=[.5; .5; .5];
编辑
使用以下代码调用感知器算法,并以图形方式查看结果:
% 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,ytag0),'ro') legend('class -1','class +1','pred -1','pred +1')
更多&回答...