class gpr::RandomFourierFeatures

Overview

Random Fourier Features for SE kernel approximation. More…

#include <RandomFourierFeatures.h>
 
class RandomFourierFeatures {
public:
    // construction
 
    RandomFourierFeatures(int n_features, double lengthscale, int input_dim, int seed = 42);
 
    // methods
 
    void setLengthscale(double l);
    Eigen::VectorXd transform(const Eigen::VectorXd& x) const;
    EigenMatrix transformBatch(const EigenMatrix& X) const;
    EigenMatrix approximateKernel(const EigenMatrix& X) const;
    int nFeatures() const;
    int inputDim() const;
    double lengthscale() const;
};

Detailed Documentation

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.

Construction

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

Construct and initialize RFF weights from a seeded RNG.

Parameters:

n_features

Number of random features D

lengthscale

Kernel lengthscale l (w_i ~ N(0, 1/l^2))

input_dim

Dimensionality of input vectors

seed

RNG seed for reproducibility

Methods

void 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).

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

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

Parameters:

x

Input vector of dimension input_dim

Returns:

Feature vector of dimension n_features

EigenMatrix transformBatch(const EigenMatrix& X) const

Compute feature vectors for all rows of X.

Parameters:

X

Input matrix (n_samples x input_dim)

Returns:

Feature matrix Phi (n_samples x n_features)

EigenMatrix approximateKernel(const EigenMatrix& X) const

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

Parameters:

X

Input matrix (n_samples x input_dim)

Returns:

Approximate kernel matrix (n_samples x n_samples)