Ejemplo n.º 1
0
        public static void Learn(INeuralDataSet learningSet, INeuralDataSet testingSet, int inputSize, InputClass inputData, int testingSize, int index)
        {
            string        logDir   = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            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();
                errors.Add(Network.Error);
                iteracja++;
            } while ((iteracja < iteracje) && (Network.Error > 0.0001) && (Network.Error < 10000));
            stopwatch.Stop();

            String timeStamp = NetworkHelper.GetTimestamp(DateTime.Now);
            /// TUTAJ SIEC SIE TEORETYCZNIE NAUCZYLA
            /// TERAZ ZBIOR TESTOWY, WYNIKI
            /// I WYKRES ERRORA
            ///
            string errDir   = logDir + "\\ERRORS\\err_" + timeStamp + "_" + ".r";
            string log_name = timeStamp + "_" + index;

            Logger(inputData.ToString(), logDir, log_name);

            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);
            Logger("Neural Network Learning Result" + Environment.NewLine, logDir, log_name);
            Logger(errors[errors.Count - 1] + " %" + Environment.NewLine, logDir, log_name);
            Logger("Neural Network Testing Result" + Environment.NewLine, logDir, log_name);
            double calculateError = NetworkHelper.CalculateFinalError(answers, testingSet);

            Logger("Error: " + calculateError + " %" + Environment.NewLine, logDir, log_name);
            Logger(String.Format("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed) + Environment.NewLine, logDir, log_name);
            Console.WriteLine("FINISH");

            NetworkHelper.CreateErrorFile(errors, errDir);
        }
Ejemplo n.º 2
0
        public static ITrain LearnNetwork(INeuralDataSet learningSet, INeuralDataSet testingSet, int inputSize, InputClass inputData, int testingSize)
        {
            string        logDir   = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            int           iteracje = inputData.iterations;
            List <double> errors   = new List <double>();

            Console.WriteLine("Tworze siec...");
            ITrain Network = 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();

            String timeStamp = GetTimestamp(DateTime.Now);
            /// TUTAJ SIEC SIE TEORETYCZNIE NAUCZYLA
            /// TERAZ ZBIOR TESTOWY, WYNIKI
            /// I WYKRES ERRORA
            ///

            string errDir = logDir + "ERRORS\\err_" + timeStamp + ".r";

            Logger(inputData.ToString(), logDir, timeStamp);

            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 = DenormaliseAnswers(neuralAnswer);
            Logger("Neural Network Learning Result" + Environment.NewLine, logDir, timeStamp);
            Logger(errors[errors.Count - 1] + " %" + Environment.NewLine, logDir, timeStamp);
            Console.WriteLine("Neural Network Results");
            Logger("Neural Network Testing Result" + Environment.NewLine, logDir, timeStamp);
            double calculateError = CalculateFinalError(answers, testingSet);

            Console.WriteLine("Error: " + calculateError + " %");
            Logger("Error: " + calculateError + " %" + Environment.NewLine, logDir, timeStamp);
            Console.WriteLine("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed);
            Logger(String.Format("Time elapsed: {0:hh\\:mm\\:ss}", stopwatch.Elapsed) + Environment.NewLine, logDir, timeStamp);
            Console.WriteLine("FINISH");

            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";
            CreateErrorFile(errors, errDir);


            return(Network);
        }