SHOGUN  v3.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  * Written (W) 2012 Jacob Walker
8  *
9  * Adapted from WeightedDegreeRBFKernel.cpp
10  */
11 
12 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 {
21  init();
22 }
23 
24 
26 {
27  init();
28 }
29 
31  CDenseFeatures<float64_t>* r, int32_t size) : CDotKernel(size)
32 {
33  init();
34  init(l,r);
35 }
36 
37 void CLinearARDKernel::init()
38 {
39  SG_ADD(&m_weights, "weights", "Feature weights", MS_AVAILABLE,
41 }
42 
44 {
46 }
47 
48 bool CLinearARDKernel::init(CFeatures* l, CFeatures* r)
49 {
50  CDotKernel::init(l, r);
51 
53 
54  return init_normalizer();
55 }
56 
58 {
59  if (!lhs || !rhs)
60  return;
61 
62  int32_t alen, blen;
63 
64  alen=((CDenseFeatures<float64_t>*) lhs)->get_num_features();
65  blen=((CDenseFeatures<float64_t>*) rhs)->get_num_features();
66 
67  REQUIRE(alen==blen, "Number of right and left hand features must be the "
68  "same\n")
69 
70  if (m_weights.vlen != alen)
71  {
73  m_weights.set_const(1.0);
74  }
75 
76  SG_DEBUG("Initialized weights for LinearARDKernel (%p).\n", this)
77 }
78 
80 {
81  if (i >= m_weights.vlen)
82  {
83  SG_ERROR("Index %i out of range for LinearARDKernel."\
84  "Number of features is %i.\n", i, m_weights.vlen);
85  }
86 
87  m_weights[i]=w;
88 }
89 
91 {
92  if (i >= m_weights.vlen)
93  {
94  SG_ERROR("Index %i out of range for LinearARDKernel."\
95  "Number of features is %i.\n", i, m_weights.vlen);
96  }
97 
98  return m_weights[i];
99 }
100 
101 float64_t CLinearARDKernel::compute(int32_t idx_a, int32_t idx_b)
102 {
103  REQUIRE(lhs && rhs, "Features not set!\n")
104 
105  SGVector<float64_t> avec=
106  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a);
107  SGVector<float64_t> bvec=
108  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b);
109 
110  REQUIRE(avec.vlen==bvec.vlen, "Number of right and left hand "
111  "features must be the same\n")
112 
113  float64_t result=0;
114 
115  for (index_t i = 0; i < avec.vlen; i++)
116  result += avec[i]*bvec[i]*m_weights[i]*m_weights[i];
117 
118  return result;
119 }
120 
122  const TParameter* param, index_t index)
123 {
124  REQUIRE(lhs && rhs, "Features not set!\n")
125 
126  if (!strcmp(param->m_name, "weights"))
127  {
128  SGMatrix<float64_t> derivative(num_lhs, num_rhs);
129 
130  for (index_t j=0; j<num_lhs; j++)
131  {
132  for (index_t k=0; k<num_rhs; k++)
133  {
134  SGVector<float64_t> avec=
135  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(j);
136  SGVector<float64_t> bvec=
137  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(k);
138 
139  REQUIRE(avec.vlen==bvec.vlen, "Number of right and left hand "
140  "features must be the same\n");
141 
142  derivative(j,k)=avec[index]*bvec[index]*m_weights[index];
143  }
144  }
145  return derivative;
146  }
147  else
148  {
149  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name);
150  return SGMatrix<float64_t>();
151  }
152 }

SHOGUN Machine Learning Toolbox - Documentation