AvgDiagKernelNormalizer.h

Go to the documentation of this file.
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 _AVGDIAGKERNELNORMALIZER_H___
00012 #define _AVGDIAGKERNELNORMALIZER_H___
00013 
00014 #include "kernel/KernelNormalizer.h"
00015 
00030 class CAvgDiagKernelNormalizer : public CKernelNormalizer
00031 {
00032     public:
00038         CAvgDiagKernelNormalizer(float64_t c=0.0)
00039         {
00040             scale=c;
00041         }
00042 
00044         virtual ~CAvgDiagKernelNormalizer()
00045         {
00046         }
00047 
00050         virtual bool init(CKernel* k)
00051         {
00052             if (scale<=0)
00053             {
00054                 ASSERT(k);
00055                 int32_t num=k->get_num_vec_lhs();
00056                 ASSERT(num>0);
00057 
00058                 CFeatures* old_lhs=k->lhs;
00059                 CFeatures* old_rhs=k->rhs;
00060                 k->lhs=old_lhs;
00061                 k->rhs=old_lhs;
00062 
00063                 float64_t sum=0;
00064                 for (int32_t i=0; i<num; i++)
00065                     sum+=k->compute(i, i);
00066 
00067                 scale=sum/num;
00068                 k->lhs=old_lhs;
00069                 k->rhs=old_rhs;
00070             }
00071 
00072             return true;
00073         }
00074 
00080         inline virtual float64_t normalize(
00081             float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00082         {
00083             return value/scale;
00084         }
00085 
00090         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00091         {
00092             return value/sqrt(scale);
00093         }
00094 
00099         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00100         {
00101             return value/sqrt(scale);
00102         }
00103 
00105         inline virtual const char* get_name() const { return "AvgDiagKernelNormalizer"; }
00106 
00107     protected:
00109         float64_t scale;
00110 };
00111 
00112 #endif

SHOGUN Machine Learning Toolbox - Documentation