0
我必须使用Matlab实现单层感知器。如何返回到矩阵的第一个索引
我面临的问题是,当我运行我的程序时,它给了我每个输入的输出(它显示结果4次),但我想回到矩阵的第一个索引,当达到第四,我不知道如何回到矩阵的第一个索引。
我想训练我的程序,以便通过在每个循环中迭代矩阵,得到与b
相同的结果。
这是我当前的代码:
a = [ 1 1
1 -1
-1 1
-1 -1 ];
b = [1
-1
-1
-1];
disp(a);
disp(b);
x = a(:,1);
disp(x);
y = a(:,2);
disp(y)
learningrate = 0.1;
maxiteration = 10;
weight(1)=0.1;
weight(2)=0.1;
weight(3)=0.1;
count = length(x);
for p = 1:count
s = (x(p) * weight(1))+ (y(p) * weight(2))+ weight(3);
if s >= 0
result = 1;
if result ~= b(p)
weight(1) = weight(1)+learningrate*(b(p)-result)*x(p);
weight(2) = weight(2)+learningrate*(b(p)-result)*y(p);
weight(3) = weight(3)+learningrate*(b(p)-result);
disp(result);
disp(x(p));
disp(y(p));
disp(weight(1));
disp(weight(2));
disp(weight(3));
end
else
if s <= 0
result = -1;
disp(result);
if result ~= b(p)
weight(1)=weight(1)+learningrate*(b(p)-result)*x(p);
weight(2)=weight(2)+learningrate*(b(p)-result)*y(p);
weight(3)=weight(3)+learningrate*(b(p)-result);
disp(x(p));
disp(y(p));
disp(weight(1));
disp(weight(2));
disp(weight(3));
end
end
end
end
看看http://stackoverflow.com/questions/3455660/matlab-single-layer-neural-network – zellus 2010-11-13 09:30:10