Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单层神经网络中的matlab语法错误_Matlab_Neural Network - Fatal编程技术网

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

单层神经网络中的matlab语法错误,matlab,neural-network,Matlab,Neural Network,我必须实现单层神经网络或感知器。为此,我有两个文件数据集,一个用于输入,一个用于输出。我必须在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” 我试

我必须实现单层神经网络或感知器。为此,我有两个文件数据集,一个用于输入,一个用于输出。我必须在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);
load in.data
加载数据
x=in(:1);
y=in(:2);
学习率=0.2;
最大迭代次数=50;
函数结果=计算输出(权重,x,y)
s=x*(权重(1)+权重(2)+权重(3));
如果s>=0
结果=1
其他:
结果=-1
结束
结束
计数=长度(x);
权重[0]=rand();
权重[1]=rand();
权重[2]=rand();
iter=0;
做{
iter++;
全局错误=0;
对于第10行中的(p=0;p,您有
权重(1)+权重(2)+权重(3)
;但代码的其余部分有
权重
,带有
s

编辑:另外,MATLAB没有
++
运算符;您的
for
循环将导致错误。在MATLAB中,为
构建一个
for
循环,如下所示:

for p=0:count
    blah blah blah
end
此外,正如Jonas在其代码中指出的那样,MATLAB也不使用
+=
运算符


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

以下是我看到的错误列表:

深呼吸

  • 索引语法
    (:1)
    不正确。您的意思可能是
    (:,1)
    如“列1的所有行”中所示
  • MATLAB中没有do…while循环。只有and循环。另外,for循环定义错误。MATLAB对此有不同的语法
  • MATLAB中没有
    ++
    +=
    运算符
  • MATLAB中的参数是
    ~=
    ,而不是
    !=
  • 需要使用括号
    ()
    ,而不是方括号
    []
  • 所有这些都在a或a中吗?您不能在脚本中定义函数,即
    calculateOutput
    。您必须将其放入自己的m文件
    calculateOutput.m
    。如果所有代码实际上都在一个更大的函数中,那么
    calculateOutput
    是a,应该可以正常工作(假设您用
    结束
    结束了较大的封闭函数)
  • 变量名有许多明显的拼写错误:
    • 重量
      重量
      (根据)
    • Count
      Count
      (MATLAB区分大小写)
    • calculateOutput
      vs.
      calculateOutput
      (同样,区分大小写)
    • 学习率
      学习率
      (变量中不能有空格)
呼气沉重;)


简而言之,它需要做大量的工作。

主要的错误是,这不是用Matlab语法编写的。下面是一个尝试,我认为您正在尝试做的事情

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

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
函数神经网络
%#加载数据
加载数据
加载数据
x=in(:,1);
y=in(:,2);
%#设置常数
学习率=0.2;
最大迭代次数=50;
%初始化参数
计数=长度(x);
weights=rand(1,3);%创建具有随机权重的1×3数组
iter=0;
而全局错误~=0&&iter=0
结果=1
其他:
结果=-1
结束
结束

这还不是一个算法错误。这是一个语法错误
Count≢ count
从您的代码中可以清楚地看出,您还没有准备好一次尝试一个完整的解决方案。我建议您从伪代码算法开始,以增量和独立的方式执行每个步骤。如果您不知道如何编写循环或增量变量,尝试编写一个完整的程序并不是一个有成效的学习g经验。重写它是个好主意,但MATLAB也不使用
+=
。你必须做
权重[0]=weights[0]+learningrate*localerror*x[p]
对不起,我已经完整地阅读了您的注释块的最后一部分。我将注释移到了我的答案中,并向您表示感谢。如果calculateOutput是一个嵌套函数,则需要使用
结尾来关闭主函数。
@Jonas:谢谢,我已将其更加明确。