Encog.Neural.Networks.BasicNetwork.GetActivation C# (CSharp) Method

GetActivation() public method

Get the activation function for the specified layer.
public GetActivation ( int layer ) : IActivationFunction
layer int The layer.
return IActivationFunction
        public IActivationFunction GetActivation(int layer)
        {
            _structure.RequireFlat();
            int layerNumber = LayerCount - layer - 1;
            return _structure.Flat.ActivationFunctions[layerNumber];
        }

Usage Example

        /// <summary>
        ///   Measure the performance of the network
        /// </summary>
        /// <param name = "network">Network to analyze</param>
        /// <param name = "dataset">Dataset with input and ideal data</param>
        /// <returns>Error % of correct bits, returned by the network.</returns>
        public static double MeasurePerformance(BasicNetwork network, BasicNeuralDataSet dataset)
        {
            int correctBits = 0;
            float threshold = 0.0f;
            IActivationFunction activationFunction = network.GetActivation(network.LayerCount - 1); //get the activation function of the output layer
            if (activationFunction is ActivationSigmoid)
            {
                threshold = 0.5f; /* > 0.5, range of sigmoid [0..1]*/
            }
            else if (activationFunction is ActivationTANH)
            {
                threshold = 0.0f; /*> 0, range of bipolar sigmoid is [-1..1]*/
            }
            else
                throw new ArgumentException("Bad activation function");
            int n = (int) dataset.Count;

            Parallel.For(0, n, (i) =>
                               {
                                   IMLData actualOutputs = network.Compute(dataset.Data[i].Input);
                                   lock (LockObject)
                                   {
                                       for (int j = 0, k = actualOutputs.Count; j < k; j++)
                                           if ((actualOutputs[j] > threshold && dataset.Data[i].Ideal[j] > threshold)
                                               || (actualOutputs[j] < threshold && dataset.Data[i].Ideal[j] < threshold))
                                               correctBits++;
                                   }
                               });

            long totalOutputBitsCount = dataset.Count*dataset.Data[0].Ideal.Count;

            return (double) correctBits/totalOutputBitsCount;
        }
All Usage Examples Of Encog.Neural.Networks.BasicNetwork::GetActivation