DiceKernelNormalizer.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 _DICEKERNELNORMALIZER_H___
00012 #define _DICEKERNELNORMALIZER_H___
00013 
00014 #include "kernel/KernelNormalizer.h"
00015 #include "kernel/CommWordStringKernel.h"
00016 
00024 class CDiceKernelNormalizer : public CKernelNormalizer
00025 {
00026     public:
00031         CDiceKernelNormalizer(bool use_opt_diag=false) : diag_lhs(NULL),
00032             diag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag)
00033         {
00034         }
00035 
00037         virtual ~CDiceKernelNormalizer()
00038         {
00039             delete[] diag_lhs;
00040             delete[] diag_rhs;
00041         }
00042 
00045         virtual bool init(CKernel* k)
00046         {
00047             ASSERT(k);
00048             int32_t num_lhs=k->get_num_vec_lhs();
00049             int32_t num_rhs=k->get_num_vec_rhs();
00050             ASSERT(num_lhs>0);
00051             ASSERT(num_rhs>0);
00052 
00053             CFeatures* old_lhs=k->lhs;
00054             CFeatures* old_rhs=k->rhs;
00055 
00056             k->lhs=old_lhs;
00057             k->rhs=old_lhs;
00058             bool r1=alloc_and_compute_diag(k, diag_lhs, num_lhs);
00059 
00060             k->lhs=old_rhs;
00061             k->rhs=old_rhs;
00062             bool r2=alloc_and_compute_diag(k, diag_rhs, num_rhs);
00063 
00064             k->lhs=old_lhs;
00065             k->rhs=old_rhs;
00066 
00067             return r1 && r2;
00068         }
00069 
00075         inline virtual float64_t normalize(
00076             float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00077         {
00078             float64_t diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
00079             return 2*value/diag_sum;
00080         }
00081 
00086         inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00087         {
00088             SG_ERROR("linadd not supported with Dice normalization.\n");
00089             return 0;
00090         }
00091 
00096         inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00097         {
00098             SG_ERROR("linadd not supported with Dice normalization.\n");
00099             return 0;
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]=((CCommWordStringKernel*) k)->compute_diag(i);
00118                     else
00119                         v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
00120                 }
00121                 else
00122                     v[i]=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* diag_lhs;
00135         float64_t* diag_rhs;
00137         bool use_optimized_diagonal_computation;
00138 };
00139 
00140 #endif

SHOGUN Machine Learning Toolbox - Documentation