Exemplo n.º 1
0
        public static Tuple <double, ConfusionMatrix, double, double> LearnNetwork(INeuralDataSet learningSet, INeuralDataSet testingSet, int inputSize, InputClass inputData, int testingSize)
        {
            int           iteracje = inputData.iterations;
            List <double> errors   = new List <double>();

            Console.WriteLine("Tworze siec...");
            ITrain Network = NetworkHelper.CreateNeuronNetwork(learningSet, inputSize, inputData);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int iteracja = 0;

            do
            {
                Network.Iteration();
                Console.WriteLine("Epoch #" + iteracja + " Error:" + Network.Error);
                errors.Add(Network.Error);
                iteracja++;
            } while ((iteracja < iteracje) && (Network.Error > 0.0001) && (Network.Error < 10000));
            stopwatch.Stop();

            /// TUTAJ SIEC SIE TEORETYCZNIE NAUCZYLA

            double[] neuralAnswer = new double[testingSize];
            int      i            = 0;

            foreach (INeuralDataPair pair in testingSet)
            {
                INeuralData output = Network.Network.Compute(pair.Input);
                double      small  = 0.0;
                for (int r = 0; r < 4; r++)
                {
                    if ((double)(output[r]) >= small)
                    {
                        neuralAnswer[i] = (double)r;
                        small           = (double)(output[r]);
                    }
                }

                i++;
            }
            int[]  answers        = NetworkHelper.DenormaliseAnswers(neuralAnswer);
            double calculateError = NetworkHelper.CalculateFinalError(answers, testingSet);


            if ((errors[errors.Count - 1] * 100).ToString().Length > 4)
            {
                inputData.learningError = (errors[errors.Count - 1] * 100).ToString().Substring(0, 4) + " %";
            }
            else
            {
                inputData.learningError = (errors[errors.Count - 1] * 100).ToString() + " %";
            }
            if (calculateError.ToString().Length > 4)
            {
                inputData.testingError = calculateError.ToString().Substring(0, 4) + " %";
            }
            else
            {
                inputData.testingError = calculateError.ToString() + " %";
            }
            inputData.timeElapsed = stopwatch.Elapsed.Hours + "h " + stopwatch.Elapsed.Minutes + "min " + stopwatch.Elapsed.Seconds + "sec";

            ConfusionMatrix confusionMatrix = new ConfusionMatrix(4);

            CalculateConfusionMatrix(confusionMatrix, answers, testingSet);
            double precission = CalculatePrecission(confusionMatrix);
            double recall     = CalculateRecall(confusionMatrix);

            return(new Tuple <double, ConfusionMatrix, double, double>(calculateError, confusionMatrix, precission, recall));
        }