Struct faiss::PCAMatrix

struct PCAMatrix : public faiss::LinearTransform

Applies a principal component analysis on a set of vectors, with optionally whitening and random rotation.

Public Functions

explicit PCAMatrix(int d_in = 0, int d_out = 0, float eigen_power = 0, bool random_rotation = false)
virtual void train(idx_t n, const float *x) override

train on n vectors. If n < d_in then the eigenvector matrix will be completed with 0s

void copy_from(const PCAMatrix &other)

copy pre-trained PCA matrix

void prepare_Ab()

called after mean, PCAMat and eigenvalues are computed

virtual void apply_noalloc(idx_t n, const float *x, float *xt) const override

same as apply, but result is pre-allocated

void transform_transpose(idx_t n, const float *y, float *x) const

compute x = A^T * (x - b) is reverse transform if A has orthonormal lines

virtual void reverse_transform(idx_t n, const float *xt, float *x) const override

works only if is_orthonormal

void set_is_orthonormal()

compute A^T * A to set the is_orthonormal flag

void print_if_verbose(const char *name, const std::vector<double> &mat, int n, int d) const
virtual void check_identical(const VectorTransform &other) const override
float *apply(idx_t n, const float *x) const

apply the transformation and return the result in an allocated pointer

Parameters:
  • n – number of vectors to transform

  • x – input vectors, size n * d_in

Returns:

output vectors, size n * d_out

Public Members

float eigen_power

after transformation the components are multiplied by eigenvalues^eigen_power

=0: no whitening =-0.5: full whitening

float epsilon

value added to eigenvalues to avoid division by 0 when whitening

bool random_rotation

random rotation after PCA

size_t max_points_per_d

ratio between # training vectors and dimension

int balanced_bins

try to distribute output eigenvectors in this many bins

std::vector<float> mean

Mean, size d_in.

std::vector<float> eigenvalues

eigenvalues of covariance matrix (= squared singular values)

std::vector<float> PCAMat

PCA matrix, size d_in * d_in.

bool have_bias
bool is_orthonormal

! whether to use the bias term

check if matrix A is orthonormal (enables reverse_transform)

std::vector<float> A

Transformation matrix, size d_out * d_in.

std::vector<float> b

bias vector, size d_out

bool verbose
int d_in
int d_out

! input dimension

bool is_trained

set if the VectorTransform does not require training, or if training is done already