// $Id$ // // File: nnann_function.h // Created: Tue Mar 22 23:07:10 EDT 2011 // Creator: davidl (on Darwin Amelia.local 9.8.0 i386) // #ifndef _nnann_function_ #define _nnann_function_ /// The nnann_function represents an activation function. It really provides an interface /// so different activation functions can be provided allowing the other nnann classes /// to use them without having to know them all a priori. The interface mainly defines /// two virtual methods. The "f(x)" method is the activation function itself while the /// "dfdx(x)" method is the first derivative w.r.t. x. If the "dfdx(x)" method is not /// implemented in a base class, a numerical derivative function is provided. class nnann_function{ public: nnann_function(){}; virtual ~nnann_function(){}; virtual std::string GetName(void)=0; virtual double f(double x)=0; ///< Activation function /// Derivative of activation function. /// If subclass doesn't implement dfdx, then a simple numerical /// derivative is done. This may not be very accurate or efficient. virtual double dfdx(double x){ double dx = 0.01; double dx_over_2 = dx/2.0; double df = f(x+dx_over_2) - f(x-dx_over_2); return df/dx; } /// Calculate error on input based on the input (x) and the target output. /// The default routine is just to (target-f(x))/dfdx(x). virtual double InputError(double x, double target){ double df_dx = dfdx(x); if(df_dx==0.0)df_dx=1.0E-6; return (target-f(x))/df_dx; } protected: }; #endif // _nnann_function_