Пример #1
0
        static void Step5()
        {
            Console.WriteLine("STEP 5: Train neural network...");
            NetworkTrainer networkteTrainer = new NetworkTrainer();

            networkteTrainer.Train(DataFilesInfoGetter.NetworkFile, DataFilesInfoGetter.NormalizedTrainingFile);
        }
Пример #2
0
        static void Main(string[] args)
        {
            //IList<Tuple<double[], double[]>> trainset = new List<Tuple<double[], double[]>>
            //{
            //    Tuple.Create<double[], double[]>(new double[]{ 97, 98, 97, 98, 97, 98, 97, 98 }, new double[]{ 1.0 / 97, 1.0 / 98 }),
            //    Tuple.Create<double[], double[]>(new double[]{ 98, 98, 98, 98, 98, 98, 98, 98 }, new double[]{ 1.0 / 98, 1.0 / 98 }),
            //    Tuple.Create<double[], double[]>(new double[]{ 95, 101, 95, 101, 95, 101, 95, 101 }, new double[]{ 1.0 / 95, 1.0 / 101 }),
            //    Tuple.Create<double[], double[]>(new double[]{ 120, 121, 120, 121, 120, 121, 120, 121 }, new double[]{ 1.0 / 120, 1.0 / 121 })
            //};

            IList <Tuple <double[], double[]> > trainset = new List <Tuple <double[], double[]> >
            {
                Tuple.Create <double[], double[]>(new double[] { 1, 0, 1, 0, 1, 0, 1, 0 }, new double[] { 1, 0 }),
                Tuple.Create <double[], double[]>(new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }, new double[] { 0, 1 }),
                Tuple.Create <double[], double[]>(new double[] { 0, 0, 0, 0, 0, 0, 0, 0 }, new double[] { 0, 0 }),
                Tuple.Create <double[], double[]>(new double[] { 1, 1, 1, 1, 1, 1, 1, 1 }, new double[] { 1, 1 })
            };

            NeuralNetwork net = NeuralNetwork.GetInstance(inputs: null);

            NetworkTrainer.Train(net /*, trainset*/);
            NetworkTrainer.Test(net, trainset);

            Console.ReadKey();
        }
Пример #3
0
        private static void Main(string[] args)
        {
            var net     = new Network();
            var trainer = new NetworkTrainer();
            var tester  = new NetworkTester();

            trainer.Train(net);
            tester.Test(net);

            Console.ReadKey();
        }
Пример #4
0
        //static void FuckWithWeights(Network network)
        //{
        //    network.HiddenLayers[0].Neurons[0].Bias = -1.74976547;
        //    network.HiddenLayers[0].Neurons[0].UpstreamDendrites[0].Weight = 0.22117967;
        //    network.HiddenLayers[0].Neurons[0].UpstreamDendrites[1].Weight = -1.07004333;

        //    network.HiddenLayers[0].Neurons[1].Bias = 0.3426804;
        //    network.HiddenLayers[0].Neurons[1].UpstreamDendrites[0].Weight = -0.18949583;
        //    network.HiddenLayers[0].Neurons[1].UpstreamDendrites[1].Weight = 0.25500144;

        //    network.HiddenLayers[0].Neurons[2].Bias = 1.1530358;
        //    network.HiddenLayers[0].Neurons[2].UpstreamDendrites[0].Weight = -0.45802699;
        //    network.HiddenLayers[0].Neurons[2].UpstreamDendrites[1].Weight = 0.43516349;

        //    network.HiddenLayers[0].Neurons[3].Bias = -0.25243604;
        //    network.HiddenLayers[0].Neurons[3].UpstreamDendrites[0].Weight = -0.58359505;
        //    network.HiddenLayers[0].Neurons[3].UpstreamDendrites[1].Weight = 0.81684707;

        //    network.HiddenLayers[0].Neurons[4].Bias = 0.98132079;
        //    network.HiddenLayers[0].Neurons[4].UpstreamDendrites[0].Weight = 0.67272081;
        //    network.HiddenLayers[0].Neurons[4].UpstreamDendrites[1].Weight = -0.10441114;

        //    network.OutputLayer.Neurons[0].Bias = 0.51421884;
        //    network.OutputLayer.Neurons[0].UpstreamDendrites[0].Weight = -0.53128038;
        //    network.OutputLayer.Neurons[0].UpstreamDendrites[1].Weight = 1.02973269;
        //    network.OutputLayer.Neurons[0].UpstreamDendrites[2].Weight = -0.43813562;
        //    network.OutputLayer.Neurons[0].UpstreamDendrites[3].Weight = -1.11831825;
        //    network.OutputLayer.Neurons[0].UpstreamDendrites[4].Weight = 1.61898166;
        //}

        static void Main(string[] args)
        {
            // What I cannot create, I do not understand.
            // ~Richard P. Feynman


            ITrainingDataBuilder trainingDataBuilder = new MNISTTrainingDataBuilder();

            trainingDataBuilder.BuildTrainingData();

            double    totalAccuracy      = 0.0;
            const int NumberOfIterations = 1;

            DateTime start = DateTime.Now;

            for (int c = 0; c < NumberOfIterations; c++)
            {
                Random rand = new Random();

                Network network = Network.BuildNetwork(
                    rand,
                    new Math.CostFunctions.CrossEntropyCostFunction(),
                    new Math.RegularizationFunctions.L2Normalization(.1),
                    Common.WeightIntializerType.RandomGaussianWithNeuronCount,
                    new DropoutLayerOptions(0),
                    784, 10, 30);

                NetworkTrainer networkTrainer = new NetworkTrainer();
                networkTrainer.Train(network,
                                     trainingDataBuilder,
                                     .5, 30, 10,
                                     -1,
                                     OnLearningProgress,
                                     OnValidationDataUpdate);

                totalAccuracy += trainingDataBuilder.GradeResults(network, trainingDataBuilder.TestData) * 100.0;
            }

            Console.WriteLine($"Accurancy: {(totalAccuracy / (NumberOfIterations * 1.0)).ToString("000.00")}% in {(DateTime.Now - start).TotalSeconds} seconds.");

            return;
        }
Пример #5
0
        public void TestTrainXor()
        {
            var pg             = new PositiveUniformParameterGenerator();
            var neuronFactor   = new NeuronFactory(pg);
            var synapseFactory = new SynapseFactory(pg);
            var n = NetworkFactory.CreateMultilayerPerceptron(new[] { 2, 2, 1 }, ActivationFunction.Sigmoid,
                                                              ActivationFunction.Identity, null, neuronFactor, synapseFactory);
            var trainer = new NetworkTrainer(n, 0.9, 0.1);

            var examples = new Matrix(new double[, ] {
                { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }
            });
            var labels = new Vector(0, 1, 1, 0);

            trainer.Train(examples, labels, 1000);

            for (var i = 0; i < labels.Length; i++)
            {
                var x = examples.GetRow(i);
                var y = labels[i];
                Console.WriteLine("Actual: {0}, Result: {1}", y, n.Compute(x));
                Assert.IsTrue(Math.Abs(y - n.Compute(x)[0]) < 0.01);
            }
        }