Learning Curve Plus Plus (LCPP)
data::Gram< KERNEL, T > Struct Template Reference

Public Member Functions

 Gram ()
 Default constructor.
 
template<typename... Ts>
 Gram (Ts &&... args)
 Construct and initialize kernel with arbitrary arguments. More...
 
arma::Mat< T > GetMatrix2 (const arma::Mat< T > &input1, const arma::Mat< T > &input2) const
 Compute Gram matrix for row-major ordered data. More...
 
arma::Mat< T > GetMatrix (const arma::Mat< T > &input1, const arma::Mat< T > &input2) const
 Compute Gram matrix for column-major ordered data. More...
 
arma::Mat< T > GetApprox (const arma::Mat< T > &input1, const arma::Mat< T > &input2, size_t k) const
 Compute an approximate Gram matrix using the Nyström method. More...
 
arma::Mat< T > GetMatrix (const arma::Mat< T > &input1) const
 Compute Gram matrix of a dataset with itself (column-major). More...
 
template<typename Archive >
void serialize (Archive &ar, const unsigned int)
 

Public Attributes

KERNEL kernel_
 

Detailed Description

template<class KERNEL, class T = DTYPE>
struct data::Gram< KERNEL, T >

Definition at line 136 of file data.h.

Constructor & Destructor Documentation

◆ Gram()

template<class KERNEL , class T = DTYPE>
template<typename... Ts>
data::Gram< KERNEL, T >::Gram ( Ts &&...  args)
inline

Construct and initialize kernel with arbitrary arguments.

Template Parameters
TsArgument types for the kernel constructor.
Parameters
argsArguments to forward to the kernel constructor.

Definition at line 147 of file data.h.

147 : kernel_(args...) {}

Member Function Documentation

◆ GetApprox()

template<class KERNEL , class T = DTYPE>
arma::Mat<T> data::Gram< KERNEL, T >::GetApprox ( const arma::Mat< T > &  input1,
const arma::Mat< T > &  input2,
size_t  k 
) const
inline

Compute an approximate Gram matrix using the Nyström method.

Selects k random landmark points, computes their kernel matrix W, and uses it to approximate the full kernel matrix: K_approx = C * pinv(W) * C^T

Parameters
input1First dataset (columns are samples).
input2Second dataset (columns are samples).
kNumber of landmark points to sample.
Returns
Approximated Gram matrix.

Definition at line 204 of file data.h.

207  {
208  size_t n_samples = input1.n_rows;
209  arma::uvec indices = arma::randi<arma::uvec>(k, arma::distr_param(0, n_samples - 1));
210  arma::Mat<T> landmarks = input1.cols(indices);
211 
212  arma::Mat<T> W = this->GetMatrix(landmarks, landmarks);
213  arma::Mat<T> C = this->GetMatrix(input1, landmarks);
214  arma::Mat<T> W_pinv = arma::pinv(W);
215 
216  return C * W_pinv * C.t();
217  }
arma::Mat< T > GetMatrix(const arma::Mat< T > &input1, const arma::Mat< T > &input2) const
Compute Gram matrix for column-major ordered data.
Definition: data.h:178

References data::Gram< KERNEL, T >::GetMatrix().

+ Here is the call graph for this function:

◆ GetMatrix() [1/2]

template<class KERNEL , class T = DTYPE>
arma::Mat<T> data::Gram< KERNEL, T >::GetMatrix ( const arma::Mat< T > &  input1) const
inline

Compute Gram matrix of a dataset with itself (column-major).

Parameters
input1Dataset (columns are samples).
Returns
Symmetric Gram matrix of size input1.n_cols × input1.n_cols.

Definition at line 224 of file data.h.

225  {
226  return GetMatrix(input1, input1);
227  }

References data::Gram< KERNEL, T >::GetMatrix().

+ Here is the call graph for this function:

◆ GetMatrix() [2/2]

template<class KERNEL , class T = DTYPE>
arma::Mat<T> data::Gram< KERNEL, T >::GetMatrix ( const arma::Mat< T > &  input1,
const arma::Mat< T > &  input2 
) const
inline

Compute Gram matrix for column-major ordered data.

Parameters
input1First dataset (columns are samples).
input2Second dataset (columns are samples).
Returns
Gram matrix of size input1.n_cols × input2.n_cols.

Definition at line 178 of file data.h.

180  {
181  arma::Mat<T> matrix(input1.n_cols, input2.n_cols);
182 
183  #pragma omp parallel for collapse(2)
184  for (int i = 0; i < int(input1.n_cols); i++)
185  for (int j = 0; j < int(input2.n_cols); j++)
186  matrix(i,j) = kernel_.Evaluate(input1.col(i).eval(),
187  input2.col(j).eval());
188 
189  return matrix;
190  }

Referenced by data::Gram< KERNEL, T >::GetApprox(), and data::Gram< KERNEL, T >::GetMatrix().

+ Here is the caller graph for this function:

◆ GetMatrix2()

template<class KERNEL , class T = DTYPE>
arma::Mat<T> data::Gram< KERNEL, T >::GetMatrix2 ( const arma::Mat< T > &  input1,
const arma::Mat< T > &  input2 
) const
inline

Compute Gram matrix for row-major ordered data.

Parameters
input1First dataset (rows are samples).
input2Second dataset (rows are samples).
Returns
Gram matrix of size input1.n_rows × input2.n_rows.

Definition at line 158 of file data.h.

160  {
161  arma::Mat<T> matrix(input1.n_rows, input2.n_rows);
162 
163  #pragma omp parallel for collapse(2)
164  for (int i = 0; i < int(input1.n_rows); i++)
165  for (int j = 0; j < int(input2.n_rows); j++)
166  matrix(i,j) = kernel_.Evaluate(input1.row(i).eval(),
167  input2.row(j).eval());
168 
169  return matrix;
170  }

◆ serialize()

template<class KERNEL , class T = DTYPE>
template<typename Archive >
void data::Gram< KERNEL, T >::serialize ( Archive &  ar,
const unsigned int   
)
inline

Serialize the model.

Definition at line 232 of file data.h.

233  {
234  ar ( cereal::make_nvp("kernel",kernel_) );
235  }

The documentation for this struct was generated from the following file: