Algoritma Perceptron untuk Pengenalan Huruf - Elang Sakti
Download Ebook Belajar Arduino PDF, Arduino untuk pemula
Jasa Pembuatan Program Arduino, pemrograman Arduino
# Hack Your Skills! to be Professional Mechatronics

Algoritma Perceptron untuk Pengenalan Huruf

1 komentar
contoh source code algoritma perceptron pengenalan huruf
Bagian script JST perceptron untuk pengenalan huruf

Proses pembelajaran perceptron memang tergolong sederhana, lebih sederhana dari algoritma bacpropagation, tapi lebih sulit daripada aturan Hebb. Source code ini, digunakan untuk pembelajaran huruf kapital T, L, dan F dengan sampel 5x5, atau bisa diidentikkan dengan 5x5 pixel jika diibaratkan dalam bentuk citra. Akan tetapi bisa dikembangkan untuk huruf-huruf lainnya.

Dalam kasus ini, ada 25 neuron input dan 2 neuron output. Dan masing-masing input mempelajari 9 kombinasi data. Bobot dan bias awal adalah 0, learning rate dan minimal eror masing-masing 0.8 dan 0.3. 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ai;

import java.text.DecimalFormat;

/**
 *
 * @author x86
 */
public class perceptron {
    double x[][]; // input
    double w[][]; // bobot
    double t[][]; // target
    double b[];   // bias
    double alfa;  // learning rate
    double teta;  // treshold
    DecimalFormat format = new DecimalFormat("#.00");
    
    String format(double d){
        return this.format.format(d);
    }
    
    void set_input(double x[][]){
        this.x = x;
    }
    
    void set_target(double t[][]){
        this.t = t;
    }
    
    void set_bias(double b[]){
        this.b = b;
    }
    
    void set_alfa(double alfa){
        this.alfa = alfa;
    }
    
    void set_teta(double teta){
        this.teta = teta;
    }
    
    void init_bobot(){
        this.w = new double[this.x[0].length][this.t[0].length];
        for(int i=0; i<this.w.length; i++){
            for(int j=0; j<this.w[0].length; j++){
                this.w[i][j] = 0;
            }
        }
    }
    
    double aktivasi(double in){
        if(in > this.teta){
            return 1;
        }else if( in < -this.teta){
            return -1;
        }else{
            return 0;
        }
    }
    
    boolean check(double out[][]){
        boolean tmp = true;
        for(int i=0; i<this.t.length; i++){
            for(int j=0; j<this.t[0].length; j++){
                tmp = (tmp && (this.t[i][j]==out[i][j]));
            }
        }
        return tmp;
    }
    
    void learn(){
        System.out.println("Inisialisasi");
        this.print_bobot_bias();
        double y_in, y = 0;
        double Y[][] = new double[this.t.length][this.t[0].length];
        boolean loop = false;
        int counter=0;
        while(!loop){
            System.out.println("\nEPOH "+ counter);
            for(int i=0; i<this.x.length; i++){
                
                this.print_input(x[i]);
                
                double tmp_yin[] = new double[this.t[0].length];
                double tmp_y[] = new double[this.t[0].length];
                
                boolean sama = true;
                for(int k=0; k<this.t[0].length; k++){
                    double tmp = 0;
                    for(int j=0; j<this.x[0].length; j++){
                        tmp += this.x[i][j] * this.w[j][k];
                    }
                    y_in    = this.b[k] + tmp;
                    y       = this.aktivasi(y_in);
                    Y[i][k] = y;
                    
                    tmp_yin[k] = y_in;
                    tmp_y[k] = y;
                    
                    sama = sama && (y == this.t[i][k]);
                }
                
                if(!sama){
                    for(int k=0; k<this.t[0].length; k++){
                        for(int j=0; j<this.x[0].length; j++){
                            this.w[j][k] += this.alfa * this.t[i][k] * this.x[i][j];
                        }
                        this.b[k] += this.alfa * this.t[i][k];
                    }
                }
                
                this.print_bobot_bias();
            }
            loop = this.check(Y);
            counter++;
            System.out.println("EPOH : "+counter);
        }
    }
    
    void test(double input[]){
        System.out.println("TESTIONG : ");
        double ax[] = new double[this.t.length];
        for(int i=0; i<this.t[0].length; i++){
            double tmp = 0;
            String ex = "";
            for(int j=0; j<input.length; j++){
                tmp += input[j] * this.w[j][i];
                ex += input[j] +"*"+ this.w[j][i]+" + ";
            }
            double y_out = tmp + this.b[i];
            System.out.println("Y_in = "+ this.b[i] +" + "+ ex);
            double y = this.aktivasi(y_out);
            System.out.println("Y = "+ y);
            ax[i] = y;
        }
        String hasil = this.huruf(ax);
        System.out.println("OUPUT : "+ hasil);
    }
    
    String huruf(double out[]){
        String str = "";
        if(out[0]==1 && out[1]==1){
            str = "T";
        }else if(out[0]== 1 && out[1]==-1){
            str = "L";
        }else if(out[0]== -1 && out[1]==1){
            str = "F";
        }else{
            str = "BUKAN T";
        }
        return str;
    }
    
    void print_input(double in[]){
        System.out.println("INPut : ");
        for(int i=0; i<in.length; i++){
            System.out.print((int)(in[i]));
        }
        System.out.println();
    }
    
    void print_bobot_bias(){
        System.out.println("BOBOT : ");
        for(int i=0; i<this.w[0].length; i++){
            for(int j=0; j<this.w.length; j++){
                System.out.print(this.format(this.w[j][i])+ " ");
            }
            System.out.println();
        }
        System.out.println("BIAS : ");
        for(int i=0; i<this.b.length; i++){
            System.out.println(this.format(this.b[i])+" ");
        }
        System.out.println();
    }
    
    public static void main(String hx[]){
        double x[][] = {{1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0}, //T
                        {1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0}, //T
                        {1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,1,0}, //T
                        {1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1}, //L
                        {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1}, //L
                        {1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1}, //L
                        {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,0,0,0}, //F
                        {1,1,1,1,1,1,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0}, //F
                        {1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0}};//F 
        double t[][] = {{1,1},{1,1},{1,1},     // T
                        {1,-1},{1,-1},{1,-1},  // L
                        {-1,1},{-1,1},{-1,1}}; // F
        double alfa = 0.8;
        double teta = 0.3;
        double b[] = {0,0};
        
        perceptron P = new perceptron();
        P.set_input(x);
        P.set_target(t);
        P.set_alfa(alfa);
        P.set_teta(teta);
        P.set_bias(b);
        P.init_bobot();
        P.learn();
        
        double t1[] = {1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0}; // T
        double t2[] = {1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,1,0}; // T
        double t3[] = {1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1}; // L
        double t4[] = {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,1,0,0,0,0}; // F
        double t5[] = {1,1,1,1,1,1,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0}; // F
        double t6[] = {0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // F
        
        P.test(t1);
        P.test(t2);
        P.test(t3);
        P.test(t4);
        P.test(t5);
        P.test(t6);
    }
}

Saya buka diskusi jika ada pertanyaan, :D

Written by ElangSakti
Algoritma Perceptron untuk Pengenalan Huruf
Bahasan: Bagian script JST perceptron untuk pengenalan huruf Proses pembelajaran perceptron memang tergolong sederhana, lebih sederhana dari a...
Published at Minggu, 09 November 2014, Updated at Minggu, 09 November 2014
Reviewed by dr. on
Rating: 4.7

1 komentar :