MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中实现和绘制感知器 (https://www.labfans.com/bbs/showthread.php?t=26269)

poster 2019-12-14 20:13

在MATLAB中实现和绘制感知器
 
我回顾了[URL="http://www.cs.toronto.edu/~hinton/csc321/tutorialcode/perceptron.m"]多伦多[/URL] Perceptron [URL="http://www.cs.toronto.edu/~hinton/csc321/tutorialcode/perceptron.m"]MATLAB代码中的代码[/URL]

该代码是

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],该算法会发生什么?

[B]那么,任何这样的感知器代码对Y = [0,1]的输入数据都无效吗?[/B]

[B] - - - - - - - - - - - - - - -编辑 - - - - - - - - - - ----[/B]

还有一个问题,如果我想绘制[B]划分两类[/B]的[B]线[/B] ,我知道我们可以得到与权重有关的线性方程组求解线,但是我该怎么办?喜欢

% 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???

[B]回答:[/B]

您首先应该了解每个输入的含义:
[LIST][*] X是示例的输入矩阵,大小为M x N,其中M是特征向量的维数,N是样本数。由于用于预测的感知器模型为Y=w*X+b ,因此您必须在X提供一个额外的维,该维是恒定的,通常设置为1 ,因此b项是“内置”到X 。在下面的X的示例中,我将所有样本中X的最后一个条目设置为1 。[*] Y是X每个样本的正确分类(您希望感知器学习的分类),因此它应该是N维行向量-每个输入示例一个输出。由于感知器是[B]二进制[/B]分类器,因此它应仅具有2个不同的可能值。查看代码,您会看到它检查了预测的符号,这告诉您Y的允许值应为-1,+1 (例如,而不是0,1 )。[*] w是您要学习的权重向量。[/LIST]因此,尝试使用以下命令调用该函数:

X=[0 0; 0 1; 1 1]; Y=[1 -1]; w=[.5; .5; .5]; [B]编辑[/B]

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

% 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')

[url=https://stackoverflow.com/questions/4882367]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:19

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.