SHOGUN
v1.1.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) 2010 Gorden Jemwa 00008 * Copyright (C) 2010 University of Stellenbosch 00009 */ 00010 00011 #ifndef _ZEROMEANCENTERKERNELNORMALIZER_H___ 00012 #define _ZEROMEANCENTERKERNELNORMALIZER_H___ 00013 00014 #include <shogun/kernel/KernelNormalizer.h> 00015 00016 namespace shogun 00017 { 00041 class CZeroMeanCenterKernelNormalizer : public CKernelNormalizer 00042 { 00043 public: 00046 CZeroMeanCenterKernelNormalizer() 00047 : CKernelNormalizer(), ktrain_row_means(NULL), num_ktrain(0), 00048 ktest_row_means(NULL), num_ktest(0) 00049 { 00050 m_parameters->add_vector(&ktrain_row_means, &num_ktrain, 00051 "num_ktrain", "Train row means."); 00052 m_parameters->add_vector(&ktest_row_means, &num_ktest, 00053 "num_ktest","Test row means."); 00054 } 00055 00057 virtual ~CZeroMeanCenterKernelNormalizer() 00058 { 00059 SG_FREE(ktrain_row_means); 00060 SG_FREE(ktest_row_means); 00061 } 00062 00065 virtual bool init(CKernel* k) 00066 { 00067 ASSERT(k); 00068 int32_t num_lhs=k->get_num_vec_lhs(); 00069 int32_t num_rhs=k->get_num_vec_rhs(); 00070 ASSERT(num_lhs>0); 00071 ASSERT(num_rhs>0); 00072 00073 CFeatures* old_lhs=k->lhs; 00074 CFeatures* old_rhs=k->rhs; 00075 00076 /* compute mean for each row of the train matrix*/ 00077 k->lhs=old_lhs; 00078 k->rhs=old_lhs; 00079 00080 bool r1=alloc_and_compute_row_means(k, ktrain_row_means, num_lhs,num_lhs); 00081 00082 /* compute mean for each row of the test matrix */ 00083 k->lhs=old_lhs; 00084 k->rhs=old_rhs; 00085 00086 bool r2=alloc_and_compute_row_means(k, ktest_row_means, num_lhs,num_rhs); 00087 00088 /* compute train kernel matrix mean */ 00089 ktrain_mean=0; 00090 for (int32_t i=0;i<num_lhs;i++) 00091 ktrain_mean += (ktrain_row_means[i]/num_lhs); 00092 00093 k->lhs=old_lhs; 00094 k->rhs=old_rhs; 00095 00096 return r1 && r2; 00097 } 00098 00104 inline virtual float64_t normalize( 00105 float64_t value, int32_t idx_lhs, int32_t idx_rhs) 00106 { 00107 value += (-ktrain_row_means[idx_lhs] - ktest_row_means[idx_rhs] + ktrain_mean); 00108 return value; 00109 } 00110 00115 inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs) 00116 { 00117 SG_ERROR("normalize_lhs not implemented"); 00118 return 0; 00119 } 00120 00125 inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs) 00126 { 00127 SG_ERROR("normalize_rhs not implemented"); 00128 return 0; 00129 } 00130 00135 bool alloc_and_compute_row_means(CKernel* k, float64_t* &v, int32_t num_lhs, int32_t num_rhs) 00136 { 00137 SG_FREE(v); 00138 v=SG_MALLOC(float64_t, num_rhs); 00139 00140 for (int32_t i=0; i<num_rhs; i++) 00141 { 00142 v[i]=0; 00143 for (int32_t j=0; j<num_lhs; j++) 00144 v[i] += ( k->compute(j,i)/num_lhs ); 00145 } 00146 return (v!=NULL); 00147 } 00148 00150 inline virtual const char* get_name() const { return "ZeroMeanCenterKernelNormalizer"; } 00151 00152 protected: 00154 float64_t* ktrain_row_means; 00155 00157 int32_t num_ktrain; 00158 00160 float64_t* ktest_row_means; 00161 00163 int32_t num_ktest; 00164 00166 float64_t ktrain_mean; 00167 }; 00168 } 00169 #endif