Kohonen Neural Network

3
C:\Documents and Settings\Denny Hermawanto\Desktop\code\Kohonen.java 1: /* 2: * Copyright: Denny Hermawanto - 2006 3: * Mail: [email protected] 4: */ 5: 6: public class Kohonen { 7: 8: private void DefineInput ( ) { 9: inputvector = new double [ numberofinput ] [ inputdimension ] ; 10: 11: inputvector [ 0 ] [ 0 ] = 1 ; 12: inputvector [ 0 ] [ 1 ] = 1 ; 13: inputvector [ 0 ] [ 2 ] = 0 ; 14: inputvector [ 0 ] [ 3 ] = 0 ; 15: 16: inputvector [ 1 ] [ 0 ] = 0 ; 17: inputvector [ 1 ] [ 1 ] = 0 ; 18: inputvector [ 1 ] [ 2 ] = 0 ; 19: inputvector [ 1 ] [ 3 ] = 1 ; 20: 21: inputvector [ 2 ] [ 0 ] = 0 ; 22: inputvector [ 2 ] [ 1 ] = 0 ; 23: inputvector [ 2 ] [ 2 ] = 1 ; 24: inputvector [ 2 ] [ 3 ] = 0 ; 25: 26: inputvector [ 3 ] [ 0 ] = 1 ; 27: inputvector [ 3 ] [ 1 ] = 1 ; 28: inputvector [ 3 ] [ 2 ] = 0 ; 29: inputvector [ 3 ] [ 3 ] = 0 ; 30: } 31: 32: private double RandomNumberGenerator ( ) { 33: java . util . Random rnd = new java . util . Random ( ) ; 34: return rnd . nextDouble ( ) ; 35: } 36: 37: private double LearningRateDecay ( double currentlearningrate ) { 38: double result = 0 ; 39: result = 0.8 * currentlearningrate ; 40: return result ; 41: } 42: 43: private void InitializeWeigths ( ) { 44: weights = new double [ numberofcluster ] [ inputdimension ] ; 45: for ( int i = 0 ; i < numberofcluster ; i ++ ) { 46: for ( int j = 0 ; j < inputdimension ; j ++ ) { 47: weights [ i ] [ j ] = RandomNumberGenerator ( ) ; 48: } 49: } 50: } 51: 52: private double ComputeEuclideanDistance ( double [ ] vector1 , double [ ] vector2 ) { 53: double result ; 54: double distance = 0 ; 55: for ( int j = 0 ; j < inputdimension ; j ++ ) { 56: distance += Math . pow ( ( vector1 [ j ] - vector2 [ j ] ) , 2 ) ; 57: } 58: result = distance ; 59: return result ; 60: } 61: 62: private void TrainKohonen ( int maxiteration ) { 63: euclideandistance = new double [ numberofcluster ] ; 64: 65: for ( int iter = 0 ; iter < maxiteration ; iter ++ ) { Print Date: 3/15/2011. Time: 8:22:34 PM 1/3

description

This java source code shows the implementation of Kohonen neural networks for clustering binary data.

Transcript of Kohonen Neural Network

Page 1: Kohonen Neural Network

C:\Documents and Settings\Denny Hermawanto\Desktop\code\Kohonen.java

1: /*2: * Copyright: Denny Hermawanto - 20063: * Mail: [email protected]: */5: 6: public class Kohonen{7: 8: private void DefineInput(){9: inputvector = new double[numberofinput][inputdimension];

10: 11: inputvector[0][0] = 1;12: inputvector[0][1] = 1;13: inputvector[0][2] = 0;14: inputvector[0][3] = 0;15: 16: inputvector[1][0] = 0;17: inputvector[1][1] = 0;18: inputvector[1][2] = 0;19: inputvector[1][3] = 1;20: 21: inputvector[2][0] = 0;22: inputvector[2][1] = 0;23: inputvector[2][2] = 1;24: inputvector[2][3] = 0;25: 26: inputvector[3][0] = 1;27: inputvector[3][1] = 1;28: inputvector[3][2] = 0;29: inputvector[3][3] = 0;30: }31: 32: private double RandomNumberGenerator(){33: java.util.Random rnd = new java.util.Random();34: return rnd.nextDouble();35: }36: 37: private double LearningRateDecay(double currentlearningrate){38: double result = 0;39: result = 0.8 * currentlearningrate;40: return result;41: }42: 43: private void InitializeWeigths(){44: weights = new double[numberofcluster][inputdimension];45: for(int i=0;i<numberofcluster;i++){46: for(int j=0;j<inputdimension;j++){47: weights[i][j] = RandomNumberGenerator();48: }49: }50: }51: 52: private double ComputeEuclideanDistance(double[] vector1, double[]

vector2){53: double result;54: double distance =0;55: for(int j=0;j<inputdimension;j++){56: distance += Math.pow((vector1[j] - vector2[j]), 2);57: }58: result = distance;59: return result;60: }61: 62: private void TrainKohonen(int maxiteration){63: euclideandistance = new double[numberofcluster];64: 65: for(int iter=0;iter<maxiteration;iter++){

Print Date: 3/15/2011. Time: 8:22:34 PM 1/3

Page 2: Kohonen Neural Network

C:\Documents and Settings\Denny Hermawanto\Desktop\code\Kohonen.java

66: for(int k=0;k<numberofinput;k++){67: //Get the winning neuron68: winningneuron = 0;69: for(int i=0;i<numberofcluster;i++){70: euclideandistance[i] =

ComputeEuclideanDistance(weights[i],inputvector[k]);71: if(i!=0){72:

if(euclideandistance[i]<euclideandistance[winningneuron]){73: winningneuron = i;74: }75: }76: //System.out.println(euclideandistance[i]);77: }78: //System.out.println("Winner:"+winningneuron);79: //Update the winning neuron80: for(int i=0;i<inputdimension;i++){81: weights[winningneuron][i] += learnrate *

(inputvector[k][i] - weights[winningneuron][i]);82: }83: }84: learnrate = LearningRateDecay(learnrate); 85: System.out.println("Learn Rate:"+learnrate);86: }87: }88: 89: private void MappingInputVector(){90: for(int k=0;k<numberofinput;k++){91: winningneuron = 0;92: for(int i=0;i<numberofcluster;i++){93: euclideandistance[i] = ComputeEuclideanDistance(weights[i],

inputvector[k]);94: if(i!=0){95:

if(euclideandistance[i]<euclideandistance[winningneuron]){96: winningneuron = i;97: }98: }99: //System.out.println(euclideandistance[i]);

100: }101: System.out.println("Input["+k+"] -> Cluster

No:"+winningneuron);102: }103: }104: 105: public void RunKohonen(){106: DefineInput();107: InitializeWeigths();108: TrainKohonen(50);109: MappingInputVector();110: }111: 112: public static void main(String[] args){113: Kohonen kohonen = new Kohonen();114: kohonen.numberofcluster = 2;115: kohonen.inputdimension = 4;116: kohonen.numberofinput = 4;117: kohonen.learnrate = 0.6;118: kohonen.RunKohonen();119: }120: 121: //define variables122: private double[][] inputvector;123: private double[][] weights;124: private double[] euclideandistance;125: private int numberofcluster;

Print Date: 3/15/2011. Time: 8:22:34 PM 2/3

Page 3: Kohonen Neural Network

C:\Documents and Settings\Denny Hermawanto\Desktop\code\Kohonen.java

126: private int inputdimension;127: private int numberofinput;128: private double learnrate;129: private int winningneuron;130: }

Print Date: 3/15/2011. Time: 8:22:34 PM 3/3