cost.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 #ifndef __LEMGA_COST_H__
00003 #define __LEMGA_COST_H__
00004 
00011 #include <cmath>
00012 #include "object.h"
00013 
00014 namespace lemga {
00015 namespace cost {
00016 
00017 template <typename _Num>
00018 struct AdaCost {
00019     virtual ~AdaCost () { /* get rid of GCC 4.0 warnings */ }
00020     virtual _Num cost (const _Num& F, const _Num& y) const {
00021         assert(std::fabs(y*y-1) < INFINITESIMAL);
00022         return std::exp(-(F * y));
00023     }
00024     inline _Num operator() (const _Num& F, const _Num& y) const
00025     { return cost(F, y); }
00026 
00027     virtual _Num deriv1 (const _Num& F, const _Num& y) const {
00028         assert(std::fabs(y*y-1) < INFINITESIMAL);
00029         return -std::exp(- (F * y)) * y;
00030     }
00031 };
00032 
00033 /* A temporary workaround: Using real class instead of functor */
00034 typedef AdaCost<REAL> Cost;
00035 
00036 const Cost _cost = Cost();
00037 
00038 struct exponential : public Cost {
00039     REAL lambda;
00040     exponential () : lambda(1) {};
00041     virtual REAL cost (const REAL& F, const REAL& y) const {
00042         return std::exp(-lambda*F*y);
00043     }
00044     virtual REAL deriv1 (const REAL& F, const REAL& y) const {
00045         return -lambda*y * std::exp(-lambda*F*y);
00046     }
00047 };
00048 
00049 struct logistic : public Cost {
00050     REAL lambda;
00051     logistic () : lambda(1) {};
00052     virtual REAL cost (const REAL& F, const REAL& y) const {
00053         return std::log(1 + std::exp(-lambda*F*y));
00054     }
00055     virtual REAL deriv1 (const REAL& F, const REAL& y) const {
00056         const REAL t = std::exp(-lambda*F*y);
00057         return -lambda*y * t / (1+t);
00058     }
00059 };
00060 
00061 struct sigmoid : public Cost {
00062     REAL lambda;
00063     sigmoid () : lambda(1) {};
00064     virtual REAL cost (const REAL& F, const REAL& y) const {
00065         return 1 - std::tanh(lambda*F*y);
00066     }
00067     virtual REAL deriv1 (const REAL& F, const REAL& y) const {
00068         const REAL t = std::tanh(lambda*F*y);
00069         return lambda*y * (t*t - 1);
00070     }
00071 };
00072 
00073 struct bisigmoid : public Cost {
00074     REAL lambda;
00075     REAL ratio;   
00076     bisigmoid () : lambda(1), ratio(0.8) {};
00077     virtual REAL cost (const REAL& F, const REAL& y) const {
00078         const REAL m = lambda*F*y;
00079         if (m > 0) return 1 - std::tanh(m)/lambda;
00080         return 1 - std::tanh(ratio*m)/(ratio*lambda);
00081     }
00082     virtual REAL deriv1 (const REAL& F, const REAL& y) const {
00083         const REAL m = lambda*F*y;
00084         const REAL t = std::tanh(m>0? m:(ratio*m));
00085         return y * (t*t - 1);
00086     }
00087 };
00088 
00089 }} // namespace lemga::cost
00090 
00091 #ifdef  __COST_H__  // check duplicate filenames
00092 #warning "This header file may conflict with another `cost.h' file."
00093 #endif
00094 #define __COST_H__
00095 #endif

Generated on Mon Jan 9 23:43:23 2006 for LEMGA by  doxygen 1.4.6