SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TensorProductPairKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CDotKernel(0), subkernel(NULL)
21 {
23 }
24 
26 : CDotKernel(size), subkernel(s)
27 {
30 }
31 
33 : CDotKernel(10), subkernel(s)
34 {
36  init(l, r);
38 }
39 
41 {
43  cleanup();
44 }
45 
46 bool CTensorProductPairKernel::init(CFeatures* l, CFeatures* r)
47 {
48  CDotKernel::init(l, r);
50  return true;
51 }
52 
53 float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b)
54 {
55  int32_t alen, blen;
56  bool afree, bfree;
57 
58  int32_t* avec=((CSimpleFeatures<int32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
59  int32_t* bvec=((CSimpleFeatures<int32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
60 
61  ASSERT(alen==2);
62  ASSERT(blen==2);
63 
64  CKernel* k=subkernel;
65  ASSERT(k && k->has_features());
66 
67  int32_t a=avec[0];
68  int32_t b=avec[1];
69  int32_t c=bvec[0];
70  int32_t d=bvec[1];
71 
72  float64_t result = k->kernel(a,c)*k->kernel(b,d) + k->kernel(a,d)*k->kernel(b,c);
73 
74  ((CSimpleFeatures<int32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
75  ((CSimpleFeatures<int32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
76 
77  return result;
78 }
79 
81 {
82  m_parameters->add((CSGObject**)&subkernel, "subkernel", "the subkernel");
83 }
virtual void cleanup()
Definition: Kernel.cpp:138
Parameter * m_parameters
Definition: SGObject.h:297
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:218
#define SG_REF(x)
Definition: SGObject.h:44
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:29
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
#define ASSERT(x)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
double float64_t
Definition: common.h:56
virtual bool init(CFeatures *l, CFeatures *r)
virtual bool init_normalizer()
Definition: Kernel.cpp:133
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
#define SG_UNREF(x)
Definition: SGObject.h:45
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
The class SimpleFeatures implements dense feature matrices.
Definition: LDA.h:24
The Kernel base class.
Definition: Kernel.h:172
virtual bool has_features()
Definition: Kernel.h:455

SHOGUN Machine Learning Toolbox - Documentation