SqrtDiagKernelNormalizer.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 _SQRTDIAGKERNELNORMALIZER_H___
00012 #define _SQRTDIAGKERNELNORMALIZER_H___
00013 
00014 #include "kernel/KernelNormalizer.h"
00015 #include "kernel/CommWordStringKernel.h"
00016 
00027 class CSqrtDiagKernelNormalizer : public CKernelNormalizer
00028 {
00029     public:
00034         CSqrtDiagKernelNormalizer(bool use_opt_diag=false): sqrtdiag_lhs(NULL),
00035             sqrtdiag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag)
00036         {
00037         }
00038 
00040         virtual ~CSqrtDiagKernelNormalizer()
00041         {
00042             delete[] sqrtdiag_lhs;
00043             delete[] sqrtdiag_rhs;
00044         }
00045 
00048         virtual bool init(CKernel* k)
00049         {
00050             ASSERT(k);
00051             int32_t num_lhs=k->get_num_vec_lhs();
00052             int32_t num_rhs=k->get_num_vec_rhs();
00053             ASSERT(num_lhs>0);
00054             ASSERT(num_rhs>0);
00055 
00056             CFeatures* old_lhs=k->lhs;
00057             CFeatures* old_rhs=k->rhs;
00058 
00059             k->lhs=old_lhs;
00060             k->rhs=old_lhs;
00061             bool r1=alloc_and_compute_diag(k, sqrtdiag_lhs, num_lhs);
00062 
00063             k->lhs=old_rhs;
00064             k->rhs=old_rhs;
00065             bool r2=alloc_and_compute_diag(k, sqrtdiag_rhs, num_rhs);
00066 
00067             k->lhs=old_lhs;
00068             k->rhs=old_rhs;
00069 
00070             return r1 && r2;
00071         }
00072 
00078         inline virtual float64_t normalize(
00079             float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00080         {
00081             float64_t sqrt_both=sqrtdiag_lhs[idx_lhs]*sqrtdiag_rhs[idx_rhs];
00082             return value/sqrt_both;
00083         }
00084 
00089         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00090         {
00091             return value/sqrtdiag_lhs[idx_lhs];
00092         }
00093 
00098         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00099         {
00100             return value/sqrtdiag_rhs[idx_rhs];
00101         }
00102 
00103     public:
00108         bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
00109         {
00110             delete[] v;
00111             v=new float64_t[num];
00112 
00113             for (int32_t i=0; i<num; i++)
00114             {
00115                 if (k->get_kernel_type() == K_COMMWORDSTRING)
00116                 {
00117                     if (use_optimized_diagonal_computation)
00118                         v[i]=sqrt(((CCommWordStringKernel*) k)->compute_diag(i));
00119                     else
00120                         v[i]=sqrt(((CCommWordStringKernel*) k)->compute_helper(i,i, true));
00121                 }
00122                 else
00123                     v[i]=sqrt(k->compute(i,i));
00124 
00125                 if (v[i]==0.0)
00126                     v[i]=1e-16; /* avoid divide by zero exception */
00127             }
00128 
00129             return (v!=NULL);
00130         }
00131 
00133         inline virtual const char* get_name() const { return "SqrtDiagKernelNormalizer"; }
00134 
00135     protected:
00137         float64_t* sqrtdiag_lhs;
00139         float64_t* sqrtdiag_rhs;
00141         bool use_optimized_diagonal_computation;
00142 };
00143 
00144 #endif

SHOGUN Machine Learning Toolbox - Documentation