private static void SingleInput_WeightOne_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            // Activate and test.
            net.InputVector[0] = 0.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.Equal(0.5, net.OutputVector[0]);
            }

            // Activate and test.
            net.InputVector[0] = 1.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.Equal(actFn.Fn(1), net.OutputVector[0]);
            }

            // Activate and test.
            net.InputVector[0] = 10.0;
            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                Assert.Equal(actFn.Fn(10), net.OutputVector[0]);
            }
        }
Beispiel #2
0
        private static void TestOverloads(IActivationFunction <double> actFn, Random rng)
        {
            // Init an array of random values.
            double[] v = GetRandomArray(1001, rng);

            // Init empty target arrays.
            double[] v_scalar = new double[v.Length];
            double[] v_vector = new double[v.Length];
            double[] v_zero   = new double[v.Length];

            // Apply the scalar overload (single input) of the activation function.
            ApplyFunc(actFn.Fn, v, v_scalar);

            // Apply vector based overloads.
            // Overload 1.
            Array.Copy(v, v_vector, v.Length);
            actFn.Fn(v_vector);
            Compare(v_scalar, v_vector);

            // Overload 2.
            Array.Copy(v, v_vector, v.Length);
            actFn.Fn(v_vector, 10, 20);
            Compare(v, v_vector, 0, 10);
            Compare(v_scalar, v_vector, 10, 20);
            Compare(v, v_vector, 20, v.Length);

            // Overload 3.
            Array.Clear(v_vector, 0, v_vector.Length);
            actFn.Fn(v, v_vector, 10, 20);
            Compare(v_zero, v_vector, 0, 10);
            Compare(v_scalar, v_vector, 10, 20);
            Compare(v_zero, v_vector, 20, v.Length);
        }
        private static void TwoInputs_WeightHalf_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            double x;

            // Activate and test.
            net.InputVector[0] = 0.0;
            net.InputVector[1] = 0.0;
            net.Activate();
            Assert.Equal(0.5, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.InputVector[1] = 2.0;
            net.Activate();
            x = 1.5; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 10.0;
            net.InputVector[1] = 20.0;
            net.Activate();
            x = 15.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);
        }
    private static void TwoInputs_WeightHalf_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        double x;
        var    inputs  = net.Inputs.Span;
        var    outputs = net.Outputs.Span;

        // Activate and test.
        inputs[0] = 0.0;
        inputs[1] = 0.0;
        net.Activate();
        Assert.Equal(0.5, outputs[0]);

        // Activate and test.
        inputs[0] = 1.0;
        inputs[1] = 2.0;
        net.Activate();
        x = 1.5; actFn.Fn(ref x);
        Assert.Equal(x, outputs[0]);

        // Activate and test.
        inputs[0] = 10.0;
        inputs[1] = 20.0;
        net.Activate();
        x = 15.0; actFn.Fn(ref x);
        Assert.Equal(x, outputs[0]);
    }
 private static void MultipleInputsOutputs_Inner(
     IBlackBox <double> net,
     IActivationFunction <double> actFn)
 {
     // Activate and test.
     net.InputVector[0] = 1.0;
     net.InputVector[1] = 2.0;
     net.InputVector[2] = 3.0;
     net.Activate();
     Assert.AreEqual(actFn.Fn(2.0), net.OutputVector[0]);
     Assert.AreEqual(actFn.Fn(3.0), net.OutputVector[1]);
     Assert.AreEqual(actFn.Fn(1.0), net.OutputVector[2]);
 }
    private static void ComplexCyclic_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        // Simulate network in C# and compare calculated outputs with actual network outputs.
        double[] preArr  = new double[3];
        double[] postArr = new double[3];
        var      inputs  = net.Inputs.Span;
        var      outputs = net.Outputs.Span;

        postArr[0] = 3.0;
        inputs[0]  = 3.0;

        for (int i = 0; i < 10; i++)
        {
            preArr[1] = postArr[0] * -2.0 + postArr[2];
            preArr[2] = postArr[0] + postArr[1];

            actFn.Fn(ref preArr[1], ref postArr[1]);
            actFn.Fn(ref preArr[2], ref postArr[2]);

            net.Activate();

            Assert.Equal(postArr[1], outputs[0]);
        }

        // Rest the network's internal state.
        net.Reset();
        Assert.Equal(0.0, outputs[0]);

        // Run the test again.
        Array.Clear(preArr, 0, preArr.Length);
        Array.Clear(postArr, 0, postArr.Length);

        postArr[0] = 3.0;
        inputs[0]  = 3.0;

        for (int i = 0; i < 10; i++)
        {
            preArr[1] = postArr[0] * -2.0 + postArr[2];
            preArr[2] = postArr[0] + postArr[1];

            actFn.Fn(ref preArr[1], ref postArr[1]);
            actFn.Fn(ref preArr[2], ref postArr[2]);

            net.Activate();

            Assert.Equal(postArr[1], outputs[0]);
        }
    }
Beispiel #7
0
        private static void Fn_Overloads_Inner(IActivationFunction <double> actFn, Random rng)
        {
            // Init an array of random values.
            double[] v = GetRandomArray(1001, rng);

            // Init empty target arrays.
            double[] v_scalar = new double[v.Length];
            double[] v_vector = new double[v.Length];
            double[] v_zero   = new double[v.Length];

            // Apply the scalar overload (single input) of the activation function.
            ApplyFunc(actFn.Fn, v, v_scalar);

            // Apply vector based overloads.
            // Overload 1.
            Array.Copy(v, v_vector, v.Length);
            actFn.Fn(v_vector);
            Assert.Equal(v_scalar, v_vector);

            // Overload 2.
            Array.Copy(v, v_vector, v.Length);
            actFn.Fn(v_vector.AsSpan(10..20));
            ConponentwiseEqual(v, v_vector, 0, 10);
            ConponentwiseEqual(v_scalar, v_vector, 10, 20);
            ConponentwiseEqual(v, v_vector, 20, v.Length);

            // Overload 3.
            Array.Clear(v_vector, 0, v_vector.Length);
            actFn.Fn(v[10..20], v_vector.AsSpan(10..20));
        private static void HiddenNode_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            // Activate and test.
            net.InputVector[0] = 0.0;
            net.InputVector[1] = 0.0;
            net.Activate();
            Assert.AreEqual(actFn.Fn(1.0), net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();
            Assert.AreEqual(actFn.Fn(actFn.Fn(0.375) * 2.0), net.OutputVector[0]);
        }
        private static void Complex_WeightOne_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();

            double output1 = actFn.Fn(actFn.Fn(0.25));

            Assert.AreEqual(output1, net.OutputVector[1]);

            double output0 = actFn.Fn(actFn.Fn(output1 + 0.5 + 0.25) * 0.9);

            Assert.AreEqual(output0, net.OutputVector[0]);
        }
Beispiel #10
0
        private static void AssertMonotonic(IActivationFunction <double> actFn, bool strict)
        {
            Func <double, double> fn = delegate(double x) {
                actFn.Fn(ref x);
                return(x);
            };

            Assert.True(FuncTestUtils.IsMonotonicIncreasing(fn, -6, 6, 0.01, strict));
        }
Beispiel #11
0
        private static void MultipleInputsOutputs_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            double x;

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.InputVector[1] = 2.0;
            net.InputVector[2] = 3.0;
            net.Activate();

            x = 2.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);

            x = 3.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[1]);

            x = 1.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[2]);
        }
        private static void HiddenNode_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            double x;

            // Activate and test.
            net.InputVector[0] = 0.0;
            net.InputVector[1] = 0.0;
            net.Activate();
            x = 1.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();
            x  = 0.375; actFn.Fn(ref x);
            x *= 2; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);
        }
        private static void Complex_WeightOne_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            double x;

            // Activate and test.
            net.InputVector[0] = 0.5;
            net.InputVector[1] = 0.25;
            net.Activate();

            x = 0.25; actFn.Fn(ref x); actFn.Fn(ref x);
            double output1 = x;;

            Assert.Equal(output1, net.OutputVector[1]);

            x  = output1 + 0.5 + 0.25; actFn.Fn(ref x);
            x *= 0.9; actFn.Fn(ref x);
            double output0 = x;

            Assert.Equal(output0, net.OutputVector[0]);
        }
    private static void MultipleInputsOutputs_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        double x;
        var    inputs  = net.Inputs.Span;
        var    outputs = net.Outputs.Span;

        // Activate and test.
        inputs[0] = 1.0;
        inputs[1] = 2.0;
        inputs[2] = 3.0;
        net.Activate();

        x = 2.0; actFn.Fn(ref x);
        Assert.Equal(x, outputs[0]);

        x = 3.0; actFn.Fn(ref x);
        Assert.Equal(x, outputs[1]);

        x = 1.0; actFn.Fn(ref x);
        Assert.Equal(x, outputs[2]);
    }
    private static void HiddenNode_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        double x;
        var    inputs  = net.Inputs.Span;
        var    outputs = net.Outputs.Span;

        // Activate and test.
        inputs[0] = 0.0;
        inputs[1] = 0.0;
        net.Activate();
        x = 1.0; actFn.Fn(ref x);
        Assert.Equal(x, outputs[0]);

        // Activate and test.
        inputs[0] = 0.5;
        inputs[1] = 0.25;
        net.Activate();
        x  = 0.375; actFn.Fn(ref x);
        x *= 2; actFn.Fn(ref x);
        Assert.Equal(x, outputs[0]);
    }
        private static void SingleInput_WeightOne_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            double x;

            // Activate and test.
            net.InputVector[0] = 0.0;
            net.Activate();
            Assert.Equal(0.5, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 1.0;
            net.Activate();
            x = 1.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);

            // Activate and test.
            net.InputVector[0] = 10.0;
            net.Activate();
            x = 10.0; actFn.Fn(ref x);
            Assert.Equal(x, net.OutputVector[0]);
        }
    private static void SingleInput_WeightOne_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        var inputs  = net.Inputs.Span;
        var outputs = net.Outputs.Span;

        // Activate and test.
        inputs[0] = 0.0;
        for (int i = 0; i < 10; i++)
        {
            net.Activate();
            Assert.Equal(0.5, outputs[0]);
        }

        // Activate and test.
        double x;

        x         = 1.0; actFn.Fn(ref x);
        inputs[0] = 1.0;
        for (int i = 0; i < 10; i++)
        {
            net.Activate();

            Assert.Equal(x, outputs[0]);
        }

        // Activate and test.
        x         = 10.0; actFn.Fn(ref x);
        inputs[0] = 10.0;
        for (int i = 0; i < 10; i++)
        {
            net.Activate();
            Assert.Equal(x, outputs[0]);
        }
    }
    private static void Complex_WeightOne_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        double x;
        var    inputs  = net.Inputs.Span;
        var    outputs = net.Outputs.Span;

        // Activate and test.
        inputs[0] = 0.5;
        inputs[1] = 0.25;
        net.Activate();

        x = 0.25; actFn.Fn(ref x); actFn.Fn(ref x);
        double output1 = x;

        Assert.Equal(output1, outputs[1]);

        x  = output1 + 0.5 + 0.25; actFn.Fn(ref x);
        x *= 0.9; actFn.Fn(ref x);
        double output0 = x;

        Assert.Equal(output0, outputs[0]);
    }
        private static void CyclicOutput_Inner(
            IBlackBox <double> net,
            IActivationFunction <double> actFn)
        {
            // Activate and test.
            const double input    = 0.1;
            double       inputVal = input;

            net.InputVector[0] = inputVal;

            for (int i = 0; i < 10; i++)
            {
                net.Activate();
                double outputExpected = actFn.Fn(inputVal);
                Assert.Equal(outputExpected, net.OutputVector[0]);
                inputVal = input + outputExpected;
            }
        }
    private static void CyclicOutput_Inner(
        IBlackBox <double> net,
        IActivationFunction <double> actFn)
    {
        // Activate and test.
        const double input   = 0.1;
        var          inputs  = net.Inputs.Span;
        var          outputs = net.Outputs.Span;

        double inputVal = input;

        inputs[0] = inputVal;

        for (int i = 0; i < 10; i++)
        {
            net.Activate();
            double outputExpected = inputVal; actFn.Fn(ref outputExpected);
            Assert.Equal(outputExpected, outputs[0]);
            inputVal = input + outputExpected;
        }
    }