Ejemplo n.º 1
0
        public static void AdalineError(
            double learningRate,
            double weightsLimit,
            double adalineThreshold,
            int inputsCount)
        {
            CsvPrinter.DumpParams(
                new KeyValuePair <string, object>("weightsLimit", weightsLimit),
                new KeyValuePair <string, object>("learningRate", learningRate),
                new KeyValuePair <string, object>("inputsCount", inputsCount)
                );
            CsvPrinter.DumpHeaderLine("n", "error");

            try
            {
                var p      = PerceptronTrainer.CreatePerceptron(learningRate, weightsLimit, inputsCount, StepFunction.Bipolar, true);
                var epochs = PerceptronTrainer.TrainPerceptron_And(p, adalineThreshold, dumpData: true);
            }
            catch (PerceptronLearnException)
            {
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            var options = new Options();
            var parser = Parser.Default;

            string invokedCommand = "";
            object invokedSubOptions = null;

            if (!parser.ParseArgumentsStrict(args, options, (command, subOptions) =>
            {
                invokedCommand = command;
                invokedSubOptions = subOptions;
            }))
            {
                Environment.Exit(Parser.DefaultExitCodeFail);
            }

            var verbose = ((CommonSubOptions)invokedSubOptions).Verbose;
            if (verbose)
            {
                Console.WriteLine(Options.GetHeading().ToString());
                Console.WriteLine();
            }

            switch (invokedCommand)
            {
                case "train":
                    var trainSubOptions = (TrainSubOptions)invokedSubOptions;

                    if (!trainSubOptions.Validate()) Environment.Exit(Parser.DefaultExitCodeFail);

                    var learningRate = trainSubOptions.LearningRate;
                    var stepFunction = trainSubOptions.StepFunction;
                    var weightsLimit = trainSubOptions.InitialWeights;
                    var useAdaline = trainSubOptions.UseAdaline;

                    var perceptron = PerceptronTrainer.CreatePerceptron(
                        learningRate,
                        weightsLimit,
                        2,
                        stepFunction,
                        useAdaline
                        );

                    if (useAdaline)
                    {
                        var adalineThreshold = trainSubOptions.AdalineThreshold;
                        PerceptronTrainer.TrainPerceptron_And(perceptron, adalineThreshold, verbose);
                    }
                    else
                    {
                        PerceptronTrainer.TrainPerceptron_And(perceptron);
                    }

                    if (!PerceptronTrainer.Test_And(perceptron, verbose))
                    {
                        Console.Error.WriteLine("Test data failed!");
                    }

                    if (trainSubOptions.Octave)
                    {
                        ConsoleHelper.WriteYellowLine("Octave plot code:");
                        Console.WriteLine(perceptron.GenerateOctaveCode());
                        Console.WriteLine();
                    }

                    Console.Write(perceptron.ToJson());

                    break;
                case "experiment":
                    var experimentSubOptions = (ExperimentSubOptions)invokedSubOptions;

                    if (!experimentSubOptions.Validate()) Environment.Exit(Parser.DefaultExitCodeFail);

                    switch (experimentSubOptions.Type)
                    {
                        case ExperimentRunner.ExperimentType.LearningRate:
                            ExperimentRunner.LearningRate(
                                experimentSubOptions.From,
                                experimentSubOptions.To,
                                experimentSubOptions.Step,
                                experimentSubOptions.Repetitions,
                                experimentSubOptions.InitialWeights,
                                experimentSubOptions.StepFunction,
                                2,
                                experimentSubOptions.UseAdaline,
                                experimentSubOptions.AdalineThreshold,
                                experimentSubOptions.Verbose
                                );
                            break;
                        case ExperimentRunner.ExperimentType.InitialWeights:
                            ExperimentRunner.WeightsRange(
                                experimentSubOptions.From,
                                experimentSubOptions.To,
                                experimentSubOptions.Step,
                                experimentSubOptions.Repetitions,
                                experimentSubOptions.LearningRate,
                                experimentSubOptions.StepFunction,
                                2,
                                experimentSubOptions.UseAdaline,
                                experimentSubOptions.AdalineThreshold,
                                experimentSubOptions.Verbose
                                );
                            break;
                        case ExperimentRunner.ExperimentType.AdalineThreshold:
                            ExperimentRunner.AdalineTreshold(
                                experimentSubOptions.From,
                                experimentSubOptions.To,
                                experimentSubOptions.Step,
                                experimentSubOptions.Repetitions,
                                experimentSubOptions.LearningRate,
                                experimentSubOptions.InitialWeights,
                                2,
                                experimentSubOptions.Verbose
                                );
                            break;
                        case ExperimentRunner.ExperimentType.AdalineError:
                            ExperimentRunner.AdalineError(
                                experimentSubOptions.LearningRate,
                                experimentSubOptions.InitialWeights,
                                experimentSubOptions.AdalineThreshold,
                                2
                                );
                            break;
                        case ExperimentRunner.ExperimentType.Error:
                            ExperimentRunner.Error(
                                experimentSubOptions.LearningRate,
                                experimentSubOptions.InitialWeights,
                                experimentSubOptions.StepFunction,
                                2
                                );
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }

                    break;
                case "test":
                    var testSubOptions = (TestSubOptions)invokedSubOptions;

                    if (!testSubOptions.Validate()) Environment.Exit(Parser.DefaultExitCodeFail);

                    var json = File.ReadAllText(testSubOptions.PerceptronJsonPath);

                    var p = PerceptronTrainer.CreatePerceptron(json);

                    if (p == null)
                    {
                        ConsoleHelper.WriteErrorLine("Perceptron json is invalid");
                        Environment.Exit(Parser.DefaultExitCodeFail);
                    }

                    if (testSubOptions.TestAnd) PerceptronTrainer.Test_And(p, true);

                    if (!double.IsNaN(testSubOptions.X1) && !double.IsNaN(testSubOptions.X2))
                    {
                        var input = new[] { testSubOptions.X1, testSubOptions.X2 };
                        Console.WriteLine(p.Feedforward(input));
                    }

                    if (testSubOptions.Octave) Console.Write(p.GenerateOctaveCode());

                    break;
                default:
                    Environment.Exit(Parser.DefaultExitCodeFail);
                    break;
            }

            Environment.Exit(0);
        }