Rabu, 18 November 2009

Contoh Algoritma Hebb dengan Java

import java unit yang bersangkutan :
import java.awt.*;
import java.util.*;

=========================================================================
Class utama
public class Perceptron {
int dW[] = new int[2];
int Z[] = new int[3];
int db, y;
//int epoch = 0;
int lr = 1;
public Perceptron() {}

=========================================================================
Ini adalah bagian utama algoritma :
public void run(int a, int b, int c) {
y = a*dW[0] + b*dW[1] + db;
aktivasi (y);

if (y != c) {
dW[0] = dW[0] + lr * (c - y) * a;
dW[1] = dW[1] + lr * (c - y) * a;
} else {
dW[0] = dW[0];
dW[1] = dW[1];
}
}

=========================================================================
Fungsi untuk cek nilai output :
public void cek(int a, int b) {
Z[0] = dW[0] * a + dW[1] * b + db;
System.out.println("f("+Z[0]+") = "+ aktivasi(Z[0]));
}

=========================================================================
Fungsi Aktivasi :

public int aktivasi (int a) {
if (a>=0) {
return y=1;
} else {
return y=0;
}
}

=========================================================================
Fungsi Utama :
public static void main(String args[]) {
int X1[] ={0,0,1,1};
int X2[] = {0,1,0,1};
int Y[] = {0,1,1,1};

Perceptron o = new Perceptron();

for (int p=0;p<=3;p++) {
o.run(X1[p],X2[p],Y[p]);
}

for (int q=0;q<=3;q++) {
System.out.println("Hasil cek data ke-"+q);
o.cek(X1[q],X2[q]);
}
}
}

=========================================================================
Download all code disini

Tidak ada komentar:

Posting Komentar