Compute() public method

Compute the output for a given input to the neural network.
public Compute ( IMLData input ) : IMLData
input IMLData The input to the neural network.
return IMLData
Esempio n. 1
0
        static void Main(string[] args)
        {
            //create a neural network withtout using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));

            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            int epoch = 1;
            do
            {
                train.Iteration();
                Console.WriteLine($"Epoch #{epoch} Error: {train.Error}");
                epoch++;
            } while (train.Error > 0.01);
            train.FinishTraining();

            Console.WriteLine("Neural Network Results:");
            foreach (IMLDataPair iPair in trainingSet)
            {
                IMLData output = network.Compute(iPair.Input);
                Console.WriteLine($"{iPair.Input[0]}, {iPair.Input[0]}, actual={output[0]}, ideal={iPair.Ideal[0]}");
            }

            EncogFramework.Instance.Shutdown();

            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            var trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            var train = new ResilientPropagation(network, trainingSet);
            var epoch = 1;
            do
            {
                train.Iteration();

            } while (train.Error > 0.01);

            train.FinishTraining();

            foreach (var pair in trainingSet)
            {
                var output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @", " + pair.Input[1] + @" , actual=" + output[0] + @", ideal=" + pair.Ideal[0]);
            }

            EncogFramework.Instance.Shutdown();
            Console.ReadLine();
        }
Esempio n. 3
0
        public static double EvaluateNetworks(BasicNetwork network, BasicMLDataSet set)
        {
            int count = 0;
            int correct = 0;
            foreach (IMLDataPair pair in set)
            {
                IMLData input = pair.Input;
                IMLData actualData = pair.Ideal;
                IMLData predictData = network.Compute(input);

                double actual = actualData[0];
                double predict = predictData[0];
                double diff = Math.Abs(predict - actual);

               Direction  actualDirection = DetermineDirection(actual);
               Direction predictDirection = DetermineDirection(predict);

                if (actualDirection == predictDirection)
                    correct++;
                count++;
                Console.WriteLine(@"Number" + @"count" + @": actual=" + Format.FormatDouble(actual, 4) + @"(" + actualDirection + @")"
                                  + @",predict=" + Format.FormatDouble(predict, 4) + @"(" + predictDirection + @")" + @",diff=" + diff);
               
            }
            double percent = correct / (double)count;
            Console.WriteLine(@"Direction correct:" + correct + @"/" + count);
            Console.WriteLine(@"Directional Accuracy:"
                              + Format.FormatPercent(percent));

            return percent;
        }
        /// <summary>
        ///   Measure the performance of the network
        /// </summary>
        /// <param name = "network">Network to analyze</param>
        /// <param name = "dataset">Dataset with input and ideal data</param>
        /// <returns>Error % of correct bits, returned by the network.</returns>
        public static double MeasurePerformance(BasicNetwork network, BasicNeuralDataSet dataset)
        {
            int correctBits = 0;
            float threshold = 0.0f;
            IActivationFunction activationFunction = network.GetActivation(network.LayerCount - 1); //get the activation function of the output layer
            if (activationFunction is ActivationSigmoid)
            {
                threshold = 0.5f; /* > 0.5, range of sigmoid [0..1]*/
            }
            else if (activationFunction is ActivationTANH)
            {
                threshold = 0.0f; /*> 0, range of bipolar sigmoid is [-1..1]*/
            }
            else
                throw new ArgumentException("Bad activation function");
            int n = (int) dataset.Count;

            Parallel.For(0, n, (i) =>
                               {
                                   IMLData actualOutputs = network.Compute(dataset.Data[i].Input);
                                   lock (LockObject)
                                   {
                                       for (int j = 0, k = actualOutputs.Count; j < k; j++)
                                           if ((actualOutputs[j] > threshold && dataset.Data[i].Ideal[j] > threshold)
                                               || (actualOutputs[j] < threshold && dataset.Data[i].Ideal[j] < threshold))
                                               correctBits++;
                                   }
                               });

            long totalOutputBitsCount = dataset.Count*dataset.Data[0].Ideal.Count;

            return (double) correctBits/totalOutputBitsCount;
        }
 public void EvaluateNetwork(BasicNetwork trainedNetwork, BasicMLDataSet trainingData)
 {
     foreach (var trainingItem in trainingData)
     {
         var output = trainedNetwork.Compute(trainingItem.Input);
         Console.WriteLine("Input:{0}, {1}  Ideal: {2}  Actual : {3}", trainingItem.Input[0], trainingItem.Input[1], trainingItem.Ideal, output[0]);
     }
     Console.ReadKey();
 }
Esempio n. 6
0
        private static void Main(string[] args)
        {
            // create a neural network, without using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

            // train the neural network
            IMLTrain train = new Backpropagation(network, trainingSet, 0.5, 0.2);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            }
            while (train.Error > 0.01);

            // test the neural network

            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }

            Console.Read();
        }
Esempio n. 7
0
        public override void Run()
        {
            testNetwork = new BasicNetwork();

            testNetwork.AddLayer(new BasicLayer(null, true, 2));
            testNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 4));
            testNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            testNetwork.Structure.FinalizeStructure();
            testNetwork.Reset();

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

            // train the neural network
            IMLTrain train = new Backpropagation(testNetwork, trainingSet);
            //IMLTrain train = new ResilientPropagation(testNetwork, trainingSet); //Encog manual says it is the best general one

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.0001);

            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = testNetwork.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
        }
        public void Predict(BasicNetwork network)
        {
            Console.WriteLine(@"Year    Actual    Predict     Closed Loop     Predict    Denormalized Value   Real Value");

            for (int year = EvaluateStart; year < EvaluateEnd; year++)
            {
                // calculate based on actual data
                IMLData input = new BasicMLData(WindowSize);
                for (var i = 0; i < input.Count; i++)
                {
                    input.Data[i] = _normalizedSunspots[(year - WindowSize) + i];
                }
                IMLData output = network.Compute(input);
                double prediction = output.Data[0];
                _closedLoopSunspots[year] = prediction;

                // calculate "closed loop", based on predicted data
                for (var i = 0; i < input.Count; i++)
                {
                    input.Data[i] = _closedLoopSunspots[(year - WindowSize) + i];
                }
                output = network.Compute(input);
                double closedLoopPrediction = output.Data[0];

                // display
                Console.WriteLine((StartingYear + year)
                                  + @"  " + Format.FormatDouble(_normalizedSunspots[year], 5)
                                  + @"  " + Format.FormatDouble(prediction, 5)
                                  + @"  " + Format.FormatDouble(closedLoopPrediction, 5)
                                  + @" Accuracy:" +
                                  Format.FormatDouble(_normalizedSunspots[year] - prediction, 5)
                                  + " Denormalized:" + array.Stats.DeNormalize(prediction)
                                  + " Real value:" + Sunspots[year]);

            }
        }
        public void Predict(BasicNetwork network)
        {
            Console.WriteLine(@"Year    Actual    Predict     Closed Loop     Predict    Denormalized Value   Real Value");

            for (var year = EvaluateStart; year < EvaluateEnd; year++)
            {
                // calculate based on actual data
                var input = new BasicMLData(WindowSize);
                for (var i = 0; i < input.Count; i++)
                {
                    input[i] = _normalizedForexPair[(year - WindowSize) + i];
                }
                IMLData output = network.Compute(input);
                var prediction = output[0];
                _closedLoopForexPair[year] = prediction;

                // calculate "closed loop", based on predicted data
                for (var i = 0; i < input.Count; i++)
                {
                    input[i] = _closedLoopForexPair[(year - WindowSize) + i];
                }
                output = network.Compute(input);
                var closedLoopPrediction = output[0];

                // display
                Console.WriteLine("{0}  {1}  {2}  {3} Accuracy:{4} Denormalized:{5} Real value:{6}",
                    (StartingYear + year),
                    Format.FormatDouble(_normalizedForexPair[year], 5),
                    Format.FormatDouble(prediction, 5),
                    Format.FormatDouble(closedLoopPrediction, 5),
                    Format.FormatDouble(_normalizedForexPair[year] - prediction, 5),
                    array.Stats.DeNormalize(prediction),
                    ForexPair[year]);
            }
        }
Esempio n. 10
0
        // Wrap it to be linq friendly
        private static double CallNN(BasicNetwork network, double input1, double input2)
        {
            double[] input = new[] { input1, input2 };
            double[] output = new double[1];

            network.Compute(input, output);

            return output[0];
        }
Esempio n. 11
0
        public void Predict(BasicNetwork network)
        {
            Console.WriteLine(@"Year    Actual  Predict Closed Loop Predict");

            for (int year = EvaluateStart; year < EvaluateEnd; year++)
            {
                // calculate based on actual data
                var input = new BasicMLData(WindowSize);
                for (var i = 0; i < input.Count; i++)
                {
                    input[i] = _normalizedSunspots[(year - WindowSize) + i];
                }
                IMLData output = network.Compute(input);
                double prediction = output[0];
                _closedLoopSunspots[year] = prediction;

                // calculate "closed loop", based on predicted data
                for (var i = 0; i < input.Count; i++)
                {
                    input[i] = _closedLoopSunspots[(year - WindowSize) + i];
                }
                output = network.Compute(input);
                double closedLoopPrediction = output[0];

                // display
                Console.WriteLine((StartingYear + year)
                                  + @"  " + Format.FormatDouble(_normalizedSunspots[year], 2)
                                  + @"  " + Format.FormatDouble(prediction, 2)
                                  + @"  " + Format.FormatDouble(closedLoopPrediction, 2));
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Evaluate the network and display (to the console) the output for every
        /// value in the training set. Displays ideal and actual.
        /// </summary>
        /// <param name="network">The network to evaluate.</param>
        /// <param name="training">The training set to evaluate.</param>
        public static void Evaluate(BasicNetwork network,
                 INeuralDataSet training)
        {
            foreach (INeuralDataPair pair in training)
            {
                INeuralData output = network.Compute(pair.Input);
                Console.WriteLine("Input="
                        + EncogUtility.FormatNeuralData(pair.Input)
                        + ", Actual=" + EncogUtility.FormatNeuralData(output)
                        + ", Ideal="
                        + EncogUtility.FormatNeuralData(pair.Ideal));

            }
        }
        public List<double[]> Learn(double[][] data, double[][] ideal)
        {
            double[][] origData = (double[][])data.Clone();
            int n = data.Length;
            int m = data[0].Length;
            double[][] output = new double[n][];
            double[][] sgmNeighbours = new double[n][];
            for (var i = 0; i < n; i++)
            {
                double[] sgmN = new double[SegmentationData.SEGMENT_NEIGHBOURS];
                Array.Copy(data[i], m - SegmentationData.SEGMENT_NEIGHBOURS, sgmN, 0, SegmentationData.SEGMENT_NEIGHBOURS);
                sgmNeighbours[i] = sgmN;
                data[i] = data[i].Take(m - SegmentationData.SEGMENT_NEIGHBOURS).ToArray();
                output[i] = new double[m - SegmentationData.SEGMENT_NEIGHBOURS];
                data[i].CopyTo(output[i], 0);
            }

            IMLDataSet trainingSet = new BasicMLDataSet(data, output);

            int inputLayerSize = layersConfiguration[0] - SegmentationData.SEGMENT_NEIGHBOURS;
            int trainingLayerSize = layersConfiguration[1];
            BasicNetwork oneLayerAutoencoder = new BasicNetwork();
            oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
            oneLayerAutoencoder.Structure.FinalizeStructure();
            oneLayerAutoencoder.Reset();

            IMLTrain train = new ResilientPropagation(oneLayerAutoencoder, trainingSet);
            //IMLTrain train = new Backpropagation(oneLayerAutoencoder, trainingSet, LEARNING_RATE, MOMENTUM);

            int epoch = 1;
            List<double[]> errors = new List<double[]>();
            double[] trainError = new double[AUTOENCODER_MAX_ITER];

            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;
                //errors.Add(train.Error);
            } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            BasicNetwork encoder = new BasicNetwork();
            encoder.AddLayer(new BasicLayer(null, BIAS, oneLayerAutoencoder.GetLayerNeuronCount(0)));
            encoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
            encoder.Structure.FinalizeStructure();
            encoder.Reset();

            //przypisanie wag do encodera
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                        encoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(i, f, t));

            //Compare2Networks(oneLayerAutoencoder, encoder);

            for(int l=1; l<layersConfiguration.Count -2; l++)
            {
                inputLayerSize = layersConfiguration[l];
                trainingLayerSize = layersConfiguration[l+1];
                oneLayerAutoencoder = new BasicNetwork();
                oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
                oneLayerAutoencoder.Structure.FinalizeStructure();
                oneLayerAutoencoder.Reset();

                //liczenie outputu z dotychczasowego encodera
                double[][] input = new double[n][];
                double[][] newOutput = new double[n][];
                for(int ni = 0; ni <n; ni++)
                {
                    IMLData res = encoder.Compute(new BasicMLData(data[ni]));
                    double[] resD = new double[res.Count];
                    for(int i=0; i<res.Count; i++)
                        resD[i] = res[i];
                    input[ni] = resD;
                    newOutput[ni] = new double[res.Count];
                    input[ni].CopyTo(newOutput[ni], 0);
                }

                BasicMLDataSet newTrainingSet = new BasicMLDataSet(input, newOutput);
                train = new ResilientPropagation(oneLayerAutoencoder, newTrainingSet);
                //train = new Backpropagation(oneLayerAutoencoder, newTrainingSet, LEARNING_RATE, MOMENTUM);

                epoch = 1;
                trainError = new double[AUTOENCODER_MAX_ITER];
                do
                {
                    train.Iteration();
                    ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                    Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                    trainError[epoch - 1] = train.Error;
                    epoch++;
                } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
                errors.Add(trainError);
                train.FinishTraining();

                BasicNetwork extendedEncoder = new BasicNetwork();
                extendedEncoder.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0)));
                for (int el = 1; el < encoder.LayerCount; el++ )
                    extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el)));
                extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
                extendedEncoder.Structure.FinalizeStructure();

                //przypisanie wag do extendedencodera
                for (int i = 0; i < extendedEncoder.LayerCount - 1; i++)
                {
                    if (i < encoder.LayerCount-1)
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }
                    else
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(0, f, t));
                    }
                }
                encoder = extendedEncoder;

            }

            //tworzenie struktury ostatecznej sieci
            network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0) + SegmentationData.SEGMENT_NEIGHBOURS));
            for (int el = 1; el < encoder.LayerCount; el++)
                network.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el) + SegmentationData.SEGMENT_NEIGHBOURS));
            network.AddLayer(new BasicLayer(CurrentActivationFunction(), false, layersConfiguration[layersConfiguration.Count - 1]));
            network.Structure.FinalizeStructure();
            network.Reset();

            /*
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
            */
            //dla innych ustawic wagi 0, dla samych sobie 1

            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < network.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < network.GetLayerNeuronCount(i + 1); t++)
                    {
                        if (f < encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t < encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 1);
                        else
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }

            //uczenie koncowej sieci
            trainingSet = new BasicMLDataSet(origData, ideal);

            train = new ResilientPropagation(network, trainingSet);
            //train = new Backpropagation(network, trainingSet, LEARNING_RATE, MOMENTUM);

            epoch = 1;
            trainError = new double[FINAL_NETWORK_MAX_ITER];
            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;

            } while (train.Error > EPS && epoch < FINAL_NETWORK_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            try
            {
                string networkFileName = "autoencoder wo cmp 300 125 50 3";
                EncogDirectoryPersistence.SaveObject(new FileInfo(networkFileName), network);
                MessageBox.Show("NETWORK SAVED TO FILE " + networkFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return errors;
        }
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="app">Holds arguments and other info.</param>
        public void Execute(IExampleInterface app)
        {
            // create a neural network, without using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

            // train the neural network using online (batch=1)
            Propagation train = new Backpropagation(network, trainingSet, 0.7, 0.3);
            train.BatchSize = 1;

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);

            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
        }
Esempio n. 15
0
        public void TestModel(string testDataPath, BasicNetwork model, ProblemType problem, ActivationType activation)
        {
            TestDataPath = testDataPath;
            var csvReader = new ReadCSV(testDataPath, true, CSVFormat.DecimalPoint);

            var values = new List<double[]>();
            var originalValues = new List<double[]>();

            while (csvReader.Next())
            {
                values.Add(ProblemType.Classification == problem
                    ? new[] {csvReader.GetDouble(0), csvReader.GetDouble(1)}
                    : new[] {csvReader.GetDouble(0)});

                originalValues.Add(ProblemType.Classification == problem
                    ? new[] { csvReader.GetDouble(0), csvReader.GetDouble(1) }
                    : new[] { csvReader.GetDouble(0) });
            }

            csvReader.Close();

            Normalize(values, _valuesMins, _valuesMaxes, activation);

            var answers = new List<double>();
            foreach (var value in values)
            {
                var answer = new double[LastLayerSize];
                model.Compute(value, answer);
                answers.Add(problem == ProblemType.Regression ? DenormalizeAnswer(answer[0], activation) : GetClassFromAnswer(answer));
            }

            AnswerPath = Path.GetFullPath(TestDataPath) + ".solved";

            var lines = new List<string>();
            lines.Add(problem == ProblemType.Classification ? "x,y,clc" : "x,y");

            lines.AddRange(answers.Select((t, i) =>
                problem == ProblemType.Regression
                ? originalValues[i][0].ToString(CultureInfo.InvariantCulture) + "," + t.ToString(CultureInfo.InvariantCulture)
                : originalValues[i][0].ToString(CultureInfo.InvariantCulture) + "," + originalValues[i][1].ToString(CultureInfo.InvariantCulture) + "," + t.ToString(CultureInfo.InvariantCulture)));

            File.WriteAllLines(AnswerPath, lines);
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            double[][] input = CsvReader.ConvertCsvToTwoDimDoubleArrary(@"X.csv");
            double[][] idealTemp = CsvReader.ConvertCsvToTwoDimDoubleArrary(@"Y.csv");
            double[][] ideal = convertIdealArrary(idealTemp, 10);

            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 400));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 25));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 10));
            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(input, ideal);

            //IMLTrain train = new Backpropagation(network, trainingSet);
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            /*
            int epoch = 1;
            do
            {
                train.Iteration();

                if (epoch % 10 == 0)
                {
                    Console.WriteLine(@"Epoch# " + epoch + @" Error: " + train.Error);
                }

                epoch++;
            }
            while (train.Error > 0.01);
            */

            for (int epoch = 1; epoch < 1000; epoch++)
            {
                train.Iteration();

                if (epoch % 10 == 0)
                {
                    Console.WriteLine(@"Epoch# " + epoch + @" Error: " + train.Error);
                }
            }

            train.FinishTraining();

            Console.WriteLine("Training Completed");

            Console.WriteLine(@"Neural network results:");
            double successCount = 0;
            double totalCount = 0;
            foreach (IMLDataPair pair in trainingSet)
            {
                totalCount++;
                IMLData output = network.Compute(pair.Input);

                double max1 = -99;
                int index1 = -1;
                for (int i = 0; i < output.Count; i++)
                {
                    if (output[i] > max1)
                    {
                        max1 = output[i];
                        index1 = i;
                    }
                }

                double max2 = -99;
                int index2 = -2;
                for (int i = 0; i < pair.Ideal.Count; i++)
                {
                    if (pair.Ideal[i] > max2)
                    {
                        max2 = pair.Ideal[i];
                        index2 = i;
                    }
                }

                if (index1 == index2)
                {
                    successCount++;
                }
            }

            double rate = successCount / totalCount;
            Console.WriteLine(rate);

            EncogFramework.Instance.Shutdown();
            Console.ReadLine();
        }