mlpack
nystroem_method_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_NYSTROEM_METHOD_NYSTROEM_METHOD_IMPL_HPP
14 #define MLPACK_METHODS_NYSTROEM_METHOD_NYSTROEM_METHOD_IMPL_HPP
15 
16 // In case it hasn't been included yet.
17 #include "nystroem_method.hpp"
18 
19 namespace mlpack {
20 namespace kernel {
21 
22 template<typename KernelType, typename PointSelectionPolicy>
24  const arma::mat& data,
25  KernelType& kernel,
26  const size_t rank) :
27  data(data),
28  kernel(kernel),
29  rank(rank)
30 { }
31 
32 template<typename KernelType, typename PointSelectionPolicy>
34  const arma::mat* selectedData,
35  arma::mat& miniKernel,
36  arma::mat& semiKernel)
37 {
38  // Assemble mini-kernel matrix.
39  for (size_t i = 0; i < rank; ++i)
40  for (size_t j = 0; j < rank; ++j)
41  miniKernel(i, j) = kernel.Evaluate(selectedData->col(i),
42  selectedData->col(j));
43 
44  // Construct semi-kernel matrix with interactions between selected data and
45  // all points.
46  for (size_t i = 0; i < data.n_cols; ++i)
47  for (size_t j = 0; j < rank; ++j)
48  semiKernel(i, j) = kernel.Evaluate(data.col(i),
49  selectedData->col(j));
50  // Clean the memory.
51  delete selectedData;
52 }
53 
54 template<typename KernelType, typename PointSelectionPolicy>
56  const arma::Col<size_t>& selectedPoints,
57  arma::mat& miniKernel,
58  arma::mat& semiKernel)
59 {
60  // Assemble mini-kernel matrix.
61  for (size_t i = 0; i < rank; ++i)
62  {
63  for (size_t j = 0; j < rank; ++j)
64  {
65  miniKernel(i, j) = kernel.Evaluate(data.col(selectedPoints(i)),
66  data.col(selectedPoints(j)));
67  }
68  }
69 
70  // Construct semi-kernel matrix with interactions between selected points and
71  // all points.
72  for (size_t i = 0; i < data.n_cols; ++i)
73  for (size_t j = 0; j < rank; ++j)
74  semiKernel(i, j) = kernel.Evaluate(data.col(i),
75  data.col(selectedPoints(j)));
76 }
77 
78 template<typename KernelType, typename PointSelectionPolicy>
80 {
81  arma::mat miniKernel(rank, rank);
82  arma::mat semiKernel(data.n_cols, rank);
83 
84  GetKernelMatrix(PointSelectionPolicy::Select(data, rank), miniKernel,
85  semiKernel);
86 
87  // Singular value decomposition mini-kernel matrix.
88  arma::mat U, V;
89  arma::vec s;
90  arma::svd(U, s, V, miniKernel);
91 
92  // Construct the output matrix. We need to have special handling when
93  // miniKernel ended up being low-rank.
94  arma::mat normalization = arma::diagmat(1.0 / sqrt(s));
95  for (size_t i = 0; i < s.n_elem; ++i)
96  if (std::abs(s[i]) <= 1e-20)
97  normalization(i, i) = 0.0;
98 
99  output = semiKernel * U * normalization * V;
100 }
101 
102 } // namespace kernel
103 } // namespace mlpack
104 
105 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Apply(arma::mat &output)
Apply the low-rank factorization to obtain an output matrix G such that K&#39; = G * G^T.
Definition: nystroem_method_impl.hpp:79
NystroemMethod(const arma::mat &data, KernelType &kernel, const size_t rank)
Create the NystroemMethod object.
Definition: nystroem_method_impl.hpp:23
void GetKernelMatrix(const arma::mat *data, arma::mat &miniKernel, arma::mat &semiKernel)
Construct the kernel matrix with matrix that contains the selected points.
Definition: nystroem_method_impl.hpp:33