单层神经网络中的matlab语法错误

use*_*981 -5 matlab neural-network

我必须实现单层神经网络或感知器.为此,我有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
Run Code Online (Sandbox Code Playgroud)

目标输出为"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);
Run Code Online (Sandbox Code Playgroud)

这个算法的错误在哪里?

我指的是以下链接中给出的例子: -

感知器学习算法不收敛到0

gno*_*ice 9

这是我看错了的列表:

深呼吸

  • 索引语法(:1)不正确.也许你的意思(:,1)是"第1列的所有行".
  • 在MATLAB中没有do ... while循环.只有FORWHILE循环.此外,您的for循环定义错误.MATLAB有不同的语法.
  • MATLAB 中没有+++=运算符.
  • MATLAB中的"不等于"运算符~=不是!=.
  • 矢量和矩阵的索引需要用括号来完成(),而不是用方括号[].
  • 所有这些都在函数脚本中吗?您无法定义函数,即calculateOutput在脚本中.你必须把它放在自己的m文件中calculateOutput.m.如果所有代码实际上都在一个更大的函数中,那么它calculateOutput是一个嵌套函数,应该可以正常工作(假设你已经用a结束了更大的封闭函数end).
  • 你有一些明显的变量名称拼写错误:
    • weightweights(根据phoffer的回答)
    • Countvs. count(MATLAB区分大小写)
    • calculateOutputvs. calculateoutput(再次,区分大小写)
    • learning ratevs. learningrate(变量不能包含空格)

沉重的呼气 ;)

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