【问题标题】:matlab syntax errors in single layer neural network [closed]单层神经网络中的matlab语法错误[关闭]
【发布时间】:2011-03-27 14:32:56
【问题描述】:

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

 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 表示对应输入所属的特定类,”0 表示其余 2 个输出。

我试过这样做,但它不适合我。

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

【问题讨论】:

  • 这还不是算法错误。这是语法错误Count ≢ count
  • 从您的代码中可以清楚地看出,您还没有准备好一次尝试一个完整的解决方案。我建议您从伪代码算法开始,逐步独立地实现每个步骤。如果您不知道如何编写循环或递增变量,那么尝试编写完整的程序并不是一种富有成效的学习体验。

标签: matlab neural-network


【解决方案1】:

这是我看到的错误列表:

深呼吸

  • 索引语法(:1) 不正确。也许您的意思是(:,1),就像“第 1 列的所有行”一样。
  • 在 MATLAB 中没有 do...while 循环之类的东西。只有FORWHILE 循环。此外,您的 for 循环定义错误。 MATLAB 对此有不同的语法。
  • MATLAB 中没有 +++= 运算符。
  • MATLAB 中的"not equal" operator~=,而不是!=
  • Indexing of vectors and matrices 需要用括号 () 完成,而不是方括号 []
  • 所有这些都在functionscript 中吗?您不能在脚本中定义函数,即calculateOutput。你必须把它放在它自己的 m 文件 calculateOutput.m 中。如果所有代码实际上都在一个更大的函数中,那么calculateOutput 是一个nested function 并且应该可以正常工作(假设您已经用end 结束了更大的封闭函数)。
  • 变量名称有许多明显的拼写错误:
    • weightweights(根据 phoffer's answer
    • Countcount(MATLAB 区分大小写)
    • calculateOutputcalculateoutput(再次区分大小写)
    • learning rate vs. learningrate(变量中不能有空格)

重重呼气 ;)

简而言之,它需要相当多的工作。

【讨论】:

  • 如果calculateOutput是一个嵌套函数,主函数需要用end关闭
  • @Jonas:谢谢,我说得更清楚了。
【解决方案2】:

在第 10 行,您有 weights(1) +weight(2) +weight(3);但其余代码有weightss

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

for p=0:count
    blah blah blah
end

另外,正如 Jonas 在他的代码中指出的那样,MATLAB 也不使用 += 运算符。你需要这样做:

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

【讨论】:

    【解决方案3】:

    主要的错误是这不是使用 Matlab 语法编写的。这是我认为您正在尝试做的尝试。

    不幸的是,您的算法存在根本问题(请参阅代码中的 cmets)。另外,我认为您应该看看非常好的 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
    

    【讨论】:

    • 重写它的好主意,但 MATLAB 也不使用+=。你必须做weights[0] = weights[0] + learningrate * localerror * x[p]
    • 对不起,我完全阅读了您评论块的最后一部分。我将评论移至我的答案并记入了您。
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 2011-03-28
    • 2016-01-22
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多