BayesOpt
kernel_atomic.hpp
Go to the documentation of this file.
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 #ifndef _KERNEL_ATOMIC_HPP_
25 #define _KERNEL_ATOMIC_HPP_
26 
27 #include <valarray>
28 #include "kernel_functors.hpp"
29 #include "ublas_elementwise.hpp"
30 
31 namespace bayesopt
32 {
33 
36 
37 
39  class AtomicKernel : public Kernel
40  {
41  public:
42  virtual void init(size_t input_dim)
43  { n_inputs = input_dim; };
44 
45  void setHyperParameters(const vectord &theta)
46  {
47  if(theta.size() != n_params)
48  {
49  throw std::invalid_argument("Wrong number of kernel hyperparameters");
50  }
51  params = theta; //TODO: To make enough space. Make it more efficient.
52  std::transform(theta.begin(), theta.end(), params.begin(), (double (*)(double)) exp);
53  };
54 
55  vectord getHyperParameters()
56  {
57  vectord theta(params.size());
58  std::transform(params.begin(), params.end(), theta.begin(), (double (*)(double)) log);
59  return theta;
60  };
61  size_t nHyperParameters() {return n_params;};
62 
63  virtual ~AtomicKernel(){};
64 
65  protected:
66  size_t n_params;
67  vectord params;
68  };
69 
71 
73  class ISOkernel : public AtomicKernel
74  {
75  public:
76  virtual ~ISOkernel(){};
77 
78  protected:
79  inline double computeWeightedNorm2(const vectord &x1, const vectord &x2)
80  {
81  assert(n_inputs == x1.size());
82  assert(x1.size() == x2.size());
83  return norm_2(x1-x2)/params(0);
84  };
85  };
86 
90  class ARDkernel : public AtomicKernel
91  {
92  public:
93  virtual ~ARDkernel(){};
94 
95  protected:
96  inline double computeWeightedNorm2(const vectord &x1, const vectord &x2)
97  {
98  assert(n_inputs == x1.size());
99  assert(x1.size() == x2.size());
100  assert(x1.size() == params.size());
101 
102  vectord xd = x1-x2;
103  vectord r = utils::ublas_elementwise_div(xd, params);
104  return norm_2(r);
105  };
106  };
107 
109 
110 } //namespace bayesopt
111 
112 #endif
Kernel (covariance) functions.
Abstract class for isotropic kernel functors.
Namespace of the library interface.
Definition: using.dox:1
Elementwise operations for ublas vector/matrix.
Abstract class for anisotropic kernel functors using ARD (Automatic Relevance Determination) ...
Interface for kernel functors.
v1 ublas_elementwise_div(const v1 &a, const v2 &b)
Computes the elementwise division of two vectors or matrices.
Abstract class for an atomic kernel.