0

I am programming a simple perceptron in matlab but it is not converging and I can't figure out why.

The goal is to binary classify 2D points.

%P1 Generate a dataset of two-dimensional points, and choose a random line in
%the plane as your target function f, where one side of the line maps to +1 and
%the other side to -1. Let the inputs xn 2 R2 be random points in the plane,
%and evaluate the target function f on each xn to get the corresponding output
%yn = f(xn).

clear all;
clc
clear

n = 20;
inputSize = 2; %number of inputs 
dataset = generateRandom2DPointsDataset(n)';
[f , m , b] = targetFunction();
signs = classify(dataset,m,b);
weights=ones(1,2)*0.1;
threshold = 0;
fprintf('weights before:%d,%d\n',weights);
mistakes = 1;
numIterations = 0;


figure;
plotpv(dataset',(signs+1)/2);%mapping signs from -1:1 to 0:1 in order to use plotpv
hold on;
line(f(1,:),f(2,:));
pause(1)

while true
    mistakes = 0;
    for i = 1:n   
        if dataset(i,:)*weights' > threshold
            result = 1;
        else
            result = -1;
        end

        error = signs(i) - result;
        if error ~= 0
            mistakes = mistakes + 1;
            for j = 1:inputSize
                weights(j) = weights(j) + error*dataset(i,j);
            end
        end  

        numIterations = numIterations + 1

    end

    if mistakes == 0
        break

    end


end
fprintf('weights after:%d,%d\n',weights);

random points and signs are fine since plotpv is working well enter image description here

The code is based on that http://es.mathworks.com/matlabcentral/fileexchange/32949-a-perceptron-learns-to-perform-a-binary-nand-function?focused=5200056&tab=function.

When I pause the infinite loop, this is the status of my vairables: enter image description here

I am not able to see why it is not converging.

Additional code( it is fine, just to avoid answers asking for that )

function [f,m,b] = targetFunction()

    f = rand(2,2);
    f(1,1) = 0;
    f(1,2) = 1;

    m = (f(2,2) - f(2,1)); 
    b = f(2,1);  
end


function dataset = generateRandom2DPointsDataset(n)

    dataset = rand(2,n);

end

function values = classify(dataset,m,b)

    for i=1:size(dataset,1)

         y = m*dataset(i,1) + b;
         if dataset(i,2) >= y, values(i) = 1;
         else values(i) = -1; 
         end

    end

end
Charles
  • 98
  • 8
  • Why are you using an infinite loop? That's a no-no in any programming language in any situation... – Wolfie Oct 18 '17 at 15:14
  • Infinite loop is used in many situations, this is one of them. Because the train dataset I created has linear separability, the perceptron should converge, mistakes should be 0 at some point triggering the break condition. – Charles Oct 19 '17 at 07:11
  • I was suggesting your loop had no break condition, so will always be infinite. *While* loops are used in many situations, but they should always have some failsafe escape mechanism like max iterations. You need to add some tolerance on your `error` value, because it will likely *never* satisfy `error == 0`, [because it's a floating point number](https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab) if nothing else! Instead you should use `if abs(error) < 1e-5` or some other suitably small tolerance value. – Wolfie Oct 19 '17 at 08:23
  • You are right only in one thing, max iterations is a must have in order to control integrity, but all the other things simply do not apply in this context. Mistakes are stored like an int, error came from an int substraction triggered by a boolean condition, so there is not a floating point equality anywhere! I appreciate your help but this is not the issue! Thanks anyway. – Charles Oct 19 '17 at 16:48

0 Answers0