Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }