2010-08-10 20 views
-5

我必须实现单层神经网络或感知器。为此,我有2个文件数据集,一个用于输入,另一个用于输出。我必须在matlab中执行此操作而不使用神经工具箱。下面给出2个文件的格式。单层神经网络中的matlab语法错误

In: 
    0.832 64.643 
    0.818 78.843 
    1.776 45.049 
    0.597 88.302 
    1.412 63.458 


Out: 
0 0 1 
0 0 1 
0 1 0 
0 0 1 
0 0 1 

目标输出对于相应输入所属的特定类为“1”,对于其余2个输出为“0”。

我试图做到这一点,但它不适合我。

load in.data 
load out.data 
x = in(:1); 
y = in(:2); 

learning rate = 0.2; 
max_iteration = 50; 

function result = calculateOutput(weights,x, y) 
s = x*(weights(1) +weight(2) +weight(3)); 
if s>=0 
result = 1 
else: 
result = -1 
end 
end 

Count = length(x); 
weights[0] = rand(); 
weights[1] = rand(); 
weights[2] = rand(); 

iter = 0; 
do { 
    iter++; 
    globalerror = 0; 
    for(p=0; p<count;p++){ 
    output = calculateoutput(weights,x[p],y[p]); 
    localerror = output[p] - output 
    weights[0]+= learningrate *localerror*x[p]; 
    weights[1]+= learningrate *localerror*y[p]; 
    weights[2]+= learningrate *localerror; 
    globalerror +=(localerror*localerror); 
    } 
}while(globalerror != 0 && iter <= max_iteration); 

这个算法的错误在哪里?

我指的是在下面的链接中给出的示例: -

Perceptron learning algorithm not converging to 0

+0

这不是算法错误(还)。这是一个语法错误'Count≢count' – msw 2010-08-10 02:09:27

+7

从您的代码中可以清楚地看到,您还没有准备好一次尝试完整的解决方案。我建议你从伪代码算法开始,逐步实现每一步。如果你不知道如何编写一个循环或增加一个变量,试图编写一个完整的程序不是一个有效的学习体验。 – 2010-08-10 02:36:57

回答

9

这里是我所看到的错误列表:

深呼吸

  • 索引语法(:1)不正确。也许你的意思是(:,1),如“列1的所有行”所示。
  • 在MATLAB中没有do ... while循环这样的事情。只有FORWHILE循环。另外,您的for循环定义错误。 MATLAB具有不同的语法。
  • MATLAB中没有+++=运算符。
  • MATLAB中的"not equal" operator~=而不是!=
  • Indexing of vectors and matrices需要用圆括号(),而不是方括号[]
  • 这一切都在functionscript?您无法在脚本中定义函数,即calculateOutput。你将不得不把它放在它自己的m文件calculateOutput.m。如果所有的代码实际上都在一个更大的函数中,那么calculateOutputnested function,并且应该可以正常工作(假设你已经用end结束了更大的封闭函数)。
  • 您有变量名的数表观错别字:
    • weightweights(按照phoffer's answer
    • Countcount(MATLAB是区分大小写)
    • calculateOutputcalculateoutput(再次,区分大小写)
    • learning ratelearningrate(变量中不能有空格)

重呼气;)

总之,它需要相当多的工作。

+1

如果calculateOutput是一个嵌套函数,那么主函数需要用'end'来结束@ – Jonas 2010-08-10 02:46:15

+0

@Jonas:谢谢,我说得更加明确。 – gnovice 2010-08-10 03:01:18

1

线#10,你有weights(1) +weight(2) +weight(3);但其余的代码有weightss

编辑:此外,MATLAB没有++运算符;您的for循环将导致错误。在MATLAB中,构造一个for循环是这样的:

for p=0:count 
    blah blah blah 
end 

此外,MATLAB并不要么使用+=运算符,如乔纳斯在他的代码中指出。你需要这样做:

weights(0) = weights(0) + learningrate * localerror * x(p)

1

主要的错误是,这不是使用Matlab语法编写的。这是尝试做我认为你想要做的事情。

不幸的是,您的算法存在一个基本问题(请参阅代码中的注释)。另外,我认为你应该看看非常好的Matlab文档。阅读manual会很快告诉你如何格式化。

function neuralNetwork 

%# load data 
load in.data 
load out.data 
x = in(:,1); 
y = in(:,2); 

%# set constants 
learningrate = 0.2; 
max_iteration = 50; 

% initialize parameters 
count = length(x); 
weights = rand(1,3); % creates a 1-by-3 array with random weights 

iter = 0; 
while globalerror ~= 0 && iter <= max_iteration 
    iter = iter + 1; 
    globalerror = 0; 
    for p = 1:count 
    output = calculateOutput(weights,x(p),y(p)); 

    %# the following line(s) cannot possibly work 
    %# output is not a vector, since the previous line 
    %# assigns it to a scalar 
    %# Also, arrays are accessed with parentheses 
    %# and indexing starts at 1 
    %# and there is no += operator in Matlab 
    localerror = output[p] - output 
    weights[0]+= learningrate *localerror*x[p]; 
    weights[1]+= learningrate *localerror*y[p]; 
    weights[2]+= learningrate *localerror; 
    globalerror +=(localerror*localerror); 
end %# for-loop 
end %# while-loop 


%# subfunctions in Matlab are put at the end of the file 
function result = calculateOutput(weights,x, y) 
s = x*(weights(1) +weight(2) +weight(3)); 
if s>=0 
result = 1 
else: 
result = -1 
end 
end 
+0

重写它的好主意,但MATLAB也不使用'+ ='。你必须做'权重[0] =权重[0] +学习率* localerror * x [p]' – 2010-08-10 02:30:16

+0

SORRY,我完全阅读了评论块的最后部分。我将评论转移到了我的答案中,并将您记入了评论。 – 2010-08-10 02:34:29