Beispiel #1
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);
        }
Beispiel #2
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 #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);
        }