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)
这个算法的错误在哪里?
我指的是以下链接中给出的例子: -
这是我看错了的列表:
深呼吸
(:1)不正确.也许你的意思(:,1)是"第1列的所有行".++或+=运算符.~=不是!=.(),而不是用方括号[].calculateOutput在脚本中.你必须把它放在自己的m文件中calculateOutput.m.如果所有代码实际上都在一个更大的函数中,那么它calculateOutput是一个嵌套函数,应该可以正常工作(假设你已经用a结束了更大的封闭函数end).weight与weights(根据phoffer的回答)Countvs. count(MATLAB区分大小写)calculateOutputvs. calculateoutput(再次,区分大小写)learning ratevs. learningrate(变量不能包含空格)沉重的呼气 ;)
简而言之,它需要相当多的工作.