SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CDotKernel(0), subkernel(NULL)
20 {
22 }
23 
25 : CDotKernel(size), subkernel(s)
26 {
29 }
30 
32 : CDotKernel(10), subkernel(s)
33 {
35  init(l, r);
37 }
38 
40 {
42  cleanup();
43 }
44 
45 bool CTensorProductPairKernel::init(CFeatures* l, CFeatures* r)
46 {
47  CDotKernel::init(l, r);
49  return true;
50 }
51 
52 float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b)
53 {
54  int32_t alen, blen;
55  bool afree, bfree;
56 
57  int32_t* avec=((CDenseFeatures<int32_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
58  int32_t* bvec=((CDenseFeatures<int32_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
59 
60  ASSERT(alen==2)
61  ASSERT(blen==2)
62 
63  CKernel* k=subkernel;
64  ASSERT(k && k->has_features())
65 
66  int32_t a=avec[0];
67  int32_t b=avec[1];
68  int32_t c=bvec[0];
69  int32_t d=bvec[1];
70 
71  float64_t result = k->kernel(a,c)*k->kernel(b,d) + k->kernel(a,d)*k->kernel(b,c);
72 
73  ((CDenseFeatures<int32_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
74  ((CDenseFeatures<int32_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
75 
76  return result;
77 }
78 
80 {
81  SG_ADD((CSGObject**)&subkernel, "subkernel", "the subkernel", MS_AVAILABLE);
82 }

SHOGUN Machine Learning Toolbox - Documentation