Exemplo n.º 1
0
        public static int TrainPerceptron_And(
            Perceptron perceptron,
            double adalineThreshold = 1,
            bool verbose            = false,
            bool dumpData           = false
            )
        {
            bool isTrained = false;
            int  epoch     = 0;

            if (perceptron.IsAdaline)
            {
                if (verbose)
                {
                    ConsoleHelper.WriteYellowLine($"Using adaline, error treshold - {adalineThreshold}");
                }
                double errorSum;
                do
                {
                    epoch++;
                    errorSum = 0;
                    if (verbose)
                    {
                        ConsoleHelper.WriteYellow($"Learning epoch - {epoch}");
                    }
                    foreach (var trainObject in andTraingData.Shuffle())
                    {
                        errorSum += Math.Pow(perceptron.Train(trainObject), 2);
                    }

                    errorSum /= andTraingData.Count;

                    if (verbose)
                    {
                        ConsoleHelper.WriteLine($" current error - {errorSum}");
                    }

                    if (dumpData)
                    {
                        CsvPrinter.DumpLine(epoch, errorSum);
                    }

                    if (epoch <= MaximumEpochs)
                    {
                        continue;
                    }
                    if (verbose)
                    {
                        ConsoleHelper.WriteErrorLine("Stopping!");
                        ConsoleHelper.WriteErrorLine("Did not learn nothing in 10000 epochs!");
                        ConsoleHelper.WriteErrorLine($"Using adaline, current values: error-{errorSum} > threshold-{adalineThreshold}");
                    }

                    return(0);
                }while (errorSum > adalineThreshold);
            }
            else
            {
                while (!isTrained)
                {
                    epoch++;
                    if (verbose)
                    {
                        ConsoleHelper.WriteLine($"Learning epoch - {epoch}");
                    }

                    double errorSum = 0;
                    errorSum = 0;
                    foreach (var trainObject in andTraingData)
                    {
                        var error = Math.Abs(perceptron.Train(trainObject));
                        errorSum += error;
                    }
                    if (Math.Abs(errorSum) < 0.000001)
                    {
                        isTrained = true;
                    }

                    if (dumpData)
                    {
                        CsvPrinter.DumpLine(epoch, errorSum);
                    }
                    if (epoch <= MaximumEpochs)
                    {
                        continue;
                    }
                    if (verbose)
                    {
                        ConsoleHelper.WriteErrorLine("Stopping!");
                        ConsoleHelper.WriteErrorLine("Did not learn nothing in 10000 epochs!");
                    }

                    return(0);
                }
            }
            return(epoch);
        }