Пример #1
0
        public void TestNeuralCreate()
        {
            var sets = new Tuple <double[], double[]>[]
            {
                new Tuple <double[], double[]>(new double[] { -0.066 }, new double[] { 0.09 }),
                new Tuple <double[], double[]>(new double[] { -0.1 }, new double[] { 0.18 }),
                new Tuple <double[], double[]>(new double[] { 0.5 }, new double[] { 0.27 }),
                new Tuple <double[], double[]>(new double[] { 0.7 }, new double[] { 0.36 }),
                new Tuple <double[], double[]>(new double[] { 0.04 }, new double[] { 0.45 }),
                new Tuple <double[], double[]>(new double[] { 0.035 }, new double[] { 0.54 }),
                new Tuple <double[], double[]>(new double[] { 0.03 }, new double[] { 0.63 }),
                new Tuple <double[], double[]>(new double[] { 0.038 }, new double[] { 0.72 }),
                new Tuple <double[], double[]>(new double[] { 0.06 }, new double[] { 0.81 }),
                new Tuple <double[], double[]>(new double[] { 0.02 }, new double[] { 0.90 }),
                new Tuple <double[], double[]>(new double[] { 0.1 }, new double[] { 0.99 }),
                new Tuple <double[], double[]>(new double[] { -0.2 }, new double[] { 1.08 })
            };

            var set2 = new Tuple <double[], double[]>[]
            {
                new Tuple <double[], double[]>(new double[] { 0, 1 }, new double[] { 1 }),
                new Tuple <double[], double[]>(new double[] { 1, 0 }, new double[] { 1 }),
                new Tuple <double[], double[]>(new double[] { 0, 0 }, new double[] { 0 }),
                new Tuple <double[], double[]>(new double[] { 1, 1 }, new double[] { 0 }),
            };

            var net     = new SNeuralNet(1, 1, 100, 1);
            var trainer = new NeuralNetTrainer()
                          .SetNet(net)
                          .SetDataSets(sets);

            trainer.EpochsCount = 100000;
            trainer.LearnRate   = 0.01;

            trainer.SimpleTrain();

            Console.WriteLine("");
            Console.WriteLine("Test");

            foreach (var t in sets)
            {
                var input      = t.Item1;
                var correctOut = t.Item2;

                var @out = trainer.Net.Activate(input);

                Console.WriteLine($"Input: {input[0]}\tOut: {@out[0]}\tCorrect: {correctOut[0]}");
            }
        }
Пример #2
0
        public void TestSimpleTask()
        {
            foreach (var net in Nets)
            {
                Trace.WriteLine($"layers: {net.HiddenLayersCount}\tneurons: {net.Hidden.First().NeuronsCount}");
                var trainer = new NeuralNetTrainer(net, Consts.XorSet, 10000, 0.01, 0.01, 5, Consts.TraceLog);
                trainer.SimpleTrain();

                foreach (var e in Consts.XorSet)
                {
                    var response = net.Activate(e.Item1);
                    Trace.WriteLine($"{e.Item1[0]} - {e.Item1[1]}\tResponse: {response[0]}\tCorrect: {e.Item2[0]}");
                }
            }
        }
Пример #3
0
        public async Task TestSimple1100Task()
        {
            var net     = NetSimple11000;
            var trainer = new NeuralNetTrainer(net, Consts.XorSet, 50, 0.001, 0.0001, 1, Consts.TraceLog);
            var result  = trainer.SimpleTrain();

            foreach (var e in Consts.XorSet)
            {
                var response = net.Activate(e.Item1);
                Trace.WriteLine($"{e.Item1[0]} - {e.Item1[1]}\tResponse: {response[0]}\tCorrect: {e.Item2[0]}");
            }

            if (result.ResultError < 0.2)
            {
                await MRSerializer.ToFile(FILE_PATH, result.TargetNet, true);
            }
        }
Пример #4
0
        public void TestWords(Log log)
        {
            var net     = new SNeuralNet(6, 2, 300, 1);
            var wordBag = WordBag.CreateToWords(string.Join(". ", Configuration.RawDataList), 1);

            var trainSets = new List <Tuple <double[], double[]> >();

            var wordsHistory = new List <string>();
            var vocab        = new LRVocab().Create(Configuration.VocabularyPath, (string s) => log(s));

            log("Prepare tests list");

            foreach (var word in wordBag.Read())
            {
                var w = word[0];

                if (!vocab.Vocabulary.ContainsWord(w))
                {
                    continue;
                }

                wordsHistory.Add(w);
                if (wordsHistory.Count < 4)
                {
                    continue;
                }
                if (wordsHistory.Count > 4)
                {
                    wordsHistory.RemoveAt(0);
                }

                double[] input   = new double[6];
                double[] correct = new double[1];

                input[0] = vocab.Vocabulary.GetRepresentationOrNullFor(wordsHistory[0]).MetricLength;
                input[1] = vocab.Vocabulary.GetRepresentationOrNullFor(wordsHistory[1]).MetricLength;
                input[2] = vocab.Vocabulary.GetRepresentationOrNullFor(wordsHistory[2]).MetricLength;

                input[3] = vocab.Vocabulary.GetSummRepresentationOrNullForPhrase(wordsHistory.Take(2).ToArray())?.MetricLength ?? 0d;
                input[4] = vocab.Vocabulary.GetSummRepresentationOrNullForPhrase(wordsHistory.Skip(1).Take(2).ToArray())?.MetricLength ?? 0d;
                input[5] = vocab.Vocabulary.GetSummRepresentationOrNullForPhrase(wordsHistory.Take(3).ToArray())?.MetricLength ?? 0d;

                correct = new double[] { vocab.Vocabulary.GetRepresentationFor(wordsHistory[3]).MetricLength };

                trainSets.Add(new Tuple <double[], double[]>(input, correct));
            }

            if (trainSets.Count == 0)
            {
                log("No train sets");
                return;
            }

            log($"Train sets count: {trainSets.Count}");
            log($"Train starts");

            var trainer = new NeuralNetTrainer()
                          .SetDataSets(trainSets.ToArray())
                          .SetNet(net);

            trainer.EpochsCount = 150;
            trainer.LearnRate   = 0.001;

            trainer.SimpleTrain();
            log("Train end");

            foreach (var set in trainSets)
            {
                log($"Input: {set.Item1[0]} {set.Item1[1]} {set.Item1[2]}\t Result: {net.Activate(set.Item1)}");
            }
        }
Пример #5
0
        public async Task NeuralWordsTest()
        {
            // get GAME OF THRONES

            string regexText = string.Empty;

            try
            {
                var fullText = await File.ReadAllTextAsync(Consts.GAME_OF_THRONES_PATH);

                regexText = new Regex("Page [0-9]+").Replace(fullText, string.Empty);
            }
            catch (Exception ex)
            {
                var e = ex;
            }


            var vReader = new VReader(Consts.VOCAB_PATH);

            vReader.UploadBinary();

            var bag = MRWordBag.CreateToWords(regexText, 4);

            // create traine vectors
            var allSet = new List <Tuple <double[], double[]> >();

            foreach (var step in bag.Read())
            {
                bool isValid = true;
                foreach (var v in step)
                {
                    if (!vReader.Vocab.ContainsWord(v) || !vReader.Vocab.ContainsWord(v))
                    {
                        isValid = false;
                        break;
                    }
                }

                if (!isValid)
                {
                    continue;
                }

                var           forInput = step.Take(3);
                List <double> input    = new List <double>();
                foreach (var i in forInput)
                {
                    input.AddRange(vReader.Vocab.GetRepresentationFor(i).NumericVector.Select(x => (double)x).ToList());
                }

                var      forOut = step.Last();
                double[] output = vReader.Vocab.GetRepresentationFor(forOut).NumericVector.Select(x => (double)x).ToArray();

                allSet.Add(new Tuple <double[], double[]>(input.ToArray(), output));
            }

            var trainSet = allSet.Take(allSet.Count - 10).ToArray();
            var checkSet = allSet.TakeLast(10).ToArray();

            var trainRates = new double[] { 0.00005d, 0.00001d };

            foreach (var rate in trainRates)
            {
                foreach (var net in NetsWordTest)
                {
                    Trace.WriteLine($"Train net: layers: {net.HiddenLayersCount} | neurons: {net.Hidden.First().NeuronsCount}\tRate: {rate}");
                    var trainer     = new NeuralNetTrainer(net, trainSet, 500, 1, rate, 1, Consts.TraceLog);
                    var trainResult = trainer.SimpleTrain();

                    Trace.WriteLine("-- check net --");
                    foreach (var s in checkSet)
                    {
                        var response = net.Activate(s.Item1);

                        var responseR    = new Representation(response.Select(x => (float)x).ToArray());
                        var responseWord = vReader.Vocab.Distance(responseR, 1)?.FirstOrDefault()?.Representation;

                        var correct = vReader.Vocab.Distance(new Representation(s.Item2.Select(x => (float)x).ToArray()), 1)?.FirstOrDefault()?.Representation;

                        Trace.WriteLine($"Correct: {correct.WordOrNull}\tResponse: {responseWord.WordOrNull}");
                    }

                    var name = $"Neural net ({net.HiddenLayersCount}-{net.Hidden.First().NeuronsCount}-epochs-{trainResult.EpochFinished}-error-{trainResult.ResultError}-time-{trainResult.TotalTimeMs})";
                    await MRSerializer.ToFile($"d://{name}.txt", net, true);
                }
            }
        }