SHOGUN
v2.0.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 _VARIANCEKERNELNORMALIZER_H___ 00012 #define _VARIANCEKERNELNORMALIZER_H___ 00013 00014 #include <shogun/kernel/normalizer/KernelNormalizer.h> 00015 00016 namespace shogun 00017 { 00027 class CVarianceKernelNormalizer : public CKernelNormalizer 00028 { 00029 public: 00032 CVarianceKernelNormalizer() 00033 : CKernelNormalizer(), meandiff(1.0), sqrt_meandiff(1.0) 00034 { 00035 SG_ADD(&meandiff, "meandiff", "Scaling constant.", MS_AVAILABLE); 00036 SG_ADD(&sqrt_meandiff, "sqrt_meandiff", 00037 "Square root of scaling constant.", MS_AVAILABLE); 00038 } 00039 00041 virtual ~CVarianceKernelNormalizer() 00042 { 00043 } 00044 00047 virtual bool init(CKernel* k) 00048 { 00049 ASSERT(k); 00050 int32_t n=k->get_num_vec_lhs(); 00051 ASSERT(n>0); 00052 00053 CFeatures* old_lhs=k->lhs; 00054 CFeatures* old_rhs=k->rhs; 00055 k->lhs=old_lhs; 00056 k->rhs=old_lhs; 00057 00058 float64_t diag_mean=0; 00059 float64_t overall_mean=0; 00060 for (int32_t i=0; i<n; i++) 00061 { 00062 diag_mean+=k->compute(i, i); 00063 00064 for (int32_t j=0; j<n; j++) 00065 overall_mean+=k->compute(i, j); 00066 } 00067 diag_mean/=n; 00068 overall_mean/=((float64_t) n)*n; 00069 00070 k->lhs=old_lhs; 00071 k->rhs=old_rhs; 00072 00073 meandiff=1.0/(diag_mean-overall_mean); 00074 sqrt_meandiff=CMath::sqrt(meandiff); 00075 00076 return true; 00077 } 00078 00084 inline virtual float64_t normalize( 00085 float64_t value, int32_t idx_lhs, int32_t idx_rhs) 00086 { 00087 return value*meandiff; 00088 } 00089 00094 inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs) 00095 { 00096 return value*sqrt_meandiff; 00097 } 00098 00103 inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs) 00104 { 00105 return value*sqrt_meandiff; 00106 } 00107 00109 inline virtual const char* get_name() const { return "VarianceKernelNormalizer"; } 00110 00111 protected: 00113 float64_t meandiff; 00115 float64_t sqrt_meandiff; 00116 }; 00117 } 00118 #endif