This example trains a 2-layer network using three binary inputs (X0, X1, X2) and one three-level classification (Y). Where
Y = 0 if X1 = 1
Y = 1 if X2 = 1
Y = 2 if X3 = 1
using System; using Imsl.DataMining.Neural; using PrintMatrix = Imsl.Math.PrintMatrix; using PrintMatrixFormat = Imsl.Math.PrintMatrixFormat; //***************************************************************************** // Two-Layer FFN with 3 binary inputs (X0, X1, X2) and one three-level // classification variable (Y) // Y = 0 if X1 = 1 // Y = 1 if X2 = 1 // Y = 2 if X3 = 1 // (training_ex6) //***************************************************************************** [Serializable] public class MultiClassificationEx2 { private static int nObs = 6; // number of training patterns private static int nInputs = 3; // 3 inputs, all categorical private static int nOutputs = 3; // private static double[,] xData = {{1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, 1}}; private static int[] yData = new int[]{1, 1, 2, 2, 3, 3}; private static double[] weights = new double[]{1.29099444873580580000, -0.64549722436790280000, -0.64549722436790291000, 0.00000000000000000000, 1.11803398874989490000, -1.11803398874989470000, 0.57735026918962584000, 0.57735026918962584000, 0.57735026918962584000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, 0.33333333333333331000, -0.00000000000000005851, -0.00000000000000005851, -0.57735026918962573000, 0.00000000000000000000, 0.00000000000000000000, 0.00000000000000000000}; public static void Main(System.String[] args) { FeedForwardNetwork network = new FeedForwardNetwork(); network.InputLayer.CreateInputs(nInputs); network.CreateHiddenLayer().CreatePerceptrons(3, Imsl.DataMining.Neural.Activation.Linear, 0.0); network.OutputLayer.CreatePerceptrons(nOutputs, Imsl.DataMining.Neural.Activation.Softmax, 0.0); network.LinkAll(); network.Weights = weights; MultiClassification classification = new MultiClassification(network); QuasiNewtonTrainer trainer = new QuasiNewtonTrainer(); trainer.SetError(classification.Error); trainer.MaximumTrainingIterations = 1000; trainer.GradientTolerance = 1.0e-20; trainer.StepTolerance = 1.0e-20; // Train Network classification.Train(trainer, xData, yData); // Display Network Errors double[] stats = classification.ComputeStatistics(xData, yData); System.Console.Out.WriteLine( "***********************************************"); System.Console.Out.WriteLine( "--> Cross-Entropy Error: " + (float) stats[0]); System.Console.Out.WriteLine( "--> Classification Error: " + (float) stats[1]); System.Console.Out.WriteLine( "***********************************************"); System.Console.Out.WriteLine(); double[] weight = network.Weights; double[] gradient = trainer.ErrorGradient; double[][] wg = new double[weight.Length][]; for (int i = 0; i < weight.Length; i++) { wg[i] = new double[2]; } for (int i = 0; i < weight.Length; i++) { wg[i][0] = weight[i]; wg[i][1] = gradient[i]; } PrintMatrixFormat pmf = new PrintMatrixFormat(); pmf.SetColumnLabels(new System.String[]{"Weights", "Gradients"}); new PrintMatrix().Print(pmf, wg); double[][] report = new double[nObs][]; for (int i2 = 0; i2 < nObs; i2++) { report[i2] = new double[nInputs + nOutputs + 2]; } for (int i = 0; i < nObs; i++) { for (int j = 0; j < nInputs; j++) { report[i][j] = xData[i,j]; } report[i][nInputs] = yData[i]; double[] xTmp = new double[xData.GetLength(1)]; for (int j=0; j<xData.GetLength(1); j++) xTmp[j] = xData[i,j]; double[] p = classification.Probabilities(xTmp); for (int j = 0; j < nOutputs; j++) { report[i][nInputs + 1 + j] = p[j]; } report[i][nInputs + nOutputs + 1] = classification.PredictedClass(xTmp); } pmf = new PrintMatrixFormat(); pmf.SetColumnLabels(new System.String[]{"X1", "X2", "X3", "Y", "P(C1)", "P(C2)", "P(C3)", "Predicted"}); new PrintMatrix("Forecast").Print(pmf, report); System.Console.Out.WriteLine("Cross-Entropy Error Value = " + trainer.ErrorValue); // ********************************************************************** // DISPLAY CLASSIFICATION STATISTICS // ********************************************************************** double[] statsClass = classification.ComputeStatistics(xData, yData); // Display Network Errors System.Console.Out.WriteLine( "***********************************************"); System.Console.Out.WriteLine("--> Cross-Entropy Error: " + (float)statsClass[0]); System.Console.Out.WriteLine("--> Classification Error: " + (float)statsClass[1]); System.Console.Out.WriteLine( "***********************************************"); System.Console.Out.WriteLine(""); } }
*********************************************** --> Cross-Entropy Error: 0 --> Classification Error: 0 *********************************************** Weights Gradients 0 3.2662441281062 -1.07718767511655E-21 1 -3.65785075719586 9.08961951958712E-21 2 -1.34287873934408 4.06084823246345E-20 3 -1.35223399551086 1.06265766864622E-14 4 2.68535529295218 -7.71219393879981E-15 5 -2.87468867482566 6.14442307941349E-14 6 0.763542892743849 3.50677840927428E-42 7 2.27362473750162 -4.55519260305946E-42 8 5.01937720316383 -2.23318295615209E-42 9 4.54576678825492 -3.19544164040846E-18 10 -3.2703465073713 1.92921479004263E-15 11 -0.275420280883618 -2.00863231270801E-15 12 -5.9048518354049 1.72804459210703E-17 13 4.58077622000282 -1.04328902233367E-14 14 2.32407561540204 1.08624405719788E-14 15 -2.89362789116603 -8.61521051367963E-18 16 -6.4893352039981 5.20134410597363E-15 17 10.3829630951641 -5.41550390842253E-15 18 0.809208307413763 1.06265756092745E-14 19 0.251242239686325 -7.71218484918029E-15 20 1.41064046373263 6.14442714026172E-14 21 0.278456324867293 5.88451285169608E-18 22 0.345102584752059 -3.55271367880049E-15 23 -0.623558909619325 3.69899697182332E-15 Forecast X1 X2 X3 Y P(C1) P(C2) P(C3) Predicted 0 1 0 0 1 1 3.76564781867374E-30 1.95553436920243E-21 1 1 1 0 0 1 1 3.76564781867374E-30 1.95553436920243E-21 1 2 0 1 0 2 2.94225642584804E-18 0.999999999999998 1.84949653037729E-15 2 3 0 1 0 2 2.94225642584804E-18 0.999999999999998 1.84949653037729E-15 2 4 0 0 1 3 3.85758062517221E-43 5.41178908162658E-47 1 3 5 0 0 1 3 3.85758062517221E-43 5.41178908162658E-47 1 3 Cross-Entropy Error Value = 0 *********************************************** --> Cross-Entropy Error: 0 --> Classification Error: 0 ***********************************************Link to C# source.