Forum Stats

  • 3,838,748 Users
  • 2,262,397 Discussions
  • 7,900,749 Comments

Discussions

Encog Neural Network Issue

55348051-122a-4f6d-b440-49c034776d58
edited May 4, 2018 11:45AM in Java Programming

I am building neural network using encog frame work. So if anyone working with this I hope he/she can help me. I am in the final step of the neural network. I will 1st present my question and then I will post my entire code for the consideration.

In the evaluation step

int classCount = analyst.getScript().getNormalize().getNormalizedFields().get(5).getClasses().size();

double norHigh = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedHigh();

double norLow = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedLow();

Equilateral eq = new Equilateral(classCount,norHigh,norLow);

int predictedClassInt = eq.decode(output.getData());

I have created an Equilateral object use it to get the index of output class. This gives me an arrayOutOfBound error . But the same thing works good in C. here is the C code.

<span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">var eq </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">=</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="kwd" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #101094;">new</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> </span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">Encog</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">MathUtil</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">Equilateral</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">(</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">classCount</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">,</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">normalizationhigh</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">,</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">normalizationLow</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">);</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"><br/>var predictedClassInt </span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">=</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;"> eq</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">.</span><span class="typ" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #2b91af;">Decode</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">(</span><span class="pln" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">output</span><span class="pun" style="font-style: inherit; font-weight: inherit; font-family: inherit; color: #303336;">);</span>

The below codes are just for the consideration if anyone interested in looking what I have done previously..

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now I will post entire evaluation part of the code up to above error.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

BasicNetwork network2 = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File("networkFile.eg"));

EncogAnalyst analyst2 = new EncogAnalyst();

analyst2.load(new File("Stats.ega"));

MLDataSet Evaluate = EncogUtility.loadCSV2Memory(normalizedTest.toString(),network.getInputCount(),network.getOutputCount(),true,CSVFormat.ENGLISH,false);

int count = 0;

int correctCount = 0;

for(MLDataPair pair: Evaluate){

  count++;

  final MLData output = network2.compute(pair.getInput());

  double cir = analyst2.getScript().getNormalize().getNormalizedFields().get(0).deNormalize(pair.getInput().getData(0));

  double cir2 = analyst2.getScript().getNormalize().getNormalizedFields().get(1).deNormalize(pair.getInput().getData(1));

  double Ecc = analyst2.getScript().getNormalize().getNormalizedFields().get(2).deNormalize(pair.getInput().getData(2));

  double Entro = analyst2.getScript().getNormalize().getNormalizedFields().get(3).deNormalize(pair.getInput().getData(3));

  double SumEntro = analyst2.getScript().getNormalize().getNormalizedFields().get(4).deNormalize(pair.getInput().getData(4));

   int classCount = analyst.getScript().getNormalize().getNormalizedFields().get(5).getClasses().size();

   double norHigh = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedHigh();

   double norLow = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedLow();

  Equilateral eq = new Equilateral(classCount,norHigh,norLow);

   int predictedClassInt = eq.decode(output.getData());

  String predictedClass = analyst2.getScript().getNormalize().getNormalizedFields().get(5).getClasses().get(predictedClassInt).getName();

   int IdealClassInt = eq.decode(pair.getIdealArray());

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This is my complete neural network. Just for the consideration. If anyone can comment.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public void  ANN() {

  File cancerData = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\training.CSV");

  File shuffledData = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\shuffled.CSV");

  File TestData = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\Evaluate.CSV");

  ShuffleCSV shuffle = new ShuffleCSV();

  shuffle.analyze(cancerData,true,CSVFormat.ENGLISH);

  shuffle.setProduceOutputHeaders(true);

  shuffle.process(shuffledData);

  File shuf75 = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\shuffled75.CSV");

  File shuf25 = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\shuffled25.CSV");


   File normalized = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\normalized.CSV");

  File normalizedTest = new File("C:\\Users\\Denuwan.Denuwan-PC\\Documents\\GitHub\\AnnTest1\\src\\main\\java\\NormalizedEval.CSV");

  EncogAnalyst analyst = new EncogAnalyst();

  AnalystWizard wizarddd = new AnalystWizard(analyst);

  wizarddd.wizard(shuffledData,true,AnalystFileFormat.DECPNT_COMMA);

   final AnalystNormalizeCSV norm = new AnalystNormalizeCSV();

  norm.analyze(shuffledData,true,CSVFormat.ENGLISH,analyst);

  norm.setProduceOutputHeaders(true);

  norm.normalize(normalized);

  norm.analyze(TestData,true,CSVFormat.ENGLISH,analyst);

  norm.normalize(normalizedTest);

  analyst.save(new File("Stats.ega"));

  BasicNetwork annNetwork = new BasicNetwork();

  annNetwork.addLayer(new BasicLayer(null,true,5));

  annNetwork.addLayer(new BasicLayer(new ActivationTANH(),true,5));

  annNetwork.addLayer(new BasicLayer(new ActivationSigmoid(),true,10));

  annNetwork.addLayer(new BasicLayer(new ActivationSigmoid(),true,10));

  annNetwork.addLayer(new BasicLayer(new ActivationTANH(),true,10));

  annNetwork.addLayer(new BasicLayer(new ActivationLinear(),true,2));

  annNetwork.addLayer(new BasicLayer(null,false,2));

  annNetwork.getStructure().finalizeStructure();

  annNetwork.reset();

  EncogDirectoryPersistence.saveObject(new File("networkFile.eg"),(BasicNetwork)annNetwork);

  BasicNetwork network = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File("networkFile.eg"));

  MLDataSet trainingSet = EncogUtility.loadCSV2Memory(normalized.toString(),network.getInputCount(),network.getOutputCount(),true,CSVFormat.ENGLISH,false);

   // VersatileDataSource source = new CSVDataSource(normalized, false,CSVFormat.DECIMAL_POINT);
  //VersatileMLDataSet trainingSet = new VersatileMLDataSet(source);

   Backpropagation train = new Backpropagation(annNetwork,trainingSet,0.7,0.3);

   int epoch = 1;

   do{

  train.iteration();

  System.out.println("Epoch #"+epoch+"Error"+train.getError());

  epoch++;

  }while (train.getError()>0.001);

  EncogDirectoryPersistence.saveObject(new File("networkFile.eg"),(BasicNetwork)annNetwork);

  BasicNetwork network2 = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File("networkFile.eg"));

  EncogAnalyst analyst2 = new EncogAnalyst();

  analyst2.load(new File("Stats.ega"));

  MLDataSet Evaluate = EncogUtility.loadCSV2Memory(normalizedTest.toString(),network.getInputCount(),network.getOutputCount(),true,CSVFormat.ENGLISH,false);

   int count = 0;

   int correctCount = 0;

   for(MLDataPair pair: Evaluate){

  count++;

   final MLData output = network2.compute(pair.getInput());

   double cir = analyst2.getScript().getNormalize().getNormalizedFields().get(0).deNormalize(pair.getInput().getData(0));

   double cir2 = analyst2.getScript().getNormalize().getNormalizedFields().get(1).deNormalize(pair.getInput().getData(1));

   double Ecc = analyst2.getScript().getNormalize().getNormalizedFields().get(2).deNormalize(pair.getInput().getData(2));

   double Entro = analyst2.getScript().getNormalize().getNormalizedFields().get(3).deNormalize(pair.getInput().getData(3));

   double SumEntro = analyst2.getScript().getNormalize().getNormalizedFields().get(4).deNormalize(pair.getInput().getData(4));

   int classCount = analyst.getScript().getNormalize().getNormalizedFields().get(5).getClasses().size();

   double norHigh = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedHigh();

   double norLow = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedLow();

  Equilateral eq = new Equilateral(classCount,norHigh,norLow);

   int predictedClassInt = eq.decode(output.getData());

  String predictedClass = analyst2.getScript().getNormalize().getNormalizedFields().get(5).getClasses().get(predictedClassInt).getName();

   int IdealClassInt = eq.decode(pair.getIdealArray());

}

}

rpc1

Answers

  • Unknown
    edited May 4, 2018 11:45AM
    I have created an Equilateral object use it to get the index of output class. This gives me an arrayOutOfBound error . 

    You should be able to EASILY discover the problem by just examining those variable and index values in a GUI debugger like NetBeans as you step through the code.

    int classCount = analyst.getScript().getNormalize().getNormalizedFields().get(5).getClasses().size();double norHigh = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedHigh();double norLow = analyst.getScript().getNormalize().getNormalizedFields().get(5).getNormalizedLow();Equilateral eq = new Equilateral(classCount,norHigh,norLow);

    1. put a breakpoint on the last line of code above

    2. examine the values of 'classCount', 'norHigh' and 'norLow' and confirm they all have VALID values.

    The BIGGEST mistakes I see in your code are:

    1. no logging at all - logging is ESSENTIAL in order to be able to easily review later what the app did and what the results were of key pieces of code. I don't consider your one uses of 'System.out...' to be logging.

    2. no exception handling at all - that allows ALL of your code to just blindly keep executing even if a problem occurs that should be trapped and handled.

    If you are NOT using a GUI debugger then that is the FIRST thing you should rectify. A debugger is ESSENTIAL except for the simplest of applications.

    rpc1
This discussion has been closed.