views:

121

answers:

3

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:-

http://stackoverflow.com/questions/1697243/help-with-perceptron

+1  A: 

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)

phoffer
+7  A: 

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-file calculateOutput.m. If all of the code is actually inside a larger function, then calculateOutput is a nested function and should work fine (assuming you have ended the larger enclosing function with an end).
  • 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)

heavy exhale ;)

In short, it needs quite a bit of work.

gnovice
If calculateOutput is a nested function, the main function needs to be closed with an `end`
Jonas
@Jonas: Thanks, I made that more explicit.
gnovice
+1  A: 

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
Jonas
Great idea doing a rewrite of it, but MATLAB does not use `+=` either. You have to do `weights[0] = weights[0] + learningrate * localerror * x[p]`
phoffer
@Jonas, SORRY, I completely read over the last part of your comment block. I moved the comment to my answer and credited you.
phoffer

related questions