问题
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
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);
Where is the mistake in this algorithm??
I am referring the example given in the link below:-
Perceptron learning algorithm not converging to 0
回答1:
Here's a list of what I see wrong:
deep breath
- 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, thencalculateOutputis 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:
weightvs.weights(as per phoffer's answer)Countvs.count(MATLAB is case-sensitive)calculateOutputvs.calculateoutput(again, case-sensitivity)learning ratevs.learningrate(variables can't have spaces in them)
heavy exhale ;)
In short, it needs quite a bit of work.
回答2:
The main mistake is that this is not written using Matlab syntax. Here is an attempt to do what I think you were trying to do.
Unfortunately, there is a fundamental problem with your algorithm (see comments in the code). Also, I think you should have a look at the very good Matlab documentation. Reading the manual will tell you quickly how you format this.
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
回答3:
On line #10, you have weights(1) +weight(2) +weight(3); but the rest of the code has weights with an s.
EDIT: Also, MATLAB does not have the ++ operator; your for loop will cause an error. In MATLAB, construct a for loop like this:
for p=0:count
blah blah blah
end
Also, MATLAB does not use the += operator either, as Jonas pointed out in his code. You need to do this:
weights(0) = weights(0) + learningrate * localerror * x(p)
来源:https://stackoverflow.com/questions/3445484/matlab-syntax-errors-in-single-layer-neural-network