Esempio n. 1
0
        public void TrainingTwiceStartsWhereFirstCycleEnded()
        {
            var tests = new[]
            {
                Tuple.Create(new[] { 1f, 0f, 0f }, new[] { 1f }),
                Tuple.Create(new[] { 0f, 0f, 0f }, new[] { 0f }),
                Tuple.Create(new[] { 1f, 1f, 0f }, new[] { 1f }),
                Tuple.Create(new[] { 1f, 0f, 1f }, new[] { 1f }),
                Tuple.Create(new[] { 1f, 1f, 1f }, new[] { 1f }),
                Tuple.Create(new[] { 0f, 1f, 0f }, new[] { 0f }),
                Tuple.Create(new[] { 0f, 1f, 1f }, new[] { 0f }),
                Tuple.Create(new[] { 0, 0f, 1f }, new[] { 0f })
            };

            var description = SimpleDescriptionBuilder.GetDescription(3, new[] { 3, 3, 1 });
            var net         = Net.FromDescription(description);

            WeightFiller.FillWeights(net, 0.001f);

            var source = new CancellationTokenSource();
            var token  = source.Token;

            var trainer = new Trainer(tests, net);

            var firstError = trainer.Train(
                learnFactor: 0.5f,
                inertia: 0.2f,
                desiredError: 0.0001f,
                maxRuns: 50,
                progress: j => { },
                cancel: token);

            var secondError = float.MaxValue;

            trainer.Train(
                learnFactor: 0.5f,
                inertia: 0.2f,
                desiredError: 0.0001f,
                maxRuns: 2,
                progress: j => { secondError = j.AvgError; },
                cancel: token);

            Assert.IsTrue(secondError < firstError);
        }
Esempio n. 2
0
        public Guid Post(StocksTrainingJobRequest request)
        {
            // build description
            request.HiddenLayerNodeCounts = request.HiddenLayerNodeCounts ?? new List <int>();
            request.HiddenLayerNodeCounts.Add(1);
            var description = SimpleDescriptionBuilder.GetDescription(4, request.HiddenLayerNodeCounts.ToArray());

            foreach (var id in description.Outputs)
            {
                var outNode = description.Nodes.Single(n => n.NodeId == id);
                outNode.Processor = null;
            }

            // get training events
            var tests = _events.TrainingEvents
                        .Select(evt => Tuple.Create(evt.GetInputArray(), evt.GetOutputArray()));

            // get net
            var net = Net.FromDescription(description);

            // initialize weights
            WeightFiller.FillWeights(net, request.WeightVariance);

            // create a trainer
            var trainer = new Trainer(tests, net);

            // add to the repository and return the id
            return(_trainingJobRepository
                   .CreateProcess((progress, token) =>
            {
                trainer.Train(
                    learnFactor: request.InitialLearningRate,
                    inertia: request.InitialMomentum,
                    desiredError: request.DesiredError,
                    maxRuns: request.MaxIterations,
                    progress: progress,
                    cancel: token);
            }));
        }
Esempio n. 3
0
        public void TurtleZebraGiraffe()
        {
            var tests = new[]
            {
                Tuple.Create(new[] { 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f }, new[] { 0f, 0f, 1f }),
                Tuple.Create(new[] { 1f, -1f, -1f, -1f, -1f, -1f, -1f, -1f, -1f, -1f }, new[] { 1f, 0f, 0f }),
                Tuple.Create(new[] { 1f, 1f, 1f, 1f, -1f, -1f, -1f, -1f, -1f, -1f }, new[] { 0f, 1f, 0f })
            };
            var description = SimpleDescriptionBuilder.GetDescription(10, new[] { 5, 5, 5, 3 });

            foreach (var node in description.Nodes.Where(node => description.Outputs.Contains(node.NodeId)))
            {
                node.Processor = "sigmoid";
            }
            var net = Net.FromDescription(description);

            WeightFiller.FillWeights(net, .005f);

            var trainer = new SimpleTrainer();
            var error   = trainer.Train(
                net: net,
                tests: tests,
                desiredError: 0.01f,
                maxEpochs: 20000,
                learningRate: .5f);

            Assert.IsTrue(error < .01f);

            var eval = net.GetEvaluationFunction();

            foreach (var test in tests)
            {
                var output = eval(test.Item1);
                Console.WriteLine(string.Join(",", output));
            }
        }
Esempio n. 4
0
        public void TanhNetCanBeTrainedOnXOr()
        {
            var tests = new[]
            {
                Tuple.Create(new[] { 1f, -1f }, new[] { 1f }),
                Tuple.Create(new[] { 1f, 1f }, new[] { -1f }),
                Tuple.Create(new[] { -1f, 1f }, new[] { 1f }),
                Tuple.Create(new[] { -1f, -1f }, new[] { -1f })
            };

            var initialDescription = new NetDescription
            {
                Nodes = new[]
                {
                    new NodeDescription
                    {
                        NodeId = 0, Aggregator = "sum", Processor = "tanh",
                        Weight = .21f, Inputs = new []
                        {
                            new NodeInputDescription
                            {
                                FromInputVector = true, InputId = 0, Weight = -.07f
                            },
                            new NodeInputDescription
                            {
                                FromInputVector = true, InputId = 1, Weight = -.28f
                            }
                        }
                    },
                    new NodeDescription
                    {
                        NodeId = 1, Aggregator = "sum", Processor = "tanh",
                        Weight = -.29f, Inputs = new []
                        {
                            new NodeInputDescription
                            {
                                FromInputVector = true, InputId = 0, Weight = .41f
                            },
                            new NodeInputDescription
                            {
                                FromInputVector = true, InputId = 1, Weight = -.05f
                            }
                        }
                    },
                    new NodeDescription
                    {
                        NodeId = 2, Aggregator = "sum", Processor = "tanh",
                        Weight = .11f, Inputs = new []
                        {
                            new NodeInputDescription
                            {
                                FromInputVector = false, InputId = 0, Weight = -.1f
                            },
                            new NodeInputDescription
                            {
                                FromInputVector = false, InputId = 1, Weight = -.21f
                            }
                        }
                    }
                },
                Outputs = new[] { 2 }
            };

            var net = Net.FromDescription(initialDescription);

            WeightFiller.FillWeights(net, .05f);
            var trainer = new SimpleTrainer();

            var error = trainer.Train(
                net: net,
                tests: tests,
                desiredError: .001f,
                maxEpochs: 100000,
                learningRate: 5f);

            Console.WriteLine(error);
            Assert.IsTrue(error < 0.1f);
        }
Esempio n. 5
0
        public void FullTrainToDescAndBack()
        {
            var tests = new[]
            {
                Tuple.Create(new[] { 1f, 0f, 0f }, new[] { 1f }),
                Tuple.Create(new[] { 0f, 0f, 0f }, new[] { 0f }),
                Tuple.Create(new[] { 1f, 1f, 0f }, new[] { 1f }),
                Tuple.Create(new[] { 1f, 0f, 1f }, new[] { 1f }),
                Tuple.Create(new[] { 1f, 1f, 1f }, new[] { 1f }),
                Tuple.Create(new[] { 0f, 1f, 0f }, new[] { 0f }),
                Tuple.Create(new[] { 0f, 1f, 1f }, new[] { 0f }),
                Tuple.Create(new[] { 0, 0f, 1f }, new[] { 0f })
            };

            var description = SimpleDescriptionBuilder.GetDescription(3, new[] { 3, 1 });
            var net         = Net.FromDescription(description);

            WeightFiller.FillWeights(net, 0.001f);

            var source = new CancellationTokenSource();
            var token  = source.Token;

            var trainer = new Trainer(tests, net);

            var firstError = trainer.Train(
                learnFactor: 0.5f,
                inertia: 0.2f,
                desiredError: 0.0001f,
                maxRuns: 50,
                progress: j => { },
                cancel: token);

            var desc2 = net.Description;
            var net2  = Net.FromDescription(desc2);

            var eval1 = net.GetEvaluationFunction();
            var eval2 = net2.GetEvaluationFunction();

            var avgError1 = 0d;
            var avgError2 = 0d;

            foreach (var test in tests)
            {
                var result1 = eval1(test.Item1);
                var result2 = eval2(test.Item1);
                avgError1 += Math.Pow(result1[0] - test.Item2[0], 2);
                avgError2 += Math.Pow(result2[0] - test.Item2[0], 2);
            }
            avgError2 /= tests.Length;

            Assert.IsTrue(avgError2 <= firstError);

            Func <float[], float[]> a = ((float[] inputs) =>
            {
                var in0 = inputs[0];
                var in1 = inputs[1];
                var in2 = inputs[2];
                var agg0 = (in0 * -2.458647) + (in1 * -0.07651551) + (in2 * -0.0518001) + -0.3729805;
                var out0 = Math.Log(1 + Math.Exp(agg0));
                var agg1 = (in0 * -2.523692) + (in1 * -0.05195593) + (in2 * -0.03320933) + -0.3351826;
                var out1 = Math.Log(1 + Math.Exp(agg1));
                var agg2 = (in0 * -2.458876) + (in1 * -0.07796045) + (in2 * -0.05187172) + -0.3720873;
                var out2 = Math.Log(1 + Math.Exp(agg2));
                var agg3 = (out0 * -0.8511373) + (out1 * -0.9345574) + (out2 * -0.8509701) + 4.971978;
                var out3 = Math.Log(1 + Math.Exp(agg3));
                return(new float[] { (float)out3 });
            });
        }