单层神经网络中的matlab语法错误 [英] matlab syntax errors in single layer neural network
问题描述
我必须实现一个单层神经网络或感知器,为此,我有2个文件数据集,一个用于输入,一个用于输出,我必须在matlab中执行此操作而不使用神经工具箱.下面给出2个文件.
I have to implement a single layer neural network or perceptron.For this, I have 2 files data sets , one for the input and one for the output.I have to do this in matlab without using neural toolbox.The format of 2 files is given below.
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.
The target output is "1 for a particular class that the corresponding input belongs to and "0 for the remaining 2 outputs.
我试图这样做,但是它对我不起作用.
I tried to do this, But it is not working for me.
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);
此算法的错误在哪里?
我指的是下面链接中给出的示例:-
I am referring the example given in the link below:-
推荐答案
以下是我认为有误的列表:
Here's a list of what I see wrong:
深呼吸
- 索引语法
(:1)
不正确.也许您的意思是(:,1)
,如列1的所有行"中一样. - 在MATLAB中没有do ... while循环之类的东西.仅 FOR 和不等于" MATLAB中的运算符是
~=
,而不是!=
. - 对向量和矩阵进行索引需要用括号
()
而不是方括号[]
. - 所有这些都在功能中还是脚本?您无法在脚本中定义函数,即
calculateOutput
.您必须将其放在自己的m文件calculateOutput.m
中.如果所有代码实际上都在一个较大的函数中,则calculateOutput
是嵌套函数,并且应该可以正常工作(假设您已经用end
结束了较大的封闭函数). - 对于变量名,您有许多明显的错别字:
- The indexing syntax
(:1)
is incorrect. Perhaps you mean(:,1)
as in "all rows of column 1". - There is no such thing as a do...while loop in MATLAB. Only FOR and WHILE loops. Also, your for loop is defined wrong. MATLAB has a different syntax for that.
- There are no
++
or+=
operators in MATLAB. - The "not equal" operator in MATLAB is
~=
, not!=
. - Indexing of vectors and matrices needs to be done with parentheses
()
, not square brackets[]
. - Is all of this inside a function or a script? You can not define functions, namely
calculateOutput
, in a script. You would have to put that in its own m-filecalculateOutput.m
. If all of the code is actually inside a larger function, thencalculateOutput
is a nested function and should work fine (assuming you have ended the larger enclosing function with anend
). - You have a number of apparent typos for variable names:
weight
vs.weights
(as per phoffer's answer)Count
vs.count
(MATLAB is case-sensitive)calculateOutput
vs.calculateoutput
(again, case-sensitivity)learning rate
vs.learningrate
(variables can't have spaces in them)
大量呼气;)
简而言之,它需要大量工作.
In short, it needs quite a bit of work.
这篇关于单层神经网络中的matlab语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- The indexing syntax