Beispiel #1
0
        public static void AdalineTreshold(
            double start,
            double end,
            double step,
            int repetitions,
            double learningRate,
            double weightsLimit,
            int inputsCount,
            bool verbose = false)
        {
            if (start > end || step > Math.Abs(end - start) || step == 0)
            {
                throw new ArgumentException();
            }

            CsvPrinter.DumpParams(
                new KeyValuePair <string, object>("start", start),
                new KeyValuePair <string, object>("end", end),
                new KeyValuePair <string, object>("step", step),
                new KeyValuePair <string, object>("repetitions", repetitions),
                new KeyValuePair <string, object>("weightsLimit", weightsLimit),
                new KeyValuePair <string, object>("learningRate", learningRate),
                new KeyValuePair <string, object>("inputsCount", inputsCount),
                new KeyValuePair <string, object>("verbose", verbose)
                );
            CsvPrinter.DumpHeaderLine("n", "adaline threshold", "min epochs", "max epochs", "avg epochs", "final error");

            int experimentIndex = 0;

            for (double adalineThreshold = start; adalineThreshold <= end; adalineThreshold += step)
            {
                experimentIndex++;

                var minEpochs = int.MaxValue;
                var epochsSum = 0;
                var maxEpochs = 0;

                var        run = 0;
                Perceptron p   = null;
                if (verbose)
                {
                    ConsoleHelper.WriteYellowLine($"Experiment - {experimentIndex}");
                }
                try
                {
                    for (int j = 0; j < repetitions; j++)
                    {
                        run++;
                        p = PerceptronTrainer.CreatePerceptron(learningRate, weightsLimit, inputsCount, StepFunction.Bipolar, true);
                        var epochs = PerceptronTrainer.TrainPerceptron_And(p, adalineThreshold, verbose);

                        if (epochs < minEpochs)
                        {
                            minEpochs = epochs;
                        }
                        if (epochs > maxEpochs)
                        {
                            maxEpochs = epochs;
                        }

                        epochsSum += epochs;
                    }
                }
                catch (PerceptronLearnException)
                {
                    if (verbose)
                    {
                        ConsoleHelper.WriteErrorLine("Perceptron can't learn with params:");
                    }
                    p?.Dump();
                }
                var avarageEpochs = epochsSum / run;
                var currentError  = PerceptronTrainer.GetAdalineError(p);
                CsvPrinter.DumpLine(experimentIndex, adalineThreshold, minEpochs, maxEpochs, avarageEpochs, currentError);
            }
        }
Beispiel #2
0
        public static void WeightsRange(
            double start,
            double end,
            double step,
            int repetitions,
            double learningRate,
            StepFunction stepFunction,
            int inputsCount,
            bool useAdaline         = false,
            double adalineThreshold = 1,
            bool verbose            = false
            )
        {
            if (start > end || step > Math.Abs(end - start) || step == 0)
            {
                throw new ArgumentException();
            }

            CsvPrinter.DumpParams(
                new KeyValuePair <string, object>("start", start),
                new KeyValuePair <string, object>("end", end),
                new KeyValuePair <string, object>("step", step),
                new KeyValuePair <string, object>("repetitions", repetitions),
                new KeyValuePair <string, object>("learningRate", learningRate),
                new KeyValuePair <string, object>("stepFunction", stepFunction),
                new KeyValuePair <string, object>("inputsCount", inputsCount),
                new KeyValuePair <string, object>("useAdalvine", useAdaline),
                new KeyValuePair <string, object>("adalineThreshold", adalineThreshold),
                new KeyValuePair <string, object>("verbose", verbose)
                );
            CsvPrinter.DumpHeaderLine("n", "initial weights limit", "min epochs", "max epochs", "avg epochs");

            int experimentIndex = 0;

            for (double weightsLimit = start; weightsLimit <= end; weightsLimit += step)
            {
                experimentIndex++;

                var minEpochs = int.MaxValue;
                var epochsSum = 0;
                var maxEpochs = 0;

                var        run = 0;
                Perceptron p   = null;
                if (verbose)
                {
                    ConsoleHelper.WriteYellowLine($"Experiment - {experimentIndex}");
                }

                for (int j = 0; j < repetitions; j++)
                {
                    run++;
                    p = PerceptronTrainer.CreatePerceptron(learningRate, weightsLimit, inputsCount, stepFunction, useAdaline);
                    var epochs = PerceptronTrainer.TrainPerceptron_And(p, adalineThreshold, verbose);

                    if (epochs < minEpochs)
                    {
                        minEpochs = epochs;
                    }
                    if (epochs > maxEpochs)
                    {
                        maxEpochs = epochs;
                    }

                    epochsSum += epochs;
                }

                var avarageEpochs = epochsSum / run;
                CsvPrinter.DumpLine(experimentIndex, weightsLimit, minEpochs, maxEpochs, avarageEpochs);
            }
        }
Beispiel #3
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);
        }