Skip to content

Instantly share code, notes, and snippets.

@masasdani
Created January 18, 2013 04:21
Show Gist options
  • Save masasdani/4562308 to your computer and use it in GitHub Desktop.
Save masasdani/4562308 to your computer and use it in GitHub Desktop.
artifisial neural network, menggunakan sebuah layer dan bias.
package com.masasdani.ann;
import java.util.Random;
public class ANN {
private double alpha; //nilai yg ditentukan di awal, untuk menentukan delta w2
private int hiddenLayer; // digunakan untuk menentukan banyak node hidden di layer 1
private double w1[][]; // bobot dari input ke hidden layer
private double wbias[]; // bias dari input ke hiden layer
private double wbias2; // bias dari hiddenlayer ke output Y
private double z[];// nilai hidden layer
private double w2[]; // bobot dari hidden layer ke output Y
private double y; //output Y
private boolean generateRandomWeight = false;
public ANN(double alpha, int hiddenLayerCount) {
this.alpha= alpha;
this.hiddenLayer = hiddenLayerCount;
}
public ANN(double w1[][] , double w2[], double wbias[], double wbias2, int hiddenLayerCount, double alpha){
this.w1 = w1;
this.w2 = w2;
this.alpha= alpha;
this.hiddenLayer = hiddenLayerCount;
this.wbias = wbias;
this.wbias2 = wbias2;
generateRandomWeight = true;
}
public void training(double data[], int epoch, double errormin, double target){
//inisialisasi
w1 = new double[data.length][hiddenLayer];
wbias = new double[hiddenLayer];
z = new double[hiddenLayer];
w2 = new double[hiddenLayer];
double sigmaWX[] = new double[hiddenLayer];
double sigmaWsignoidZ =0d;
y=0d;
//generate random bobot
if(generateRandomWeight) generateRandomWeight(data);
//lakukan perulangan sebanyak epoch
for(int x=0;x<epoch;x++){
System.out.println("perulangan ke = "+x);
for(int i=0;i<hiddenLayer;i++){
for(int j=0;j<data.length;j++){
sigmaWX[i] += w1[j][i];
}
z[i] = wbias[i]+sigmaWX[i];
}
for(int i=0;i<hiddenLayer;i++){
sigmaWsignoidZ += sigmoid(z[i]);
}
y = wbias2 + sigmaWsignoidZ;
double sigmoidY = sigmoid(y);
double errorY = (target-sigmoidY)*sigmoidY*(1-sigmoidY);
System.out.println(errorY+"");
/////// fungsi berhenti ketika error y < error minimal
if(errorY < errormin)
return;
/////// jika tidak, lanjut ke dalam
double deltaW1[][] = new double[data.length][hiddenLayer];
double deltaW2[] = new double[hiddenLayer];
double errornet[] = new double[hiddenLayer];
double error[] = new double[hiddenLayer];
for(int i=0;i<hiddenLayer;i++){
deltaW2[i]=alpha*errorY*sigmoid(z[i]);
errornet[i] = sigmoidY*deltaW2[i];
error[i] = errornet[i]*sigmoid(z[i])*(1-sigmoid(z[i]));
}
for(int i=0;i<data.length;i++){
for(int j=0;j<hiddenLayer;j++){
deltaW1[i][j] = alpha*error[j]*data[i];
}
}
for(int i=0;i<data.length;i++){
for(int j=0;j<hiddenLayer;j++){
w1[i][j] = w1[i][j]+deltaW1[i][j];
}
}
for(int i=0;i<hiddenLayer;i++){
w2[i] = w2[i] + deltaW2[i];
}
}
}
public double[][] getW1(){
return w1;
}
public double[] getW2(){
return w2;
}
// fungsi aktifasi (sigmoid)
private double sigmoid(double y){
return 1 /(1+(Math.pow(Math.E, -y)));
}
private void generateRandomWeight(double data[]){
Random random = new Random();
//inisialisasi bias 1 0.5
for(int i=0;i<wbias.length;i++) wbias[i]=0.5;
//inisialisasi bias 2 0.1
wbias2 = 0.1;
//inisialisasi bobot w1 secara random
for(int i=0;i<data.length;i++){
System.out.print("bias "+i+" ");
for(int j=0;j<hiddenLayer;j++){
w1[i][j] = random.nextDouble();
System.out.print(w1[i][j]+" ");
}
System.out.println("");
}
//inisialisasi bobot w2 secara random
for(int i=0;i<hiddenLayer;i++){
w2[i] = random.nextDouble();
System.out.println("bias "+i+" "+w2[i]);
}
}
}
@masasdani
Copy link
Author

test training menentukan bobot

public static void main(String args[]){
double[] data = {0.8, 0.7};
ANN ann = new ANN(0.3, 2);
ann.training(data, 1000, 0.0005, 3.25);
double w1[][] = ann.getW1();
double w2[] = ann.getW2();

 for(int i=0;i<w1.length;i++){
     for(int j=0;j< w1[i].length;j++){
         System.out.print(w1[i][j]+" ");
     }
     System.out.println();
 }
 for(int i=0; i< w2.length;i++){
     System.out.println(w2[i]);
 }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment