コード例 #1
0
        private static void TrainNetwork(FeedForwardNeuralNet net)
        {
            List <double[]> trainingInput    = new List <double[]>();
            List <double[]> trainingOutput   = new List <double[]>();
            List <double[]> validationInput  = new List <double[]>();
            List <double[]> validationOutput = new List <double[]>();

            int i = 0;

            foreach (var image in MnistReader.ReadTrainingData())
            {
                double[] input  = Encode(image.Data);
                double[] output = Encode(image.Label);

                if (i < 50000)
                {
                    trainingInput.Add(input);
                    trainingOutput.Add(output);
                }
                else
                {
                    validationInput.Add(input);
                    validationOutput.Add(output);
                }

                i++;
            }

            Console.WriteLine("Started Training:");
            net.Train(trainingInput, trainingOutput, 30, 10, 5.0, validationInput, validationOutput);
            Console.WriteLine("Finished training.");
        }
コード例 #2
0
        private static void TrainAgainstMNISTData()
        {
            //We'll be using MNIST data to train our network to recognize digits from 0 to 9.

            //We know that MNIST Training data has 60,000 images.
            //We will be using the first 50,000 for training and the other 10,000 for the validation through our training.

            //First let's create our Neural Network.
            FeedForwardNeuralNet net = new FeedForwardNeuralNet(784, 100, 10, 0.1);

            TrainNetwork(net);
            TestNetwork(net);

            bool isFileSaved = net.SaveFile("mnist.txt");

            if (isFileSaved)
            {
                Console.WriteLine("mnist.txt file has been successfully saved!");
            }

            Console.ReadKey();
        }
コード例 #3
0
        private static void TestNetwork(FeedForwardNeuralNet net)
        {
            Console.WriteLine("Started Testing");
            int testedImages = 0;
            int rightGuesses = 0;

            foreach (var image in MnistReader.ReadTestData())
            {
                double[] input  = Encode(image.Data);
                double[] output = net.Run(input);

                int result = output.ToList().IndexOf(output.Max());

                if (result == image.Label)
                {
                    rightGuesses++;
                }

                testedImages++;
            }

            Console.WriteLine($"{testedImages} has been tested. {rightGuesses} were correctly classified.");
        }
コード例 #4
0
        public static void Main(string[] args)
        {
            const double high = .99;
            const double low  = .1;

            var doubleTrainingSet = new DoubleTrainingSet
            {
                InputSet = new[]
                {
                    new[] { high, high },
                    new[] { low, high },
                    new[] { high, low },
                    new[] { low, low }
                },
                OutputSet = new[]
                {
                    new[] { low },
                    new[] { high },
                    new[] { high },
                    new[] { low }
                },
                Tollerance       = 0.0000000000001,
                IterationsPerRun = 5
            };

            var configuration = new NeuralNetConfiguration
            {
                HiddenLayerNodes = new Dictionary <int, int>
                {
                    { 1, 6 },
                    { 2, 6 }
                },
                LearningSpeed         = 3,
                NrOfInputNeurons      = doubleTrainingSet.InputSet.Max(d => d.Length),
                NumberOfOutputNeurons = doubleTrainingSet.OutputSet.Max(d => d.Length)
            };

            var net = new FeedForwardNeuralNet(configuration);

            net.Initialize(1);

            var count = 0;

            while (!doubleTrainingSet.Finished)
            {
                count++;

                net.Train(doubleTrainingSet);

                Console.SetCursorPosition(0, 1);
                foreach (var output in doubleTrainingSet.CalculatedOutputSet)
                {
                    var sb = new StringBuilder();
                    foreach (var d in output)
                    {
                        sb.Append(Math.Round(d, 14) + " ");
                    }
                    Console.WriteLine(sb.ToString());
                }
            }

            Console.WriteLine($"{count * doubleTrainingSet.IterationsPerRun} iterations required for training");

            Console.ReadKey();
        }
コード例 #5
0
        public FeedForwardNeuralNet DeserializeData()
        {
            JObject json = JObject.Parse(_data);

            JToken networkObject     = json["Network"];
            JToken inputLayerObject  = networkObject[0];
            JToken hiddenLayerObject = networkObject[1];
            JToken outputLayerObject = networkObject[2];

            /***********************input layer*********************************/
            int count = inputLayerObject[0].Count() / 4;

            double[]        biases        = new double[count];
            double[]        deltas        = new double[count];
            double[]        values        = new double[count];
            List <double[]> dendritesList = new List <double[]>();

            for (int i = 0; i < count; i++)
            {
                biases[i] = (double)inputLayerObject[0][i * 4];
                deltas[i] = (double)inputLayerObject[0][i * 4 + 1];
                values[i] = (double)inputLayerObject[0][i * 4 + 2];
                dendritesList.Add(new double[0]);
            }

            Layer inputLayer = new Layer(biases, deltas, values, dendritesList);

            /***********************input layer*********************************/

            /***********************hidden layer*********************************/
            count         = hiddenLayerObject[0].Count() / 4;
            biases        = new double[count];
            deltas        = new double[count];
            values        = new double[count];
            dendritesList = new List <double[]>();

            for (int i = 0; i < count; i++)
            {
                biases[i] = (double)hiddenLayerObject[0][i * 4];
                deltas[i] = (double)hiddenLayerObject[0][i * 4 + 1];
                values[i] = (double)hiddenLayerObject[0][i * 4 + 2];
                JToken   dendrites      = hiddenLayerObject[0][i * 4 + 3];
                double[] dendritesArray = new double[dendrites.Count()];
                for (int j = 0; j < dendrites.Count(); j++)
                {
                    dendritesArray[j] = (double)dendrites[j];
                }
                dendritesList.Add(dendritesArray);
            }

            Layer hiddenLayer = new Layer(biases, deltas, values, dendritesList);

            /***********************hidden layer*********************************/

            /***********************output layer*********************************/
            count         = outputLayerObject[0].Count() / 4;
            biases        = new double[count];
            deltas        = new double[count];
            values        = new double[count];
            dendritesList = new List <double[]>();

            for (int i = 0; i < count; i++)
            {
                biases[i] = (double)outputLayerObject[0][i * 4];
                deltas[i] = (double)outputLayerObject[0][i * 4 + 1];
                values[i] = (double)outputLayerObject[0][i * 4 + 2];
                JToken   dendrites      = outputLayerObject[0][i * 4 + 3];
                double[] dendritesArray = new double[dendrites.Count()];
                for (int j = 0; j < dendrites.Count(); j++)
                {
                    dendritesArray[j] = (double)dendrites[j];
                }
                dendritesList.Add(dendritesArray);
            }

            Layer outputLayer = new Layer(biases, deltas, values, dendritesList);
            /***********************output layer*********************************/

            /***********************Learning Rate*******************************/
            double learningRate = (double)json["LearningRate"];
            /***********************Learning Rate*******************************/

            FeedForwardNeuralNet net = new FeedForwardNeuralNet(inputLayer, hiddenLayer, outputLayer, learningRate);

            return(net);
        }
コード例 #6
0
 public FeedForwardNeuralNetworkJsonConvertor(FeedForwardNeuralNet net)
 {
     _net = net;
 }