# Hack Your Skills! to be Professional Mechatronics
Berikut hasil kompilasi dan eksekusi dari script di atas :
Java : Contoh Algoritma JST Backpropagation
Good day all, kali ini saya menulis ulang algoritma jaringan saraf tiruan (jst) dengan pembelajaran backpropagation. Script ini saya buat berdasarkan buku hijaunya Sri Kusumadewi, jadi jika ingin memahaminya lebih dalam, bisa sambil membaca buku tersebut, judulnya "Artificial Intelligence (teknik dan aplikasinya)".
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package backpro;
import java.text.DecimalFormat;
/**
* hari at elangsakti.com
* @author x86
*/
public class BP {
// kondisi
double x[][];
// target
double t[];
int unit_input;
int unit_hidden;
int unit_output;
// learning rate
double alfa;
// maximum loop
int maxloop;
// target error
double ERR;
// last error
double ERX;
// bobot input - hidden
double v[][];
// bobot bias - hidden
double v0[];
// bobot hidden - ouput
double w[][];
// bobot bias - output
double w0[];
void init_static(){
double init_x[][] = {{0,1},{1,0},{1,1},{0,0}};
double init_t[] = {1,1,0,0};
int init_uinput = init_x[0].length;
int init_uhidden = 4;
int init_uoutput = 1;
double init_alfa = 1;
int init_maxloop = 1000;
double init_ERR = 0.01;
double init_v[][] = {{0.9562,0.7762,0.1623,0.2886},{0.1962,0.6133,0.0311,0.9711}};
double init_v0[] = {0.7496,0.3796,0.7256,0.1628};
double init_w[][] = {{0.2280,0.9585,0.6799,0.0550}};
double init_w0[] = {0.9505};
this.x = init_x;
this.t = init_t;
this.unit_input = init_uinput;
this.unit_hidden = init_uhidden;
this.unit_output = init_uoutput;
this.alfa = init_alfa;
this.maxloop = init_maxloop;
this.ERR = init_ERR;
this.v = init_v;
this.v0 = init_v0;
this.w = init_w;
this.w0 = init_w0;
}
void learn_static(){
double data[][] = this.x;
double target[] = this.t;
int jumlah_data = data.length;
int jumlah_input = this.unit_input;
int jumlah_hidden = this.unit_hidden;
int jumlah_output = this.unit_output;
// do it for learn
int loop = 0;
this.maxloop = 1000;
do{
// for all data
for(int h=0; h<jumlah_data; h++){
// hitung z_in dan z
double z[] = new double[data.length];
for(int j=0; j<jumlah_hidden; j++){
//itung sigma xi vij
double z_in[] = new double[z.length];
double jum_xv=0;
for(int i=0; i<jumlah_input; i++){
double tmp=x[h][i]*v[i][j];
jum_xv=jum_xv+tmp;
}
z_in[j] = v0[j]+jum_xv;
z[j] = 1/(1+(double)Math.exp(-z_in[j]));
}
//~ itung y_in dan y (output)
double y[] = new double[jumlah_output];
for(int k=0; k<jumlah_output; k++){
double y_in[] = new double[y.length];
double jum_zw=0;
for(int j=0; j<jumlah_hidden; j++){
double tmp=z[j]*w[k][j];
jum_zw=jum_zw+tmp;
}
y_in[k]=w0[k]+jum_zw;
y[k]=1/(1+(double)Math.exp(-y_in[k]));
}
//ngitung error output dan delta bias dan delta bobot
double Err_y[] = new double[jumlah_output];
double Aw[][] = new double[this.w.length][this.w[0].length];
double Aw0[] = new double[this.w0.length];
for(int k=0; k<jumlah_output; k++){
//error otput
Err_y[k]=(t[h]-y[k])*y[k]*(1-y[k]);
for(int j=0; j<jumlah_hidden; j++){
//delta bobot hO
Aw[k][j] = alfa*Err_y[k]*z[j];
//delta bias hO
Aw0[k] = alfa*Err_y[k];
}
}
//ngitung error hiden dan delta bias dan delta bobot
double Err_in[] = new double[jumlah_hidden];
double Err_z[] = new double[jumlah_hidden];
double Av[][] = new double[this.v.length][this.v[0].length];
double Av0[] = new double[this.v0.length];
for(int j=0; j<jumlah_hidden; j++){
double tmp=0;
for(int k=0; k<jumlah_output; k++){
tmp = tmp + (Err_y[k]*this.w[k][j]);
}
// eror sebelum output / setelah hidden
Err_in[j]=tmp;
// eror hidden (t[h]-y[k])*y[k]*(1-y[k]);
Err_z[j]=Err_in[j]*(z[j])*(1-z[j]);
for(int i=0; i<jumlah_input; i++){
//delta bobot iH
Av[i][j]=this.alfa*Err_z[j]*this.x[h][i];
}
//delta bias hidden
Av0[j]=this.alfa*Err_z[j];
}
//update bobot dan bias
//update bobot bias outpuut
for(int j=0; j<jumlah_hidden; j++){
for(int k=0; k<jumlah_output; k++){
this.w[k][j]=this.w[k][j]+Aw[k][j];
}
}
for(int k=0; k<jumlah_output; k++){
this.w0[k]=this.w0[k]+Aw0[k];
}
//update bobot bias hidden
for(int i=0; i<jumlah_input; i++){
for(int j=0; j<jumlah_hidden; j++){
this.v[i][j]=this.v[i][j]+Av[i][j];
}
}
for(int j=0; j<jumlah_hidden; j++){
this.v0[j]=this.v0[j]+Av0[j];
}
}
loop++;
}while(is_stop()>this.ERR && loop<this.maxloop);
System.out.println("err : "+ERX);
System.out.println("loop : "+loop);
}
//penentuan berhenti atau lanjut
double is_stop(){
int jumlah_input = this.unit_input;
int jumlah_hidden = this.unit_hidden;
int jumlah_output = this.unit_output;
int jumlah_data = this.x.length;
double akumY=0;
//~ itung z_in dan z
for(int h=0; h<jumlah_data; h++){
double z[] = new double[jumlah_hidden];
for(int j=0; j<jumlah_hidden; j++){
//itung sigma xi vij
double z_in[] = new double[z.length];
double jum_xv=0;
for(int i=0; i<jumlah_input; i++){
double tmp=this.x[h][i]*this.v[i][j];
jum_xv=jum_xv+tmp;
}
z_in[j]=this.v0[j]+jum_xv;
z[j]=1/(1+(double)Math.exp(-z_in[j]));
}
//~ itung y_in dan y (output)
double y[] = new double[jumlah_output];
for(int k=0; k<jumlah_output; k++){
double y_in[] = new double[y.length];
double jum_zw=0;
for(int j=0; j<jumlah_hidden; j++){
double tmp=z[j]*this.w[k][j];
jum_zw=jum_zw+tmp;
}
y_in[k]=this.w0[k]+jum_zw;
y[k]=1/(1+(double)Math.exp(-y_in[k]));
akumY += Math.pow((t[h]-y[k]),2);
}
}
double E = akumY/this.x[0].length;
ERX = akumY;
return E;
}
void test(double data[]){
int jumlah_input = this.unit_input;
int jumlah_hidden = this.unit_hidden;
int jumlah_output = this.unit_output;
//pada hidden
double z[] = new double[jumlah_hidden];
for(int j=0; j<jumlah_hidden; j++){
double z_in[] = new double[z.length];
double tmp = 0;
for(int i=0; i<data.length; i++){
tmp = tmp + (data[i] * this.v[i][j]);
}
z_in[j] = this.v0[j] + tmp;
z[j] = 1/(1+(double)Math.exp(-z_in[j]));
}
//pada ouotpr
double y[] = new double[jumlah_output];
for(int k=0; k<jumlah_output; k++){
double y_in[] = new double[y.length];
double tmp = 0;
for(int j=0; j<jumlah_hidden; j++){
tmp = tmp + z[j] * this.w[k][j];
}
y_in[k] = this.w0[k] + tmp;
y[k] = 1/(1+(double)Math.exp(-y_in[k]));
if(y[k]>0.5)
y[k]=1;
else
y[k]=0;
System.out.println("Output "+(double)y[k]);
}
}
double double_format(double num, int len){
String format = "#.";
for(int a=0; a<len; a++){
format += "#";
}
DecimalFormat change = new DecimalFormat(format);
return Double.valueOf(change.format(num));
}
public static void main(String haripinter[]){
BP BP = new BP();
BP.init_static();
BP.learn_static();
double ax[] = {0,0};
double bx[] = {0,1};
double cx[] = {1,0};
double dx[] = {1,1};
BP.test(ax);
BP.test(bx);
BP.test(cx);
BP.test(dx);
}
}
Berikut hasil kompilasi dan eksekusi dari script di atas :
run: err : 0.01997614559923392 loop : 639 Output 0.0 Output 1.0 Output 1.0 Output 0.0 BUILD SUCCESSFUL (total time: 1 second)
Top Artikel :
Written by ElangSakti
Java : Contoh Algoritma JST Backpropagation
Bahasan: Good day all, kali ini saya menulis ulang algoritma jaringan saraf tiruan (jst) dengan pembelajaran backpropagation. Script ini saya buat ...
Published at Sabtu, 04 Januari 2014, Updated at Sabtu, 04 Januari 2014
Reviewed by dr. on
Rating: 4.7
Java : Contoh Algoritma JST Backpropagation
Bahasan: Good day all, kali ini saya menulis ulang algoritma jaringan saraf tiruan (jst) dengan pembelajaran backpropagation. Script ini saya buat ...
Published at Sabtu, 04 Januari 2014, Updated at Sabtu, 04 Januari 2014
Reviewed by dr. on
Rating: 4.7
Langganan:
Posting Komentar
(
Atom
)
keren banget anda mas brow.,, saya aja pusing bikin kayak gini..
BalasHapus