TanimotoKernelNormalizer.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-2009 Soeren Sonnenburg
00008  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef _TANIMOTOKERNELNORMALIZER_H___
00012 #define _TANIMOTOKERNELNORMALIZER_H___
00013 
00014 #include "kernel/KernelNormalizer.h"
00015 #include "kernel/CommWordStringKernel.h"
00016 
00017 namespace shogun
00018 {
00026 class CTanimotoKernelNormalizer : public CKernelNormalizer
00027 {
00028     public:
00033         CTanimotoKernelNormalizer(bool use_opt_diag=false) : diag_lhs(NULL),
00034             diag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag)
00035         {
00036         }
00037 
00039         virtual ~CTanimotoKernelNormalizer()
00040         {
00041             delete[] diag_lhs;
00042             delete[] diag_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, diag_lhs, num_lhs);
00061 
00062             k->lhs=old_rhs;
00063             k->rhs=old_rhs;
00064             bool r2=alloc_and_compute_diag(k, diag_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 diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
00081             return value/(diag_sum-value);
00082         }
00083 
00088         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00089         {
00090             SG_ERROR("linadd not supported with Tanimoto normalization.\n");
00091             return 0;
00092         }
00093 
00098         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00099         {
00100             SG_ERROR("linadd not supported with Tanimoto normalization.\n");
00101             return 0;
00102         }
00103 
00104     public:
00109         bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
00110         {
00111             delete[] v;
00112             v=new float64_t[num];
00113 
00114             for (int32_t i=0; i<num; i++)
00115             {
00116                 if (k->get_kernel_type() == K_COMMWORDSTRING)
00117                 {
00118                     if (use_optimized_diagonal_computation)
00119                         v[i]=((CCommWordStringKernel*) k)->compute_diag(i);
00120                     else
00121                         v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
00122                 }
00123                 else
00124                     v[i]=k->compute(i,i);
00125 
00126                 if (v[i]==0.0)
00127                     v[i]=1e-16; /* avoid divide by zero exception */
00128             }
00129 
00130             return (v!=NULL);
00131         }
00132 
00133     protected:
00135         float64_t* diag_lhs;
00137         float64_t* diag_rhs;
00139         bool use_optimized_diagonal_computation;
00140 };
00141 }
00142 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation