コード例 #1
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)}");
            }
        }