SubrangeKernel.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Variant of WeightedSumKernel which works on subranges of Vector inputs
6  *
7  *
8  *
9  * \author S., O.Krause
10  * \date 2012
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 #ifndef SHARK_MODELS_KERNELS_SUBRANGE_KERNEL_H
36 #define SHARK_MODELS_KERNELS_SUBRANGE_KERNEL_H
37 
38 
40 namespace shark {
41 namespace detail{
42 /// \brief given two vectors of input x = (x_1,...,x_n), y = (y_1,...,y_n), a subrange 1<=k<l<=n and a kernel k, computes the result of
43 /// th subrange k((x_k,...x_l),(y_k,...,y_l))
44 template<class InputType>
45 class SubrangeKernelWrapper : public AbstractKernelFunction<InputType>{
46 private:
47  typedef AbstractKernelFunction<InputType> base_type;
48 public:
52 
53  SubrangeKernelWrapper(AbstractKernelFunction<InputType>* kernel,std::size_t start, std::size_t end)
54  :m_kernel(kernel),m_start(start),m_end(end){
55  if(kernel->hasFirstParameterDerivative())
57  if(kernel->hasFirstInputDerivative())
58  this->m_features|=base_type::HAS_FIRST_INPUT_DERIVATIVE;
59  }
60 
61  /// \brief From INameable: return the class name.
62  std::string name() const
63  { return "SubrangeKernelWrapper"; }
64 
65  RealVector parameterVector() const {
66  return m_kernel->parameterVector();
67  }
68 
69  void setParameterVector(RealVector const& newParameters) {
70  m_kernel->setParameterVector(newParameters);
71  }
72 
73  std::size_t numberOfParameters() const {
74  return m_kernel->numberOfParameters();
75  }
76 
77  ///\brief creates the internal state of the kernel
78  boost::shared_ptr<State> createState()const{
79  return m_kernel->createState();
80  }
81 
82  double eval(ConstInputReference x1, ConstInputReference x2) const{
83  return m_kernel->eval(blas::subrange(x1,m_start,m_end),blas::subrange(x2,m_start,m_end));
84  }
85 
86  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result, State& state) const{
87  m_kernel->eval(columns(batchX1,m_start,m_end),columns(batchX2,m_start,m_end),result,state);
88  }
89 
90  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result) const{
91  m_kernel->eval(columns(batchX1,m_start,m_end),columns(batchX2,m_start,m_end),result);
92  }
93 
95  ConstBatchInputReference batchX1,
96  ConstBatchInputReference batchX2,
97  RealMatrix const& coefficients,
98  State const& state,
99  RealVector& gradient
100  ) const{
101  m_kernel->weightedParameterDerivative(
102  columns(batchX1,m_start,m_end),
103  columns(batchX2,m_start,m_end),
104  coefficients,
105  state,
106  gradient
107  );
108  }
110  ConstBatchInputReference batchX1,
111  ConstBatchInputReference batchX2,
112  RealMatrix const& coefficientsX2,
113  State const& state,
114  BatchInputType& gradient
115  ) const{
116  BatchInputType temp(gradient.size1(),m_end-m_start);
117  m_kernel->weightedInputDerivative(
118  columns(batchX1,m_start,m_end),
119  columns(batchX2,m_start,m_end),
120  coefficientsX2,
121  state,
122  temp
123  );
124  ensure_size(gradient,batchX1.size1(),batchX2.size2());
125  gradient.clear();
126  noalias(columns(gradient,m_start,m_end)) = temp;
127  }
128 
129  //w don't need serializing here, this is done by the implementing Kernel
130  void read(InArchive& ar){
131  }
132  void write(OutArchive& ar) const{
133  }
134 
135 private:
136  AbstractKernelFunction<InputType>* m_kernel;
137  std::size_t m_start;
138  std::size_t m_end;
139 };
140 
141 template<class InputType>
142 class SubrangeKernelBase
143 {
144 public:
145 
146  template<class Kernels,class Ranges>
147  SubrangeKernelBase(Kernels const& kernels, Ranges const& ranges){
148  SIZE_CHECK(kernels.size() == ranges.size());
149  for(std::size_t i = 0; i != kernels.size(); ++i){
150  m_kernelWrappers.push_back(
151  SubrangeKernelWrapper<InputType>(kernels[i],ranges[i].first,ranges[i].second)
152  );
153  }
154  }
155 
156  std::vector<AbstractKernelFunction<InputType>* > makeKernelVector(){
157  std::vector<AbstractKernelFunction<InputType>* > kernels(m_kernelWrappers.size());
158  for(std::size_t i = 0; i != m_kernelWrappers.size(); ++i)
159  kernels[i] = & m_kernelWrappers[i];
160  return kernels;
161  }
162 
163  std::vector<SubrangeKernelWrapper <InputType> > m_kernelWrappers;
164 };
165 }
166 
167 /// \brief Weighted sum of kernel functions
168 ///
169 /// For a set of positive definite kernels \f$ k_1, \dots, k_n \f$
170 /// with positive coeffitients \f$ w_1, \dots, w_n \f$ the sum
171 /// \f[ \tilde k(x_1, x_2) := \sum_{i=1}^{n} w_i \cdot k_i(x_1, x_2) \f]
172 /// is again a positive definite kernel function. This still holds when
173 /// the sub-kernels only operate of a subset of features, that is, when
174 /// we have a direct sum kernel ( see e.g. the UCSC Technical Report UCSC-CRL-99-10:
175 /// Convolution Kernels on Discrete Structures by David Haussler ).
176 ///
177 /// This class is very similar to the #WeightedSumKernel , except that it assumes it's
178 /// inputs to be tuples of values \f$ x=(x_1,\dots, x_n) \f$ and we calculate the direct
179 /// sum of kernels
180 /// \f[ \tilde k(x, y) := \sum_{i=1}^{n} w_i \cdot k_i(x_i, y_i) \f]
181 ///
182 /// Internally, the weights are represented as \f$ w_i = \exp(\xi_i) \f$
183 /// to allow for unconstrained optimization.
184 ///
185 /// The result of the kernel evaluation is devided by the sum of the
186 /// kernel weights, so that in total, this amounts to fixing the sum
187 /// of the weights to one.
188 template<class InputType, class InnerKernel=WeightedSumKernel<InputType> >
190 : private detail::SubrangeKernelBase<InputType>//order is important!
191 , public InnerKernel
192 {
193 private:
194  typedef detail::SubrangeKernelBase<InputType> base_type1;
195 public:
196 
197  /// \brief From INameable: return the class name.
198  std::string name() const
199  { return "SubrangeKernel"; }
200 
201  template<class Kernels,class Ranges>
202  SubrangeKernel(Kernels const& kernels, Ranges const& ranges)
203  : base_type1(kernels,ranges)
204  , InnerKernel(base_type1::makeKernelVector()){}
205 };
206 
209 
210 }
211 #endif