コード例 #1
0
        public void TestReadAndFillDoesntChangeError()
        {
            var tests = new[]
            {
                new[] { new[] { 1f, 1f }, new[] { -1f } },
                new[] { new[] { 1f, -1f }, new[] { 1f } },
                new[] { new[] { -1f, 1f }, new[] { 1f } },
                new[] { new[] { -1f, -1f }, new[] { -1f } }
            };

            var builder = new LayerBuilder();
            var desc    = builder.BuildDescription(2, new[]
            {
                new LayerBuilder.LayerSpec(2, "sum", "tanh"),
                new LayerBuilder.LayerSpec(1, "sum", "tanh")
            });

            var net   = Net.FromDescription(desc);
            var train = net.GetTrainingFunction();

            var weights = new float[net.NumberOfWeights];
            var deltas  = new float[net.NumberOfWeights];

            net.FillWeights(weights);

            var loss = 0f;

            for (var i = 0; i < 15001; i++)
            {
                loss = 0;
                foreach (var test in tests)
                {
                    loss += train(test[0], test[1], weights, deltas);
                    for (var j = 0; j < weights.Length; j++)
                    {
                        weights[j] -= deltas[j] / 2f;
                    }
                }
                if (i % 300 == 0)
                {
                    Console.WriteLine($"{i}, {loss}");
                }
            }
            loss = 0;
            foreach (var test in tests)
            {
                loss += train(test[0], test[1], weights, deltas);
            }
            Console.WriteLine(loss);

            Console.WriteLine(string.Join(", ", weights));
            net.ReadWeights(weights);

            var newWeights = new float[net.NumberOfWeights];

            net.FillWeights(newWeights);
            Console.WriteLine(string.Join(", ", newWeights));

            for (var i = 0; i < weights.Length; i++)
            {
                Assert.AreEqual(weights[i], newWeights[i]);
            }

            var newLoss = 0f;

            foreach (var test in tests)
            {
                newLoss += train(test[0], test[1], newWeights, new float[net.NumberOfWeights]);
            }

            Console.WriteLine(newLoss);
            Assert.AreEqual(loss, newLoss);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var events = ReadEventFile("trainingEvents.csv");
            var ins    = events
                         .Where(e => (e.Open - e.NextLow) / e.Open > .1f)
                         .Select(evts => evts.GetInputArray());

            Console.WriteLine($"Qualified Events: {ins.Count()}");
            var unsupervisedTests = ins.Select(i => Tuple.Create(i, i));
            var supervisedTests   = events.Select(evt => Tuple.Create(evt.GetInputArray(), evt.GetOutputArray()));

            var builder     = new LayerBuilder();
            var description = builder.BuildDescription(5, new[]
            {
                new LayerBuilder.LayerSpec(10, "sum", "softplus"),
                new LayerBuilder.LayerSpec(10, "sum", "softplus"),
                new LayerBuilder.LayerSpec(10, "sum", "tanh"),
                new LayerBuilder.LayerSpec(10, "sum", "softplus"),
                new LayerBuilder.LayerSpec(10, "sum", "softplus"),
                new LayerBuilder.LayerSpec(5, "sum", null)
            });
            //var description = builder.BuildDescription(5, new[]
            //{
            //    new LayerBuilder.LayerSpec(5, "sum", "softplus"),
            //    new LayerBuilder.LayerSpec(6, "sum", "tanh"),
            //    new LayerBuilder.LayerSpec(4, "sum", "softplus"),
            //    new LayerBuilder.LayerSpec(5, "sum", null)
            //});
            var net     = Net.FromDescription(description);
            var trainer = new SimpleTrainer();

            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 5e-6f,
                maxEpochs: 200,
                learningRate: 0.75f);
            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 5e-6f,
                maxEpochs: 9500,
                learningRate: 0.5f);
            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 200,
                learningRate: .25f);
            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 200,
                learningRate: .125f);
            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 200,
                learningRate: .0625f);


            for (var i = 0; i < 5; i++)
            {
                var nextDescription = net.Description;
                var firstSigmoidId  = nextDescription.Nodes.First(n => n.Processor == "tanh").NodeId;
                nextDescription.Nodes = nextDescription.Nodes.Where(n => n.NodeId != firstSigmoidId).ToArray();
                foreach (var node in nextDescription.Nodes)
                {
                    node.Inputs = node.Inputs
                                  .Where(inp => inp.FromInputVector || inp.InputId != firstSigmoidId)
                                  .ToArray();
                }
                net = Net.FromDescription(nextDescription);

                Console.WriteLine($"Removed {i + 1} sigmoids");

                trainer.Train(
                    net: net,
                    tests: unsupervisedTests,
                    desiredError: 1e-6f,
                    maxEpochs: 400,
                    learningRate: 0.5f);
            }

            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 1e-6f,
                maxEpochs: 500,
                learningRate: 0.5f);
            trainer.Train(
                net: net,
                tests: unsupervisedTests,
                desiredError: 1e-6f,
                maxEpochs: 400,
                learningRate: 0.25f);

            var finalDescription = net.Description;
            var outsRemoved      = new[] { finalDescription.Outputs[1], finalDescription.Outputs[2], finalDescription.Outputs[3], finalDescription.Outputs[4] };

            finalDescription.Nodes = finalDescription.Nodes
                                     .Where(n => !outsRemoved.Contains(n.NodeId))
                                     .ToArray();
            finalDescription.Outputs = new[] { finalDescription.Outputs[0] };
            var nextNet = Net.FromDescription(finalDescription);


            trainer.Train(
                net: nextNet,
                tests: supervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 20000,
                learningRate: .25f);
            trainer.Train(
                net: nextNet,
                tests: supervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 20000,
                learningRate: .125f);
            trainer.Train(
                net: nextNet,
                tests: supervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 2000,
                learningRate: .0625f);
            trainer.Train(
                net: nextNet,
                tests: supervisedTests,
                desiredError: 1e-8f,
                maxEpochs: 2000,
                learningRate: .03125f);
            var final     = net.Description;
            var finalText = JsonConvert.SerializeObject(final);

            using (var writer = File.CreateText("out3.json"))
            {
                writer.Write(finalText);
            }
            Console.WriteLine();

            Console.ReadLine();
        }