-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample3a.java
654 lines (502 loc) · 22.9 KB
/
Sample3a.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// ============================================================================
// Approximation of the periodic function outside of the training range.
//
// The input is the file consisting of records with two fields:
// - The first field is the xPoint value.
// - The second field is the target function value at that xPoint
// ============================================================================
package sample3a;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.util.Properties;
import java.time.YearMonth;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import org.encog.Encog;
import org.encog.engine.network.activation.ActivationTANH;
import org.encog.engine.network.activation.ActivationReLU;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.buffer.MemoryDataLoader;
import org.encog.ml.data.buffer.codec.CSVDataCODEC;
import org.encog.ml.data.buffer.codec.DataSetCODEC;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
import org.encog.persist.EncogDirectoryPersistence;
import org.encog.util.csv.CSVFormat;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.demo.charts.ExampleChart;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.colors.ChartColor;
import org.knowm.xchart.style.colors.XChartSeriesColors;
import org.knowm.xchart.style.lines.SeriesLines;
import org.knowm.xchart.style.markers.SeriesMarkers;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.BitmapEncoder.BitmapFormat;
import org.knowm.xchart.QuickChart;
import org.knowm.xchart.SwingWrapper;
public class Sample3a implements ExampleChart<XYChart>
{
static double Nh = 1;
static double Nl = -1;
// First column
static double maxXPointDh = 5.00;
static double minXPointDl = -1.00;
// Second column - target data
static double maxTargetValueDh = 13.00;
static double minTargetValueDl = 9.00;
static double doublePointNumber = 0.00;
static int intPointNumber = 0;
static InputStream input = null;
static double[] arrFunctionValue = new double[500];
static double inputDiffValue = 0.00;
static double predictDiffValue = 0.00;
static double targetDiffValue = 0.00;
static double valueDifferencePerc = 0.00;
static String strFunctionValuesFileName;
static int returnCode = 0;
static int numberOfInputNeurons;
static int numberOfOutputNeurons;
static int numberOfRecordsInFile;
static int intNumberOfRecordsInTestFile;
static double realTargetValue ;
static double realPredictValue ;
static String functionValuesTrainFileName;
static String functionValuesTestFileName;
static String trainFileName;
static String priceFileName;
static String testFileName;
static String chartTrainFileName;
static String chartTestFileName;
static String networkFileName;
static int workingMode;
static String cvsSplitBy = ",";
static double denormTargetDiffPerc;
static double denormPredictDiffPerc;
static List<Double> xData = new ArrayList<Double>();
static List<Double> yData1 = new ArrayList<Double>();
static List<Double> yData2 = new ArrayList<Double>();
static XYChart Chart;
@Override
public XYChart getChart()
{
// Create Chart
Chart = new XYChartBuilder().width(900).height(500).title(getClass().
getSimpleName()).xAxisTitle("x").yAxisTitle("y= f(x)").build();
// Customize Chart
Chart.getStyler().setPlotBackgroundColor(ChartColor.getAWTColor(ChartColor.GREY));
Chart.getStyler().setPlotGridLinesColor(new Color(255, 255, 255));
Chart.getStyler().setChartBackgroundColor(Color.WHITE);
Chart.getStyler().setLegendBackgroundColor(Color.PINK);
Chart.getStyler().setChartFontColor(Color.MAGENTA);
Chart.getStyler().setChartTitleBoxBackgroundColor(new Color(0, 222, 0));
Chart.getStyler().setChartTitleBoxVisible(true);
Chart.getStyler().setChartTitleBoxBorderColor(Color.BLACK);
Chart.getStyler().setPlotGridLinesVisible(true);
Chart.getStyler().setAxisTickPadding(20);
Chart.getStyler().setAxisTickMarkLength(15);
Chart.getStyler().setPlotMargin(20);
Chart.getStyler().setChartTitleVisible(false);
Chart.getStyler().setChartTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24));
Chart.getStyler().setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18));
Chart.getStyler().setLegendPosition(LegendPosition.InsideSE);
Chart.getStyler().setLegendSeriesLineLength(12);
Chart.getStyler().setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18));
Chart.getStyler().setAxisTickLabelsFont(new Font(Font.SERIF, Font.PLAIN, 11));
Chart.getStyler().setDatePattern("yyyy-MM");
Chart.getStyler().setDecimalPattern("#0.00");
// Configuration
// Train
//workingMode = 1;
//trainFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_Norm_Tan_Train.csv";
//functionValuesTrainFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_Tan_Calculate_Train.csv";
//chartTrainFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_XYLine_Tan_Train_Chart";
//numberOfRecordsInFile = 12;
// Test
workingMode = 2;
testFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_Norm_Tan_Test.csv";
functionValuesTestFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_Tan_Calculate_Test.csv";
chartTestFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_XYLine_Tan_Test_Chart";
numberOfRecordsInFile = 12;
// Common configuration
networkFileName = "C:/My_Neural_Network_Book/Book_Examples/Sample3a_Saved_Tan_Network_File.csv";
numberOfInputNeurons = 1;
numberOfOutputNeurons = 1;
try
{
// Check the working mode to run
if(workingMode == 1)
{
// Train mode
loadFunctionValueTrainFileInMemory();
File file1 = new File(chartTrainFileName);
File file2 = new File(networkFileName);
if(file1.exists())
file1.delete();
if(file2.exists())
file2.delete();
returnCode = 0; // Clear the return code variable
do
{
returnCode = trainValidateSaveNetwork();
} while (returnCode > 0);
} // End the train logic
else
{
// Testing mode.
// Load testing file in memory
loadTestFileInMemory();
File file1 = new File(chartTestFileName);
if(file1.exists())
file1.delete();
loadAndTestNetwork();
}
}
catch (Throwable t)
{
t.printStackTrace();
System.exit(1);
}
finally
{
Encog.getInstance().shutdown();
}
Encog.getInstance().shutdown();
return Chart;
} // End of the method
// =======================================================
// Load CSV to memory.
// @return The loaded dataset.
// =======================================================
public static MLDataSet loadCSV2Memory(String filename, int input, int ideal, boolean headers, CSVFormat format, boolean significance)
{
DataSetCODEC codec = new CSVDataCODEC(new File(filename), format, headers, input, ideal, significance);
MemoryDataLoader load = new MemoryDataLoader(codec);
MLDataSet dataset = load.external2Memory();
return dataset;
}
// =======================================================
// The main method.
// @param Command line arguments. No arguments are used.
// ======================================================
public static void main(String[] args)
{
ExampleChart<XYChart> exampleChart = new Sample3a();
XYChart Chart = exampleChart.getChart();
new SwingWrapper<XYChart>(Chart).displayChart();
} // End of the main method
//==========================================================================
// Train, validate, and save the trained network file
//==========================================================================
static public int trainValidateSaveNetwork()
{
double functionValue = 0.00;
// Load the training CSV file in memory
MLDataSet trainingSet =
loadCSV2Memory(trainFileName,numberOfInputNeurons,numberOfOutputNeurons,
true,CSVFormat.ENGLISH,false);
// create a neural network
BasicNetwork network = new BasicNetwork();
// Input layer
network.addLayer(new BasicLayer(null,true,1));
// Hidden layer
network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
//network.addLayer(new BasicLayer(new ActivationTANH(),true,5));
// Output layer
network.addLayer(new BasicLayer(new ActivationTANH(),false,1));
network.getStructure().finalizeStructure();
network.reset();
// train the neural network
final ResilientPropagation train = new ResilientPropagation(network, trainingSet);
int epoch = 1;
returnCode = 0;
do
{
train.iteration();
System.out.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
if (epoch >= 500 && network.calculateError(trainingSet) > 0.000000061) // 0.00049591 0.00008
{
returnCode = 1;
System.out.println("Try again");
return returnCode;
}
} while(train.getError() > 0.00000006); // 0.0004959 0.00008
// Save the network file
EncogDirectoryPersistence.saveObject(new File(networkFileName),network);
System.out.println("Neural Network Results:");
double sumDifferencePerc = 0.00;
double averNormDifferencePerc = 0.00;
double maxErrorPerc = 0.00;
int m = -1; // Record number in the input file
double xPoint_Initial = 0.00;
double xPoint_Increment = 0.12;
double xPoint = xPoint_Initial - xPoint_Increment;
realTargetValue = 0.00;
realPredictValue = 0.00;
//System.out.println ("xPoint = " + xPoint_Initial + " realTargetValue = " + realTargetValue + " realPredictValue = " + realPredictValue + " valueDifferencePerc = " + valueDifferencePerc);
for(MLDataPair pair: trainingSet)
{
m++;
xPoint = xPoint + xPoint_Increment;
//if(xPoint > 3.14)
// break;
final MLData output = network.compute(pair.getInput());
MLData inputData = pair.getInput();
MLData actualData = pair.getIdeal();
MLData predictData = network.compute(inputData);
// Calculate and print the results
inputDiffValue = inputData.getData(0);
targetDiffValue = actualData.getData(0);
predictDiffValue = predictData.getData(0);
// denormalize the values
denormTargetDiffPerc = ((minTargetValueDl - maxTargetValueDh)*targetDiffValue - Nh*minTargetValueDl + maxTargetValueDh*Nl)/(Nl - Nh);
denormPredictDiffPerc =((minTargetValueDl - maxTargetValueDh)*predictDiffValue - Nh*minTargetValueDl + maxTargetValueDh*Nl)/(Nl - Nh);
//functionValue = arrFunctionValue[m];
//realTargetValue = functionValue + denormTargetDiffPerc;
//realPredictValue = functionValue + denormPredictDiffPerc;
valueDifferencePerc =
Math.abs(((denormTargetDiffPerc - denormPredictDiffPerc)/denormTargetDiffPerc)*100.00);
System.out.println ("xPoint = " + xPoint + " realTargetValue = " +
denormTargetDiffPerc + " realPredictValue = " + denormPredictDiffPerc + " valueDifferencePerc = " + valueDifferencePerc);
sumDifferencePerc = sumDifferencePerc + valueDifferencePerc;
if (valueDifferencePerc > maxErrorPerc && m > 0)
maxErrorPerc = valueDifferencePerc;
//realTargetValue = arrFunctionValue[m];
xData.add(xPoint);
yData1.add(denormTargetDiffPerc);
yData2.add(denormPredictDiffPerc);
} // End for pair loop
XYSeries series1 = Chart.addSeries("Actual data", xData, yData1);
XYSeries series2 = Chart.addSeries("Predict data", xData, yData2);
series1.setLineColor(XChartSeriesColors.BLUE);
series2.setMarkerColor(Color.ORANGE);
series1.setLineStyle(SeriesLines.SOLID);
series2.setLineStyle(SeriesLines.SOLID);
try
{
//Save the chart image
BitmapEncoder.saveBitmapWithDPI(Chart, chartTrainFileName, BitmapFormat.JPG, 100);
System.out.println ("Train Chart file has been saved") ;
}
catch (IOException ex)
{
ex.printStackTrace();
System.exit(3);
}
// Finally, save this trained network
EncogDirectoryPersistence.saveObject(new File(networkFileName),network);
System.out.println ("Train Network has been saved") ;
averNormDifferencePerc = sumDifferencePerc/numberOfRecordsInFile;
System.out.println(" ");
System.out.println("maxErrorPerc = " + maxErrorPerc +
" averNormDifferencePerc = " + averNormDifferencePerc);
returnCode = 0;
return returnCode;
} // End of the method
//==========================================================================
// This method load and test the trained network at the points not
// used for training.
//==========================================================================
static public void loadAndTestNetwork()
{
System.out.println("Testing the networks results");
List<Double> xData = new ArrayList<Double>();
List<Double> yData1 = new ArrayList<Double>();
List<Double> yData2 = new ArrayList<Double>();
double sumDifferencePerc = 0.00;
double maxErrorPerc = 0.00;
double maxGlobalResultDiff = 0.00;
double averErrorPerc = 0.00;
double sumGlobalResultDiff = 0.00;
double functionValue;
BufferedReader br4;
BasicNetwork network;
int k1 = 0;
// Process test records
maxGlobalResultDiff = 0.00;
averErrorPerc = 0.00;
sumGlobalResultDiff = 0.00;
// Load the test dataset into memory
//MLDataSet testingSet =
//loadCSV2Memory(testFileName,numberOfInputNeurons,numberOfOutputNeurons,true,
// CSVFormat.ENGLISH,false);
MLDataSet testingSet =
loadCSV2Memory(testFileName,numberOfInputNeurons,numberOfOutputNeurons,true,
CSVFormat.ENGLISH,false);
// Load the saved trained network
network =
(BasicNetwork)EncogDirectoryPersistence.loadObject(new File(networkFileName));
int i = - 1; // Index of the current record
int m = -1;
double xPoint_Initial = 3.141592654;
double xPoint_Increment = 0.12;
double xPoint = xPoint_Initial - xPoint_Increment;
realTargetValue = 0.00;
realPredictValue = 0.00;
for (MLDataPair pair: testingSet)
{
m++;
xPoint = xPoint + xPoint_Increment;
//if(xPoint > 3.14)
// break;
final MLData output = network.compute(pair.getInput());
MLData inputData = pair.getInput();
MLData actualData = pair.getIdeal();
MLData predictData = network.compute(inputData);
// Calculate and print the results
inputDiffValue = inputData.getData(0);
targetDiffValue = actualData.getData(0);
predictDiffValue = predictData.getData(0);
// denormalize the values
denormTargetDiffPerc = ((minTargetValueDl - maxTargetValueDh)*targetDiffValue - Nh*minTargetValueDl + maxTargetValueDh*Nl)/(Nl - Nh);
denormPredictDiffPerc =((minTargetValueDl - maxTargetValueDh)*predictDiffValue - Nh*minTargetValueDl + maxTargetValueDh*Nl)/(Nl - Nh);
//functionValue = arrFunctionValue[m];
//realTargetValue = functionValue + denormTargetDiffPerc;
//realPredictValue = functionValue + denormPredictDiffPerc;
valueDifferencePerc =
Math.abs(((denormTargetDiffPerc - denormPredictDiffPerc)/denormTargetDiffPerc)*100.00);
System.out.println ("xPoint = " + xPoint + " realTargetValue = " +
denormTargetDiffPerc + " realPredictValue = " + denormPredictDiffPerc + " valueDifferencePerc = " + valueDifferencePerc);
sumDifferencePerc = sumDifferencePerc + valueDifferencePerc;
if (valueDifferencePerc > maxErrorPerc && m > 0)
maxErrorPerc = valueDifferencePerc;
//realTargetValue = arrFunctionValue[m];
xData.add(xPoint);
yData1.add(denormTargetDiffPerc);
yData2.add(denormPredictDiffPerc);
} // End for pair loop
// Print max and average results
System.out.println(" ");
averErrorPerc = sumDifferencePerc/numberOfRecordsInFile;
System.out.println("maxErrorPerc = " + maxErrorPerc);
System.out.println("averErrorPerc = " + averErrorPerc);
// All testing batch files have been processed
XYSeries series1 = Chart.addSeries("Actual", xData, yData1);
XYSeries series2 = Chart.addSeries("Predicted", xData, yData2);
series1.setLineColor(XChartSeriesColors.BLUE);
series2.setMarkerColor(Color.ORANGE);
series1.setLineStyle(SeriesLines.SOLID);
series2.setLineStyle(SeriesLines.SOLID);
// Save the chart image
try
{
BitmapEncoder.saveBitmapWithDPI(Chart, chartTestFileName , BitmapFormat.JPG, 100);
}
catch (Exception bt)
{
bt.printStackTrace();
}
System.out.println ("The Chart has been saved");
} // End of the method
//==================================================================
// Load Training Function Values file in memory
//==================================================================
public static void loadFunctionValueTrainFileInMemory()
{
BufferedReader br1 = null;
String line = "";
String cvsSplitBy = ",";
double tempYFunctionValue = 0.00;
try
{
br1 = new BufferedReader(new FileReader(functionValuesTrainFileName));
int i = -1;
int r = -2;
while ((line = br1.readLine()) != null)
{
i++;
r++;
if(i == 201)
i = i;
// Skip the header line
if(i > 0)
{
// Brake the line using comma as separator
String[] workFields = line.split(cvsSplitBy);
tempYFunctionValue = Double.parseDouble(workFields[1]);
arrFunctionValue[r] = tempYFunctionValue;
}
} // end of the while loop
br1.close();
}
catch (IOException ex)
{
ex.printStackTrace();
System.err.println("Error opening files = " + ex);
System.exit(1);
}
}
//==================================================================
// Load testing Function Values file in memory
//==================================================================
public static void loadTestFileInMemory()
{
BufferedReader br1 = null;
String line = "";
String cvsSplitBy = ",";
double tempYFunctionValue = 0.00;
try
{
br1 = new BufferedReader(new FileReader(functionValuesTestFileName));
int i = -1; // Inputline number
int r = -2; // Inputline number
while ((line = br1.readLine()) != null)
{
i++;
r++;
if(i == 201)
i = i;
// Skip the header line
if(i > 0)
{
// Brake the line using comma as separator
String[] workFields = line.split(cvsSplitBy);
tempYFunctionValue = Double.parseDouble(workFields[1]);
arrFunctionValue[r] = tempYFunctionValue;
}
} // end of the while loop
br1.close();
}
catch (IOException ex)
{
ex.printStackTrace();
System.err.println("Error opening files = " + ex);
System.exit(1);
}
}
} // End of the class