00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2009 Soeren Sonnenburg 00008 * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef _RIDGEKERNELNORMALIZER_H___ 00012 #define _RIDGEKERNELNORMALIZER_H___ 00013 00014 #include "kernel/KernelNormalizer.h" 00015 00016 namespace shogun 00017 { 00043 class CRidgeKernelNormalizer : public CKernelNormalizer 00044 { 00045 public: 00056 CRidgeKernelNormalizer(float64_t r=1e-10, float64_t c=0.0) 00057 { 00058 scale=c; 00059 ridge=r; 00060 } 00061 00063 virtual ~CRidgeKernelNormalizer() 00064 { 00065 } 00066 00069 virtual bool init(CKernel* k) 00070 { 00071 if (scale<=0) 00072 { 00073 ASSERT(k); 00074 int32_t num=k->get_num_vec_lhs(); 00075 ASSERT(num>0); 00076 00077 CFeatures* old_lhs=k->lhs; 00078 CFeatures* old_rhs=k->rhs; 00079 k->lhs=old_lhs; 00080 k->rhs=old_lhs; 00081 00082 float64_t sum=0; 00083 for (int32_t i=0; i<num; i++) 00084 sum+=k->compute(i, i); 00085 00086 scale=sum/num; 00087 k->lhs=old_lhs; 00088 k->rhs=old_rhs; 00089 } 00090 00091 ridge*=scale; 00092 return true; 00093 } 00094 00100 inline virtual float64_t normalize( 00101 float64_t value, int32_t idx_lhs, int32_t idx_rhs) 00102 { 00103 if (idx_lhs==idx_rhs) 00104 return value+ridge; 00105 else 00106 return value; 00107 } 00108 00113 inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs) 00114 { 00115 SG_ERROR("linadd not supported with Ridge normalization.\n"); 00116 return 0; 00117 } 00118 00123 inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs) 00124 { 00125 SG_ERROR("linadd not supported with Ridge normalization.\n"); 00126 return 0; 00127 } 00128 00130 inline virtual const char* get_name() const { return "RidgeKernelNormalizer"; } 00131 00132 protected: 00134 float64_t ridge; 00136 float64_t scale; 00137 }; 00138 } 00139 #endif // _RIDGEKERNELNORMALIZER_H___