示例#1
0
        static void Main(string[] args)
        {
            NeuralNetwork net2 = new NeuralNetwork(new int[] { 2, 3, 1 }, new TanhActivationFunction());

            net2.FeedForward(new double[] { 0, 0 });
            Console.WriteLine("Output for input 0, 0: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 0, 1 });
            Console.WriteLine("Output for input 0, 1: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 1, 0 });
            Console.WriteLine("Output for input 1, 0: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 1, 1 });
            Console.WriteLine("Output for input 1, 1: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.Fit(new double[, ]
            {
                { 0, 0 },
                { 0, 1 },
                { 1, 0 },
                { 1, 1 }
            }, new double[, ]
            {
                { 0 },
                { 1 },
                { 1 },
                { 0 }
            }, 60001, 1);
            net2.FeedForward(new double[] { 0, 0 });
            Console.WriteLine("Output for input 0, 0: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 0, 1 });
            Console.WriteLine("Output for input 0, 1: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 1, 0 });
            Console.WriteLine("Output for input 1, 0: " + net2.layers[net2.layers.Length - 1].activations[0]);
            net2.FeedForward(new double[] { 1, 1 });
            Console.WriteLine("Output for input 1, 1: " + net2.layers[net2.layers.Length - 1].activations[0]);
            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            NeuralNetwork NN = new NeuralNetwork(new int[] { 4, 7, 3 }, LearningRate: 0.05, Momentum: 0.001, Decay: 0.0001);

            double[][] entries = getInputs(true);
            double[][] learningData, expectedOutputData, testingData, testingDataOutputData;
            int        testingDataElementsCount = (int)(entries.Length * 0.20);

            Console.WriteLine("Shuffle data and create testing set");
            Shuffle(entries, testingDataElementsCount, out learningData, out expectedOutputData, out testingData, out testingDataOutputData);

            Console.WriteLine("Training");
            NN.Train(learningData, expectedOutputData, MaxEpoches: 2000, MSE: 0.2f);
            Console.WriteLine("Training finished");

            Console.WriteLine("Processing testing cases");
            int         rightAnswers = 0;
            NeuronLayer result;

            for (int i = 0; i < testingData.Length; i++)
            {
                double[] entry = testingData[i];
                result = NN.FeedForward(new double[] { entry[0], entry[1], entry[2], entry[3] }.GetEnumerator());
                double maxVal           = double.MinValue;
                int    maxValIndex      = 0;
                int    rightAnswerIndex = 0;
                for (int j = 0; j < result.Capacity; j++)
                {
                    if (result[j].activation > maxVal)
                    {
                        maxVal      = result[j].activation;
                        maxValIndex = j;
                    }
                    if (testingDataOutputData[i][j] == 1)
                    {
                        rightAnswerIndex = j;
                    }
                }
                if (maxValIndex == rightAnswerIndex)
                {
                    rightAnswers++;
                }
                else
                {
                    for (int j = 0; j < result.Capacity; j++)
                    {
                        Console.Write(String.Format("{0} = {1};", Math.Round(result[j].activation, 2), testingDataOutputData[i][j]));
                    }
                    Console.WriteLine();
                }
            }
            Console.WriteLine(String.Format("Network accuracy {0:p}", 1f * rightAnswers / testingDataElementsCount));
            Console.Read();
        }