SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearARDKernel.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  * (W) 2012 Jacob Walker
8  *
9  * Adapted from WeightedDegreeRBFKernel.cpp
10  *
11  */
12 
13 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 : CDotKernel()
22 {
23  init();
24 }
25 
26 
28 : CDotKernel(size)
29 {
30  init();
31 }
32 
35  int32_t size)
36 : CDotKernel(size)
37 {
38  init();
39  init(l,r);
40 }
41 
42 void CLinearARDKernel::init()
43 {
45  SG_ADD(&m_weights, "weights", "Feature Weights", MS_AVAILABLE);
46 }
47 
49 {
51 }
52 
53 bool CLinearARDKernel::init(CFeatures* l, CFeatures* r)
54 {
55  CDotKernel::init(l, r);
56 
58 
59  return init_normalizer();
60 }
61 
63 {
64  if (!lhs || !rhs)
65  return;
66 
67  int32_t alen, blen;
68 
69  alen = ((CDenseFeatures<float64_t>*) lhs)->get_num_features();
70  blen = ((CDenseFeatures<float64_t>*) rhs)->get_num_features();
71 
72  REQUIRE(alen==blen, "Number of Right and Left Hand "\
73  "Features Must be the Same./n");
74 
75  if (m_weights.vlen != alen)
76  {
78 
79  for (int32_t i=0; i < alen; i++)
80  m_weights[i]=1.0;
81  }
82 
83  SG_DEBUG("Initialized weights for LinearARDKernel (%p).\n", this);
84 
85 }
86 
88 {
89  if (i >= m_weights.vlen)
90  {
91  SG_ERROR("Index %i out of range for LinearARDKernel."\
92  "Number of features is %i.\n", i, m_weights.vlen);
93  }
94 
95  m_weights[i]=w;
96 }
97 
99 {
100  if (i >= m_weights.vlen)
101  {
102  SG_ERROR("Index %i out of range for LinearARDKernel."\
103  "Number of features is %i.\n", i, m_weights.vlen);
104  }
105 
106  return m_weights[i];
107 }
108 
109 float64_t CLinearARDKernel::compute(int32_t idx_a, int32_t idx_b)
110 {
111  if (!lhs || !rhs)
112  SG_ERROR("Features not set!\n");
113 
115  = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a);
117  = ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b);
118 
119  REQUIRE(avec.vlen==bvec.vlen, "Number of Right and Left Hand "\
120  "Features Must be the Same./n");
121 
122  float64_t result=0;
123 
124  for (index_t i = 0; i < avec.vlen; i++)
125  result += avec[i]*bvec[i]*m_weights[i]*m_weights[i];
126 
127  return result;
128 }
129 
131  CSGObject* obj, index_t index)
132 {
133  if (!lhs || !rhs)
134  SG_ERROR("Features not set!\n");
135 
136  if (!strcmp(param->m_name, "weights") && obj == this)
137  {
138  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
139 
140  for (index_t j = 0; j < num_lhs; j++)
141  {
142  for (index_t k = 0; k < num_rhs; k++)
143  {
145  = ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(j);
147  = ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(k);
148 
149  REQUIRE(avec.vlen==bvec.vlen, "Number of Right and Left Hand "\
150  "Features Must be the Same./n");
151 
152  derivative(j,k) = avec[index]*bvec[index]*m_weights[index];
153  }
154  }
155  return derivative;
156  }
157 
158  else
159  return SGMatrix<float64_t>();
160 }
161 
162 

SHOGUN Machine Learning Toolbox - Documentation