SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
TensorProductPairKernel.cpp
浏览该文件的文档.
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 }
virtual void cleanup()
Definition: Kernel.cpp:173
The class DenseFeatures implements dense feature matrices.
Definition: LDA.h:41
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
#define SG_REF(x)
Definition: SGObject.h:51
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
virtual bool init(CFeatures *l, CFeatures *r)
virtual bool init_normalizer()
Definition: Kernel.cpp:168
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:1061
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:158
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool has_features()
Definition: Kernel.h:534

SHOGUN 机器学习工具包 - 项目文档