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) 2008 Soeren Sonnenburg
00008  * Copyright (C) 2008 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 
00026 class CSqrtDiagKernelNormalizer : public CKernelNormalizer
00027 {
00028     public:
00033         CSqrtDiagKernelNormalizer(bool use_opt_diag=false): sqrtdiag_lhs(NULL),
00034             sqrtdiag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag)
00035         {
00036         }
00037 
00039         virtual ~CSqrtDiagKernelNormalizer()
00040         {
00041             delete[] sqrtdiag_lhs;
00042             delete[] sqrtdiag_rhs;
00043         }
00044 
00047         virtual bool init(CKernel* k)
00048         {
00049             ASSERT(k);
00050             int32_t num_lhs=k->get_num_vec_lhs();
00051             int32_t num_rhs=k->get_num_vec_rhs();
00052             ASSERT(num_lhs>0);
00053             ASSERT(num_rhs>0);
00054 
00055             CFeatures* old_lhs=k->lhs;
00056             CFeatures* old_rhs=k->rhs;
00057 
00058             k->lhs=old_lhs;
00059             k->rhs=old_lhs;
00060             bool r1=alloc_and_compute_diag(k, sqrtdiag_lhs, num_lhs);
00061 
00062             k->lhs=old_rhs;
00063             k->rhs=old_rhs;
00064             bool r2=alloc_and_compute_diag(k, sqrtdiag_rhs, num_rhs);
00065 
00066             k->lhs=old_lhs;
00067             k->rhs=old_rhs;
00068 
00069             return r1 && r2;
00070         }
00071 
00077         inline virtual float64_t normalize(
00078             float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00079         {
00080             float64_t sqrt_both=sqrtdiag_lhs[idx_lhs]*sqrtdiag_rhs[idx_rhs];
00081             return value/sqrt_both;
00082         }
00083 
00088         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00089         {
00090             return value/sqrtdiag_lhs[idx_lhs];
00091         }
00092 
00097         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00098         {
00099             return value/sqrtdiag_rhs[idx_rhs];
00100         }
00101 
00102     public:
00107         bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
00108         {
00109             delete[] v;
00110             v=new float64_t[num];
00111 
00112             for (int32_t i=0; i<num; i++)
00113             {
00114                 if (k->get_kernel_type() == K_COMMWORDSTRING)
00115                 {
00116                     if (use_optimized_diagonal_computation)
00117                         v[i]=sqrt(((CCommWordStringKernel*) k)->compute_diag(i));
00118                     else
00119                         v[i]=sqrt(((CCommWordStringKernel*) k)->compute_helper(i,i, true));
00120                 }
00121                 else
00122                     v[i]=sqrt(k->compute(i,i));
00123 
00124                 if (v[i]==0.0)
00125                     v[i]=1e-16; /* avoid divide by zero exception */
00126             }
00127 
00128             return (v!=NULL);
00129         }
00130 
00131     protected:
00133         float64_t* sqrtdiag_lhs;
00135         float64_t* sqrtdiag_rhs;
00137         bool use_optimized_diagonal_computation;
00138 };
00139 
00140 #endif

SHOGUN Machine Learning Toolbox - Documentation