Accord.Neuro.ActivationFunctions.BernoulliFunction.Derivative2 C# (CSharp) Method

Derivative2() public method

Calculates function derivative.

The method calculates the same derivative value as the Derivative method, but it takes not the input x value itself, but the function value, which was calculated previously with the help of Function method.

Some applications require as function value, as derivative value, so they can save the amount of calculations using this method to calculate derivative.

public Derivative2 ( double y ) : double
y double Function output value - the value, which was obtained /// with the help of method.
return double
        public double Derivative2(double y)
        {
            return (alpha * y * (1 - y));
        }

Usage Example

        public void ConstructorTest()
        {
            // Create a Bernoulli function with sigmoid's alpha = 1
            BernoulliFunction function = new BernoulliFunction();

            // Computes the function output (sigmoid function)
            double y = function.Function(x: 0.4); // 0.5986876

            // Draws a sample from a Bernoulli distribution with
            // mean given by the function output y (given as before)
            double z = function.Generate(x: 0.4); // (random, 0 or 1)

            // Here, z can be either 0 or 1. Since it follows a Bernoulli
            // distribution with mean 0.59, it is expected to be 1 about 
            // 0.59 of the time.

            // Now, please note that the above is completely equivalent 
            // to computing the line below (remember, 0.5986876 == y)
            double w = function.Generate2(y: 0.5986876); // (random, 0 or 1)


            // We can also compute the derivative of the sigmoid function
            double d = function.Derivative(x: 0.4); // 0.240260

            // Or compute the derivative given the functions' output y
            double e = function.Derivative2(y: 0.5986876); // 0.240260

            Assert.AreEqual(0.5986876, y, 1e-7);
            Assert.AreEqual(0.2402607, d, 1e-7);
            Assert.AreEqual(0.2402607, e, 1e-7);
        }