Linear Vector Quantization neural network

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

description

The implementation of linear vector quantization neural network for clustering data using supervised learning principle.

Transcript of Linear Vector Quantization neural network

Page 1: Linear Vector Quantization neural network

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

1: /* LVQ for pattern clustering2: * Copyright: Denny Hermawanto - 20063: * Mail: [email protected]: *5: * Suppose we have data patterns (1;1;0;0),(0;0;0;1),(0;0;1;0),(1;1;0;0). 6: * We want to assign the patterns to two classes:7: *8: * Vector Class9: * (1;1;0;0) 0

10: * (0;0;0;1) 111: * (0;0;1;0) 012: * (1;1;0;0) 013: */14: 15: public class LVQ{16: 17: private void DefineInput(){18: inputvector = new double[numberofinput][inputdimension];19: 20: inputvector[0][0] = 1;21: inputvector[0][1] = 1;22: inputvector[0][2] = 0;23: inputvector[0][3] = 0;24: 25: inputvector[1][0] = 0;26: inputvector[1][1] = 0;27: inputvector[1][2] = 0;28: inputvector[1][3] = 1;29: 30: inputvector[2][0] = 0;31: inputvector[2][1] = 0;32: inputvector[2][2] = 1;33: inputvector[2][3] = 0;34: 35: inputvector[3][0] = 1;36: inputvector[3][1] = 1;37: inputvector[3][2] = 0;38: inputvector[3][3] = 0;39: }40: 41: private void DefineCluster(){42: targetcluster = new int[numberofinput];43: targetcluster[0] = 0;44: targetcluster[1] = 1;45: targetcluster[2] = 0;46: targetcluster[3] = 0;47: }48: 49: private double RandomNumberGenerator(){50: java.util.Random rnd = new java.util.Random();51: return rnd.nextDouble();52: }53: 54: private double LearningRateDecay(double currentlearningrate){55: double result = 0;56: result = 0.8 * currentlearningrate;57: return result;58: }59: 60: private void InitializeWeigths(){61: weights = new double[numberofcluster][inputdimension];62: for(int i=0;i<numberofcluster;i++){63: for(int j=0;j<inputdimension;j++){64: weights[i][j] = RandomNumberGenerator();65: }66: }

Print Date: 3/15/2011. Time: 9:00:37 PM 1/3

Page 2: Linear Vector Quantization neural network

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

67: }68: 69: private double ComputeEuclideanDistance(double[] vector1, double[]

vector2){70: double result;71: double distance =0;72: for(int j=0;j<inputdimension;j++){73: distance += Math.pow((vector1[j] - vector2[j]), 2);74: }75: result = distance;76: return result;77: }78: 79: private void TrainLVQ(int maxiteration){80: euclideandistance = new double[numberofcluster];81: System.out.print("Training LVQ");82: for(int iter=0;iter<maxiteration;iter++){83: for(int k=0;k<numberofinput;k++){84: //Get the winning neuron85: winningneuron = 0;86: for(int i=0;i<numberofcluster;i++){87: euclideandistance[i] =

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

if(euclideandistance[i]<euclideandistance[winningneuron]){90: winningneuron = i;91: }92: }93: //System.out.println(euclideandistance[i]);94: }95: if(targetcluster[k] == winningneuron){96: for(int i=0;i<inputdimension;i++){97: weights[winningneuron][i] += learnrate *

(inputvector[k][i] - weights[winningneuron][i]);98: }99: }

100: else{101: for(int i=0;i<inputdimension;i++){102: weights[winningneuron][i] =

weights[winningneuron][i] - (learnrate * (inputvector[k][i] - weights[winningneuron][i]));

103: }104: }105: 106: //System.out.println("Winner:"+winningneuron);107: //Update the winning neuron108: }109: learnrate = LearningRateDecay(learnrate);110: System.out.print("."); 111: //System.out.println("Learn Rate:"+learnrate);112: }113: }114: 115: private void MappingInputVector(){116: System.out.println("\n \n"+"Mapping Input Vectors:");117: for(int k=0;k<numberofinput;k++){118: winningneuron = 0;119: for(int i=0;i<numberofcluster;i++){120: euclideandistance[i] = ComputeEuclideanDistance(weights[i],

inputvector[k]);121: if(i!=0){122:

if(euclideandistance[i]<euclideandistance[winningneuron]){123: winningneuron = i;124: }

Print Date: 3/15/2011. Time: 9:00:37 PM 2/3

Page 3: Linear Vector Quantization neural network

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

125: }126: //System.out.println(euclideandistance[i]);127: }128: System.out.println("Input["+k+"] -> Cluster

No:"+winningneuron);129: }130: }131: 132: public void RunLVQ(){133: DefineParameters();134: DefineInput();135: DefineCluster();136: InitializeWeigths();137: TrainLVQ(50);138: MappingInputVector();139: }140: 141: public void DefineParameters(){142: numberofcluster = 2;143: inputdimension = 4;144: numberofinput = 4;145: learnrate = 0.6;146: }147: 148: public static void main(String[] args){149: LVQ lvq = new LVQ();150: lvq.RunLVQ();151: }152: 153: //define variables154: private double[][] inputvector;155: private double[][] weights;156: private double[] euclideandistance;157: private int[] targetcluster;158: private int numberofcluster;159: private int inputdimension;160: private int numberofinput;161: private double learnrate;162: private int winningneuron;163: }

Print Date: 3/15/2011. Time: 9:00:37 PM 3/3