/usr/include/shogun/kernel/DiceKernelNormalizer.h is in libshogun-dev 1.1.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2009 Soeren Sonnenburg
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _DICEKERNELNORMALIZER_H___
#define _DICEKERNELNORMALIZER_H___
#include <shogun/kernel/KernelNormalizer.h>
#include <shogun/kernel/CommWordStringKernel.h>
namespace shogun
{
/** @brief DiceKernelNormalizer performs kernel normalization inspired by the Dice
* coefficient (see http://en.wikipedia.org/wiki/Dice's_coefficient)
*
* \f[
* k'({\bf x},{\bf x'}) = \frac{2k({\bf x},{\bf x'})}{k({\bf x},{\bf x})+k({\bf x'},{\bf x'}}
* \f]
*/
class CDiceKernelNormalizer : public CKernelNormalizer
{
public:
/** default constructor
* @param use_opt_diag - some kernels support faster diagonal compuation
* via compute_diag(idx), this flag enables this
*/
CDiceKernelNormalizer(bool use_opt_diag=false) : CKernelNormalizer(),
diag_lhs(NULL), num_diag_lhs(0), diag_rhs(NULL), num_diag_rhs(0),
use_optimized_diagonal_computation(use_opt_diag)
{
m_parameters->add_vector(&diag_lhs, &num_diag_lhs, "diag_lhs",
"K(x,x) for left hand side examples.");
m_parameters->add_vector(&diag_rhs, &num_diag_rhs, "diag_rhs",
"K(x,x) for right hand side examples.");
m_parameters->add(&use_optimized_diagonal_computation,
"use_optimized_diagonal_computation",
"flat if optimized diagonal computation is used");
}
/** default destructor */
virtual ~CDiceKernelNormalizer()
{
SG_FREE(diag_lhs);
SG_FREE(diag_rhs);
}
/** initialization of the normalizer
* @param k kernel */
virtual bool init(CKernel* k)
{
ASSERT(k);
num_diag_lhs=k->get_num_vec_lhs();
num_diag_rhs=k->get_num_vec_rhs();
ASSERT(num_diag_lhs>0);
ASSERT(num_diag_rhs>0);
CFeatures* old_lhs=k->lhs;
CFeatures* old_rhs=k->rhs;
k->lhs=old_lhs;
k->rhs=old_lhs;
bool r1=alloc_and_compute_diag(k, diag_lhs, num_diag_lhs);
k->lhs=old_rhs;
k->rhs=old_rhs;
bool r2=alloc_and_compute_diag(k, diag_rhs, num_diag_rhs);
k->lhs=old_lhs;
k->rhs=old_rhs;
return r1 && r2;
}
/** normalize the kernel value
* @param value kernel value
* @param idx_lhs index of left hand side vector
* @param idx_rhs index of right hand side vector
*/
inline virtual float64_t normalize(
float64_t value, int32_t idx_lhs, int32_t idx_rhs)
{
float64_t diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
return 2*value/diag_sum;
}
/** normalize only the left hand side vector
* @param value value of a component of the left hand side feature vector
* @param idx_lhs index of left hand side vector
*/
inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
{
SG_ERROR("linadd not supported with Dice normalization.\n");
return 0;
}
/** normalize only the right hand side vector
* @param value value of a component of the right hand side feature vector
* @param idx_rhs index of right hand side vector
*/
inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
{
SG_ERROR("linadd not supported with Dice normalization.\n");
return 0;
}
/** Returns the name of the SGSerializable instance. It MUST BE
* the CLASS NAME without the prefixed `C'.
*
* @return name of the SGSerializable
*/
virtual const char* get_name() const {
return "DiceKernelNormalizer"; }
public:
/**
* alloc and compute the vector containing the square root of the
* diagonal elements of this kernel.
*/
bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
{
SG_FREE(v);
v=SG_MALLOC(float64_t, num);
for (int32_t i=0; i<num; i++)
{
if (k->get_kernel_type() == K_COMMWORDSTRING)
{
if (use_optimized_diagonal_computation)
v[i]=((CCommWordStringKernel*) k)->compute_diag(i);
else
v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
}
else
v[i]=k->compute(i,i);
if (v[i]==0.0)
v[i]=1e-16; /* avoid divide by zero exception */
}
return (v!=NULL);
}
protected:
/** diagonal left-hand side */
float64_t* diag_lhs;
/** num diag lhs */
int32_t num_diag_lhs;
/** diagonal right-hand side */
float64_t* diag_rhs;
/** num diag rhs */
int32_t num_diag_rhs;
/** flat if optimized diagonal computation is used */
bool use_optimized_diagonal_computation;
};
}
#endif
|