Beispiel #1
0
        static void Main(string[] args)
        {
            string       outputFile = "..\\..\\IrisTrainTestStats.txt";
            FileStream   ofs        = new FileStream(outputFile, FileMode.Append, FileAccess.Write);
            StreamWriter sw         = new StreamWriter(ofs);

            sw.WriteLine("\nBegin neural network training demo");
            sw.WriteLine("\nData is the famous Iris flower set.");
            sw.WriteLine("Predict species from sepal length, width, petal length, width");
            sw.WriteLine("Iris setosa = 0 0 1, versicolor = 0 1 0, virginica = 1 0 0 \n");
            sw.WriteLine("Raw data resembles:");
            sw.WriteLine(" 5.1, 3.5, 1.4, 0.2, Iris setosa");
            sw.WriteLine(" 7.0, 3.2, 4.7, 1.4, Iris versicolor");
            sw.WriteLine(" 6.3, 3.3, 6.0, 2.5, Iris virginica");
            sw.WriteLine(" ......\n");

            double[][] allData  = new double[150][];
            string     dataFile = "..\\..\\IrisData.txt";

            allData = LoadData(dataFile, 150, 7);

            sw.WriteLine("\nFirst 6 rows of the 150-item data set:");
            ShowMatrix(sw, allData, 6, 1, true);

            sw.WriteLine("Creating 80% training and 20% test data matrices");
            double[][] trainData = null;
            double[][] testData  = null;
            MakeTrainTest(allData, 72, out trainData, out testData); // seed = 72 gives a pretty demo.

            sw.WriteLine("\nFirst 3 rows of training data:");
            ShowMatrix(sw, trainData, 3, 1, true);
            sw.WriteLine("First 3 rows of test data:");
            ShowMatrix(sw, testData, 3, 1, true);

            sw.WriteLine("\nCreating a 4-input, 7-hidden, 3-output neural network");
            sw.Write("Hard-coded tanh function for input-to-hidden and softmax for ");
            sw.WriteLine("hidden-to-output activations");
            int           numInput  = 4;
            int           numHidden = 7;
            int           numOutput = 3;
            NeuralNetwork nn        = new NeuralNetwork(numInput, numHidden, numOutput);

            int    maxEpochs = 1000;
            double learnRate = 0.05;
            double momentum  = 0.01;

            sw.WriteLine("Setting maxEpochs = " + maxEpochs + ", learnRate = " + learnRate + ", momentum = " + momentum);
            sw.WriteLine("Training has hard-coded mean squared " + "error < 0.040 stopping condition");

            sw.WriteLine("\nBeginning training using incremental back-propagation\n");
            nn.Train(trainData, maxEpochs, learnRate, momentum);
            sw.WriteLine("Training complete");

            double[] weights = nn.GetWeights();
            sw.WriteLine("Final neural network weights and bias values:");
            ShowVector(sw, weights, 10, 3, true);

            double trainAcc = nn.Accuracy(trainData);

            sw.WriteLine("\nAccuracy on training data = " + trainAcc.ToString("F4"));

            double testAcc = nn.Accuracy(testData);

            sw.WriteLine("\nAccuracy on test data = " + testAcc.ToString("F4"));

            sw.WriteLine("\nEnd neural network training demo\n");
            Console.ReadLine();

            sw.Close();
            ofs.Close();
        } // Main