/usr/include/shogun/kernel/CombinedKernel.h is in libshogun-dev 3.2.0-7.3build4.
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | /*
* 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) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _COMBINEDKERNEL_H___
#define _COMBINEDKERNEL_H___
#include <shogun/lib/List.h>
#include <shogun/lib/DynamicObjectArray.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/features/Features.h>
#include <shogun/features/CombinedFeatures.h>
namespace shogun
{
class CFeatures;
class CCombinedFeatures;
class CList;
class CListElement;
/**
* @brief The Combined kernel is used to combine a number of kernels into a
* single CombinedKernel object by linear combination.
*
* It keeps pointers to the added sub-kernels \f$k_m({\bf x}, {\bf x'})\f$ and
* for each sub-kernel - a kernel specific weight \f$\beta_m\f$.
*
* It is especially useful to combine kernels working on different domains and
* to combine kernels looking at independent features and requires
* CCombinedFeatures to be used.
*
* It is defined as:
*
* \f[
* k_{combined}({\bf x}, {\bf x'}) = \sum_{m=1}^M \beta_m k_m({\bf x}, {\bf x'})
* \f]
*
*/
class CCombinedKernel : public CKernel
{
public:
/** constructor
*
* @param size cache size
* @param append_subkernel_weights if subkernel weights shall be
* appended
*/
CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
virtual ~CCombinedKernel();
/** initialize kernel. Provided features have to be combined features.
* If they are not, all subkernels are tried to be initialised with the
* single same passed features objects
*
* @param lhs features of left-hand side
* @param rhs features of right-hand side
* @return if initializing was successful
*/
virtual bool init(CFeatures* lhs, CFeatures* rhs);
/** clean up kernel */
virtual void cleanup();
/** return what type of kernel we are
*
* @return kernel type COMBINED
*/
virtual EKernelType get_kernel_type()
{
return K_COMBINED;
}
/** return feature type the kernel can deal with
*
* @return feature type UNKNOWN
*/
virtual EFeatureType get_feature_type()
{
return F_UNKNOWN;
}
/** return feature class the kernel can deal with
*
* @return feature class COMBINED
*/
virtual EFeatureClass get_feature_class()
{
return C_COMBINED;
}
/** return the kernel's name
*
* @return name Combined
*/
virtual const char* get_name() const { return "CombinedKernel"; }
/** list kernels */
void list_kernels();
/** get first kernel
*
* @return first kernel
*/
inline CKernel* get_first_kernel()
{
return get_kernel(0);
}
/** get kernel
*
* @param idx index of kernel
* @return kernel at index idx
*/
inline CKernel* get_kernel(int32_t idx)
{
return (CKernel*) kernel_array->get_element(idx);
}
/** get last kernel
*
* @return last kernel
*/
inline CKernel* get_last_kernel()
{
return get_kernel(get_num_kernels()-1);
}
/** insert kernel at position idx
* idx must be < num_kernels
*
* @param k kernel
* @param idx the index of the position where the kernel should be added
* @return if inserting was successful
*/
inline bool insert_kernel(CKernel* k, int32_t idx)
{
ASSERT(k)
adjust_num_lhs_rhs_initialized(k);
if (!(k->has_property(KP_LINADD)))
unset_property(KP_LINADD);
return kernel_array->insert_element(k, idx);
}
/** append kernel to the end of the array
*
* @param k kernel
* @return if appending was successful
*/
inline bool append_kernel(CKernel* k)
{
ASSERT(k)
adjust_num_lhs_rhs_initialized(k);
if (!(k->has_property(KP_LINADD)))
unset_property(KP_LINADD);
int n = get_num_kernels();
kernel_array->push_back(k);
return n+1==get_num_kernels();
}
/** delete kernel
*
* @param idx the index of the kernel to delete
* @return if deleting was successful
*/
inline bool delete_kernel(int32_t idx)
{
bool succesful_deletion = kernel_array->delete_element(idx);
if (get_num_kernels()==0)
{
num_lhs=0;
num_rhs=0;
}
return succesful_deletion;
}
/** check if subkernel weights are appended
*
* @return if subkernel weigths are appended
*/
inline bool get_append_subkernel_weights()
{
return append_subkernel_weights;
}
/** get number of subkernels
*
* @return number of subkernels
*/
inline int32_t get_num_subkernels()
{
if (append_subkernel_weights)
{
int32_t num_subkernels = 0;
for (index_t k_idx=0; k_idx<get_num_kernels(); k_idx++)
{
CKernel* k = get_kernel(k_idx);
num_subkernels += k->get_num_subkernels();
SG_UNREF(k);
}
return num_subkernels;
}
else
return get_num_kernels();
}
/** get number of contained kernels
*
* @return number of contained kernels
*/
int32_t get_num_kernels()
{
return kernel_array->get_num_elements();
}
/** test whether features have been assigned to lhs and rhs
*
* @return true if features are assigned
*/
virtual bool has_features()
{
return initialized;
}
/** remove lhs from kernel */
virtual void remove_lhs();
/** remove rhs from kernel */
virtual void remove_rhs();
/** remove lhs and rhs from kernel */
virtual void remove_lhs_and_rhs();
/** initialize optimization
*
* @param count count
* @param IDX index
* @param weights weights
* @return if initializing was successful
*/
virtual bool init_optimization(
int32_t count, int32_t *IDX, float64_t * weights);
/** delete optimization
*
* @return if deleting was successful
*/
virtual bool delete_optimization();
/** compute optimized
*
* @param idx index to compute
* @return optimized value at given index
*/
virtual float64_t compute_optimized(int32_t idx);
/** computes output for a batch of examples in an optimized fashion
* (favorable if kernel supports it, i.e. has KP_BATCHEVALUATION. to
* the outputvector target (of length num_vec elements) the output for
* the examples enumerated in vec_idx are added. therefore make sure
* that it is initialized with ZERO. the following num_suppvec, IDX,
* alphas arguments are the number of support vectors, their indices and
* weights
*/
virtual void compute_batch(
int32_t num_vec, int32_t* vec_idx, float64_t* target,
int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
float64_t factor=1.0);
/** helper for compute optimized kernel
*
* @param p thread parameter
*/
static void* compute_optimized_kernel_helper(void* p);
/** helper for compute kernel
*
* @param p thread parameter
*/
static void* compute_kernel_helper(void* p);
/** emulates batch computation, via linadd optimization w^t x or even
* down to sum_i alpha_i K(x_i,x)
*
* @param k kernel
* @param num_vec number of vectors
* @param vec_idx vector index
* @param target target
* @param num_suppvec number of support vectors
* @param IDX IDX
* @param weights weights
*/
void emulate_compute_batch(
CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
int32_t num_suppvec, int32_t* IDX, float64_t* weights);
/** add to normal vector
*
* @param idx where to add
* @param weight what to add
*/
virtual void add_to_normal(int32_t idx, float64_t weight);
/** clear normal vector */
virtual void clear_normal();
/** compute by subkernel
*
* @param idx index
* @param subkernel_contrib subkernel contribution
*/
virtual void compute_by_subkernel(
int32_t idx, float64_t * subkernel_contrib);
/** get subkernel weights
*
* @param num_weights where number of weights is stored
* @return subkernel weights
*/
virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
/** get subkernel weights (swig compatible)
*
* @return subkernel weights
*/
virtual SGVector<float64_t> get_subkernel_weights();
/** set subkernel weights
*
* @param weights new subkernel weights
*/
virtual void set_subkernel_weights(SGVector<float64_t> weights);
/** set optimization type
*
* @param t optimization type
*/
virtual void set_optimization_type(EOptimizationType t);
/** precompute all sub-kernels */
bool precompute_subkernels();
/** Returns a casted version of the given kernel. Throws an error
* if parameter is not of class CombinedKernel. SG_REF's the returned
* kernel
*
* @param kernel kernel to cast to CombinedKernel
* @return casted version of kernel.
*/
static CCombinedKernel* obtain_from_generic(CKernel* kernel);
/** return derivative with respect to specified parameter
*
* @param param the parameter
* @param index the index of the element if parameter is a vector
*
* @return gradient with respect to parameter
*/
SGMatrix<float64_t> get_parameter_gradient(const TParameter* param,
index_t index=-1);
/** Get the Kernel array
*
* @return kernel array
*/
inline CDynamicObjectArray* get_array()
{
SG_REF(kernel_array);
return kernel_array;
}
/** Returns a list of all the different CombinedKernels produced by the
* cross-product between the kernel lists The returned list performs
* reference counting on the contained CombinedKernels.
*
* @param kernel_list a list of lists of kernels. Each sub-list must
* contain kernels of the same type
*
* @return a list of CombinedKernels.
*/
static CList* combine_kernels(CList* kernel_list);
protected:
/** compute kernel function
*
* @param x x
* @param y y
* @return computed kernel function
*/
virtual float64_t compute(int32_t x, int32_t y);
/** adjust the variables num_lhs, num_rhs and initialized
* based on the kernel to be appended/inserted
*
* @param k kernel
*/
inline void adjust_num_lhs_rhs_initialized(CKernel* k)
{
ASSERT(k)
if (k->get_num_vec_lhs())
{
if (num_lhs)
ASSERT(num_lhs==k->get_num_vec_lhs())
num_lhs=k->get_num_vec_lhs();
if (!get_num_subkernels())
{
initialized=true;
}
}
else
initialized=false;
if (k->get_num_vec_rhs())
{
if (num_rhs)
ASSERT(num_rhs==k->get_num_vec_rhs())
num_rhs=k->get_num_vec_rhs();
if (!get_num_subkernels())
{
initialized=true;
}
}
else
initialized=false;
}
private:
void init();
protected:
/** list of kernels */
CDynamicObjectArray* kernel_array;
/** support vector count */
int32_t sv_count;
/** support vector index */
int32_t* sv_idx;
/** support vector weights */
float64_t* sv_weight;
/** subkernel weights buffers */
float64_t* subkernel_weights_buffer;
/** if subkernel weights are appended */
bool append_subkernel_weights;
/** whether kernel is ready to be used */
bool initialized;
};
}
#endif /* _COMBINEDKERNEL_H__ */
|