Sunday, November 29, 2015

Perceptron leaning to classify boolean AND function with the perceptron training rule - Java

A perceptron can be trained to classify inputs according to the AND boolean function.

You need to set the bias accurately, otherwise the learning function will never converge. The code only contains the AND function, you can modify the input for other functions accordingly.

I've represented 0 with -1, 1 with 1 itself.

class Example{
    int x1,x2;
    int o;
    
    Example(int a, int b, int c){
        x1 = a;x2 = b;o = c;
    }
}

class Perceptron {
    double w[];
    double n = 0.1;
    
    Perceptron(){
        w = new double[]{-0.1, -0.9, 0.7};
    }
    
    //Getting the perceptron's output
    int processInput(double x1, double x2){ 
        double result = 1*w[0] + x1*w[1]+x2*w[2];
        if(result>0)return 1;
        else return -1;
    }
    
    //Train using perceptron training rule
    void train(Example[] examples){
        while(true){
            int count=0;
            for(Example e:examples){ 
                int o = processInput(e.x1, e.x2);
                int t = e.o;
                
                if(o!=t)System.out.println("\n"+e.x1+" "+e.x2+" : "+o);
                if(t==o)count++;
                 
                w[1] = w[1] + n*(t-o)*e.x1;        
                w[2] = w[2] + n*(t-o)*e.x2;
                
                if(o!=t)System.out.println("New weights = "+w[1]+" "+w[2]);
            }
            if(count==4)break;
        }
        System.out.println("Perceptron training complete.");
        System.out.println("Learned weights : [w1, w2] = ["+w[1]+", "+w[2]+"]");
    }
    
    public static void main(String[] args){
        Perceptron perceptron = new Perceptron();
        
        Example[] examples = new Example[4];
        examples[0] = new Example(-1, -1, -1);
        examples[1] = new Example(-1, 1, -1);
        examples[2] = new Example(1, -1, -1);
        examples[3] = new Example(1, 1, 1);
       
        perceptron.train(examples);
    }
}

class Example{
    int x1,x2;
    int o;
    
    Example(int a, int b, int c){
        x1 = a;x2 = b;o = c;
    }
}

class Perceptron {
    double w[];
    double n = 0.1;
    
    Perceptron(){
        w = new double[]{-0.1, -0.9, 0.7};
    }
    
    //Getting the perceptron's output
    int processInput(double x1, double x2){ 
        double result = 1*w[0] + x1*w[1]+x2*w[2];
        if(result>0)return 1;
        else return -1;
    }
    
    //Train using perceptron training rule
    void train(Example[] examples){
        while(true){
            int count=0;
            for(Example e:examples){ 
                int o = processInput(e.x1, e.x2);
                int t = e.o;
                
                if(o!=t)System.out.println("\n"+e.x1+" "+e.x2+" : "+o);
                if(t==o)count++;
                 
                w[1] = w[1] + n*(t-o)*e.x1;        
                w[2] = w[2] + n*(t-o)*e.x2;
                
                if(o!=t)System.out.println("New weights = "+w[1]+" "+w[2]);
            }
            if(count==4)break;
        }
        System.out.println("Perceptron training complete.");
        System.out.println("Learned weights : [w1, w2] = ["+w[1]+", "+w[2]+"]");
    }
    
    public static void main(String[] args){
        Perceptron perceptron = new Perceptron();
        
        Example[] examples = new Example[4];
        examples[0] = new Example(-1, -1, -1);
        examples[1] = new Example(-1, 1, -1);
        examples[2] = new Example(1, -1, -1);
        examples[3] = new Example(1, 1, 1);
       
        perceptron.train(examples);
    }
}

Output:

-1 -1 : 1
New weights = -0.7 0.8999999999999999

-1 1 : 1
New weights = -0.49999999999999994 0.7

-1 1 : 1
New weights = -0.29999999999999993 0.49999999999999994

-1 1 : 1
New weights = -0.09999999999999992 0.29999999999999993

-1 1 : 1
New weights = 0.10000000000000009 0.09999999999999992
Perceptron training complete.
Learned weights : [w1, w2] = [0.10000000000000009, 0.09999999999999992]

No comments:

Post a Comment