示例#1
0
        static void Main(string[] args)
        {
            var mlp = new VectorizedMatrixMLP();

            mlp.InputValue = new double[, ] {
                { 1, 0 },
                { 0, 0 },
                { 0, 1 },
                { 1, 1 }
            };
            mlp.TargetValue = new double[, ] {
                { 1 },
                { 0 },
                { 1 },
                { 0 }
            };

            mlp.activFct     = new ActivationFunction.SigmoidFunction();
            mlp.nbIterations = 10000; // Sigmoid: works

            //mlp.activFct = new ActivationFunction.HyperbolicTangentFunction();
            //mlp.nbIterations = 5000; // Hyperbolic tangent: works

            //mlp.activFct = new ActivationFunction.ELUFunction();
            //mlp.nbIterations = 1000; // ELU: works fine

            //mlp.activFct = new ActivationFunction.GaussianFunction();
            //mlp.nbIterations = 1000; // Gaussian: works fine

            //mlp.activFct = new ActivationFunction.SinusFunction();
            //mlp.nbIterations = 500; // Sinus: works fine

            //mlp.activFct = new ActivationFunction.ArcTangentFunction();
            //mlp.nbIterations = 1000; // ArcTangent: works fine

            //mlp.activFct = new ActivationFunction.ReluFunction();
            //mlp.nbIterations = 100000; // ReLU: Does not work yet, but this next one yes:

            //mlp.activFct = new ActivationFunction.ReLUSigmoidFunction();
            //mlp.nbIterations = 5000; // ReLUSigmoid: works fine

            mlp.lambdaFct           = (x) => mlp.activFct.Activation(x, gain: 1, center: 0);
            mlp.lambdaFctD          = (x) => mlp.activFct.Derivative(x, gain: 1, center: 0);
            mlp.activFctIsNonLinear = mlp.activFct.IsNonLinear();

            //int[] NeuronCount = new int[] { 2, 3, 3, 1 };
            int[] NeuronCount = new int[] { 2, 2, 1 };
            mlp.LearningRate = 0.1;
            mlp.InitStruct(NeuronCount, addBiasColumn: true);

            mlp.Randomize();

            mlp.Train();

            Console.WriteLine("Press a key to quit.");
            Console.Read();
        }
        public void VectorizedMatrixMLPXORSigmoid()
        {
            m_mlp.nbIterations        = 30000; // Sigmoid: works
            m_mlp.activFct            = new ActivationFunction.SigmoidFunction();
            m_mlp.lambdaFct           = x => m_mlp.activFct.Activation(x, gain: 1, center: 0);
            m_mlp.lambdaFctD          = x => m_mlp.activFct.Derivative(x, gain: 1, center: 0);
            m_mlp.activFctIsNonLinear = m_mlp.activFct.IsNonLinear();

            m_mlp.InitStruct(NeuronCount, addBiasColumn: true);

            m_mlp.W[0] = new double[, ] {
                { -0.5, -0.78, -0.07 },
                { 0.54, 0.32, -0.13 },
                { -0.29, 0.89, -0.8 }
            };
            m_mlp.W[1] = new double[, ] {
                { 0.28 },
                { -0.94 },
                { -0.5 },
                { -0.36 }
            };

            m_mlp.Train();

            var expectedOutput = new double[, ] {
                { 0.97 },
                { 0.02 },
                { 0.97 },
                { 0.02 }
            };

            var    output               = m_mlp.output.ToString();
            Matrix expectedMatrix       = expectedOutput; // double(,) -> Matrix
            var    expectedOutputString = expectedMatrix.ToString();

            Assert.AreEqual(output, expectedOutputString);

            var expectedLoss = 0.1;

            float[] targetArray = m_mlp.TargetValue.ToArray();
            var     loss        = m_mlp.ComputeAverageError(targetArray);
            var     lossRounded = Math.Round(loss, 2);

            Assert.AreEqual(expectedLoss, lossRounded);
        }