SHOGUN
v3.2.0
|
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 <shogun/kernel/normalizer/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 : CKernelNormalizer() 00058 { 00059 SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", 00060 MS_AVAILABLE); 00061 SG_ADD(&ridge, "ridge", "Ridge added to diagonal.", MS_AVAILABLE); 00062 00063 scale=c; 00064 ridge=r; 00065 } 00066 00068 virtual ~CRidgeKernelNormalizer() 00069 { 00070 } 00071 00074 virtual bool init(CKernel* k) 00075 { 00076 if (scale<=0) 00077 { 00078 ASSERT(k) 00079 int32_t num=k->get_num_vec_lhs(); 00080 ASSERT(num>0) 00081 00082 CFeatures* old_lhs=k->lhs; 00083 CFeatures* old_rhs=k->rhs; 00084 k->lhs=old_lhs; 00085 k->rhs=old_lhs; 00086 00087 float64_t sum=0; 00088 for (int32_t i=0; i<num; i++) 00089 sum+=k->compute(i, i); 00090 00091 scale=sum/num; 00092 k->lhs=old_lhs; 00093 k->rhs=old_rhs; 00094 } 00095 00096 ridge*=scale; 00097 return true; 00098 } 00099 00105 virtual float64_t normalize( 00106 float64_t value, int32_t idx_lhs, int32_t idx_rhs) 00107 { 00108 if (idx_lhs==idx_rhs) 00109 return value+ridge; 00110 else 00111 return value; 00112 } 00113 00118 virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs) 00119 { 00120 SG_ERROR("linadd not supported with Ridge normalization.\n") 00121 return 0; 00122 } 00123 00128 virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs) 00129 { 00130 SG_ERROR("linadd not supported with Ridge normalization.\n") 00131 return 0; 00132 } 00133 00135 virtual const char* get_name() const { return "RidgeKernelNormalizer"; } 00136 00137 protected: 00139 float64_t ridge; 00141 float64_t scale; 00142 }; 00143 } 00144 #endif // _RIDGEKERNELNORMALIZER_H___