Loading...
Searching...
No Matches
gpr::RandomFourierFeatures Class Reference

Random Fourier Features for SE kernel approximation. More...

#include <gpr/ml/RandomFourierFeatures.h>

Public Member Functions

 RandomFourierFeatures (int n_features, double lengthscale, int input_dim, int seed=42)
 Construct and initialize RFF weights from a seeded RNG.
void setLengthscale (double l)
 Change the lengthscale and regenerate random weights.
Eigen::VectorXd transform (const Eigen::VectorXd &x) const
 Compute the feature vector phi(x) for a single input.
EigenMatrix transformBatch (const EigenMatrix &X) const
 Compute feature vectors for all rows of X.
EigenMatrix approximateKernel (const EigenMatrix &X) const
 Approximate the kernel matrix K ~ Phi * Phi^T.
int nFeatures () const
int inputDim () const
double lengthscale () const

Detailed Description

Random Fourier Features for SE kernel approximation.

The random weights W (n_features x input_dim) and biases b (n_features) are generated once from a seeded PRNG. The feature map is: phi(x)_i = sqrt(2/D) * cos(w_i^T x + b_i)

The approximate kernel is Phi * Phi^T where Phi has rows phi(x_j)^T.

Definition at line 36 of file RandomFourierFeatures.h.

Constructor & Destructor Documentation

◆ RandomFourierFeatures()

gpr::RandomFourierFeatures::RandomFourierFeatures ( int n_features,
double lengthscale,
int input_dim,
int seed = 42 )

Construct and initialize RFF weights from a seeded RNG.

Parameters
n_featuresNumber of random features D
lengthscaleKernel lengthscale l (w_i ~ N(0, 1/l^2))
input_dimDimensionality of input vectors
seedRNG seed for reproducibility

Definition at line 12 of file RandomFourierFeatures.cpp.

14 : n_features_(n_features),
15 lengthscale_(lengthscale),
16 input_dim_(input_dim),
17 seed_(seed),
18 W_(n_features, input_dim),
19 b_(n_features),
20 scale_factor_(std::sqrt(2.0 / n_features))
21{
22 generateWeights();
23}

Member Function Documentation

◆ approximateKernel()

EigenMatrix gpr::RandomFourierFeatures::approximateKernel ( const EigenMatrix & X) const

Approximate the kernel matrix K ~ Phi * Phi^T.

Parameters
XInput matrix (n_samples x input_dim)
Returns
Approximate kernel matrix (n_samples x n_samples)

Definition at line 66 of file RandomFourierFeatures.cpp.

67{
69 return Phi * Phi.transpose();
70}
EigenMatrix transformBatch(const EigenMatrix &X) const
Compute feature vectors for all rows of X.
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20

◆ inputDim()

int gpr::RandomFourierFeatures::inputDim ( ) const
inline

Definition at line 85 of file RandomFourierFeatures.h.

86 {
87 return input_dim_;
88 }

◆ lengthscale()

double gpr::RandomFourierFeatures::lengthscale ( ) const
inline

Definition at line 89 of file RandomFourierFeatures.h.

90 {
91 return lengthscale_;
92 }

◆ nFeatures()

int gpr::RandomFourierFeatures::nFeatures ( ) const
inline

Definition at line 81 of file RandomFourierFeatures.h.

82 {
83 return n_features_;
84 }

◆ setLengthscale()

void gpr::RandomFourierFeatures::setLengthscale ( double l)

Change the lengthscale and regenerate random weights.

The same seed is reused so the random structure is preserved (only the scale changes).

Definition at line 25 of file RandomFourierFeatures.cpp.

26{
27 lengthscale_ = l;
28 generateWeights();
29}

◆ transform()

Eigen::VectorXd gpr::RandomFourierFeatures::transform ( const Eigen::VectorXd & x) const

Compute the feature vector phi(x) for a single input.

Parameters
xInput vector of dimension input_dim
Returns
Feature vector of dimension n_features

Definition at line 50 of file RandomFourierFeatures.cpp.

51{
52 // phi(x)_i = sqrt(2/D) * cos(w_i^T x + b_i)
53 Eigen::VectorXd u = W_ * x + b_;
54 return scale_factor_ * u.array().cos().matrix();
55}
@ x
Definition Enums.h:28

◆ transformBatch()

EigenMatrix gpr::RandomFourierFeatures::transformBatch ( const EigenMatrix & X) const

Compute feature vectors for all rows of X.

Parameters
XInput matrix (n_samples x input_dim)
Returns
Feature matrix Phi (n_samples x n_features)

Definition at line 57 of file RandomFourierFeatures.cpp.

58{
59 // U = X * W^T + ones * b^T, shape (n_samples x n_features)
60 EigenMatrix U = X * W_.transpose();
61 U.rowwise() += b_.transpose();
62 // Phi = sqrt(2/D) * cos(U)
63 return scale_factor_ * U.array().cos().matrix();
64}

The documentation for this class was generated from the following files: