Learning Curve Plus Plus (LCPP)
kernelridge_impl.h
Go to the documentation of this file.
1 /**
2  * @file kernelridge_impl.h
3  * @author Ozgur Taylan Turan
4  *
5  * Simple Kernelized Ridge Regression & Kernel Regression(Smoothing) &
6  * Semi-Parametric Kernel Ridge Regression
7  *
8  */
9 #ifndef KERNELRIDGE_IMPL_H
10 #define KERNELRIDGE_IMPL_H
11 
12 namespace algo {
13 namespace regression {
14 
15 //-----------------------------------------------------------------------------
16 // Kernel Ridge Regression
17 //-----------------------------------------------------------------------------
18 template<class KERNEL, class T>
19 template<class... Ts>
20 KernelRidge<KERNEL,T>::KernelRidge ( const arma::Mat<T>& inputs,
21  const arma::Row<T>& labels,
22  const double& lambda,
23  const Ts&... args ) :
24  cov_(args...), lambda_(lambda)
25 {
26  Train(inputs, labels);
27 }
28 ///////////////////////////////////////////////////////////////////////////////
29 template<class KERNEL, class T>
30 template<class... Ts>
31 KernelRidge<KERNEL,T>::KernelRidge ( const arma::Mat<T>& inputs,
32  const arma::Row<T>& labels ) :
33  cov_( ), lambda_(0.)
34 {
35  Train(inputs, labels);
36 }
37 ///////////////////////////////////////////////////////////////////////////////
38 template<class KERNEL, class T>
39 void KernelRidge<KERNEL,T>::Train ( const arma::Mat<T>& inputs,
40  const arma::Row<T>& labels )
41 {
42  train_inp_ = inputs;
43 
44  arma::Mat<T> k_xx = cov_.GetMatrix(train_inp_,train_inp_);
45 
46  arma::Mat<T> KLambda = k_xx+
47  (lambda_ + 1.e-6) * arma::eye<arma::Mat<T>>(k_xx.n_rows, k_xx.n_rows);
48 
49  parameters_ =
50  arma::conv_to<arma::Row<T>>::from(arma::solve(KLambda, labels.t()));
51 }
52 ///////////////////////////////////////////////////////////////////////////////
53 template<class KERNEL, class T>
54 void KernelRidge<KERNEL,T>::Predict ( const arma::Mat<T>& inputs,
55  arma::Row<T>& labels ) const
56 {
57 
58  arma::Mat<T> k_xpx = cov_.GetMatrix(train_inp_,inputs);
59  labels = (parameters_ * k_xpx );
60 }
61 ///////////////////////////////////////////////////////////////////////////////
62 template<class KERNEL, class T>
63 T KernelRidge<KERNEL,T>::ComputeError ( const arma::Mat<T>& inputs,
64  const arma::Row<T>& labels ) const
65 {
66  arma::Row<T> temp;
67  Predict(inputs, temp);
68  const size_t n_points = inputs.n_cols;
69 
70  temp = labels - temp;
71 
72  const T cost = (arma::dot(temp, temp) / n_points);
73 
74  return cost;
75 }
76 
77 //-----------------------------------------------------------------------------
78 // Kernel Regression
79 //-----------------------------------------------------------------------------
80 template<class KERNEL, class T>
81 template<class... Ts>
82 Kernel<KERNEL,T>::Kernel ( const arma::Mat<T>& inputs,
83  const arma::Row<T>& labels,
84  const Ts&... args ) : cov_(args...)
85 {
86  Train(inputs, labels);
87 }
88 ///////////////////////////////////////////////////////////////////////////////
89 template<class KERNEL, class T>
90 void Kernel<KERNEL,T>::Train ( const arma::Mat<T>& inputs,
91  const arma::Row<T>& labels )
92 {
93  train_inp_ = inputs;
94  train_lab_ = labels;
95 }
96 ///////////////////////////////////////////////////////////////////////////////
97 template<class KERNEL, class T>
98 void Kernel<KERNEL,T>::Predict ( const arma::Mat<T>& inputs,
99  arma::Row<T>& labels ) const
100 {
101  arma::Mat<T> sim = cov_.GetMatrix(train_inp_, inputs);
102  const size_t N = sim.n_rows;
103  for(size_t i=0; i<N; i++)
104  {
105  sim.row(i) /= arma::sum(sim,0);
106  }
107  labels = train_lab_ * sim;
108 }
109 ///////////////////////////////////////////////////////////////////////////////
110 template<class KERNEL, class T>
111 T Kernel<KERNEL,T>::ComputeError ( const arma::Mat<T>& inputs,
112  const arma::Row<T>& labels ) const
113 {
114  arma::Row<T> temp;
115  Predict(inputs, temp);
116  const size_t n_points = inputs.n_cols;
117 
118  temp = labels - temp;
119 
120  const T cost = (arma::dot(temp, temp) / n_points);
121 
122  return cost;
123 }
124 
125 //-----------------------------------------------------------------------------
126 // Semi-Parametric Kernel Ridge Regression with Mean addition
127 //-----------------------------------------------------------------------------
128 template<class KERNEL,class FUNC,class T>
129 template<class... Ts>
131 SemiParamKernelRidge ( const arma::Mat<T>& inputs,
132  const arma::Row<T>& labels,
133  const T lambda,
134  const size_t num_funcs,
135  const Ts&... args ) :
136  cov_(args...), lambda_(lambda), M_(num_funcs), perc_(0.), func_(num_funcs)
137 {
138  Train(inputs, labels);
139 }
140 ///////////////////////////////////////////////////////////////////////////////
141 template<class KERNEL,class FUNC,class T>
142 template<class... Ts>
144 SemiParamKernelRidge ( const arma::Mat<T>& inputs,
145  const arma::Row<T>& labels,
146  const T lambda,
147  const T perc,
148  const Ts&... args ) :
149 
150  cov_(args...), lambda_(lambda), M_(0), perc_(perc), func_(perc)
151 {
152  if (perc > 1)
153  {
154  M_ = perc;
155  perc_ = 0.;
156  }
157  Train(inputs, labels);
158 }
159 ///////////////////////////////////////////////////////////////////////////////
160 template<class KERNEL,class FUNC,class T>
161 void SemiParamKernelRidge<KERNEL,FUNC,T>::Train ( const arma::Mat<T>& inputs,
162  const arma::Row<T>& labels )
163 {
164  train_inp_ = inputs;
165  N_ = train_inp_.n_cols;
166 
167  psi_ = func_.Predict(inputs);
168 
169  if (M_ == 0)
170  M_ = func_.GetM();
171 
172  arma::Mat<T> k_xx = cov_.GetMatrix(train_inp_,train_inp_);
173 
174  arma::Mat<T> K = k_xx +
175  (1.e-8) * arma::eye<arma::Mat<T>>(k_xx.n_rows, k_xx.n_rows);
176 
177  arma::Mat<T> A = arma::join_rows(K, psi_.t());
178  arma::Mat<T> B = arma::join_cols(arma::join_rows(K,
179  arma::zeros<arma::Mat<T>>(N_,M_)),
180  arma::zeros<arma::Mat<T>>(M_,N_+M_));
181 
182  parameters_ = arma::conv_to<arma::Row<T>>::from(
183  arma::solve(A.t() * A + lambda_ * B.t(), A.t() * (labels.t()-
184  func_.Mean(inputs).t())));
185 
186 }
187 ///////////////////////////////////////////////////////////////////////////////
188 template<class KERNEL,class FUNC,class T>
189 void SemiParamKernelRidge<KERNEL,FUNC,T>::Predict ( const arma::Mat<T>& inputs,
190  arma::Row<T>& labels )
191 {
192  arma::Mat<T> K = cov_.GetMatrix(inputs,train_inp_);
193  arma::Mat<T> psi = func_.Predict(inputs);
194  arma::Mat<T> A = arma::join_rows(K, psi.t());
195  labels = (A * parameters_.t()).t() + func_.Mean(inputs);
196 }
197 ///////////////////////////////////////////////////////////////////////////////
198 template<class KERNEL,class FUNC,class T>
200  ( const arma::Mat<T>& inputs,
201  const arma::Row<T>& labels )
202 {
203  arma::Row<T> temp;
204  Predict(inputs, temp);
205  const size_t n_points = inputs.n_cols;
206 
207  temp = labels - temp;
208 
209  const T cost = (arma::dot(temp, temp) / n_points);
210 
211  return cost;
212 }
213 ///////////////////////////////////////////////////////////////////////////////
214 } // namespace regression
215 } // namespace algo
216 #endif
KernelRidge(const arma::Mat< T > &inputs, const arma::Row< T > &labels, const double &lambda, const Ts &... args)
T ComputeError(const arma::Mat< T > &points, const arma::Row< T > &responses) const
void Predict(const arma::Mat< T > &inputs, arma::Row< T > &labels) const
void Train(const arma::Mat< T > &inputs, const arma::Row< T > &labels)
T ComputeError(const arma::Mat< T > &points, const arma::Row< T > &responses) const
void Train(const arma::Mat< T > &inputs, const arma::Row< T > &labels)
Kernel(const arma::Mat< T > &inputs, const arma::Row< T > &labels, const Ts &... args)
void Predict(const arma::Mat< T > &inputs, arma::Row< T > &labels) const
void Predict(const arma::Mat< T > &inputs, arma::Row< T > &labels)
void Train(const arma::Mat< T > &inputs, const arma::Row< T > &labels)
SemiParamKernelRidge(const arma::Mat< T > &inputs, const arma::Row< T > &labels, const T lambda, const size_t num_funcs, const Ts &... args)
T ComputeError(const arma::Mat< T > &points, const arma::Row< T > &responses)