PCA

This section contains some utilties for performing PCA in PyTorch. It is mostly a PyTorch clone of the sklearn implementation with some useful features for our workflow.

PCA

class svise.pca.PCA(evecs: Tensor | None = None, rank: Tensor | None = None, mean: Tensor | None = None, scale: Tensor | None = None)[source]

Class for performing PCA with automatic selection of the rank of the using the method from https://proceedings.neurips.cc/paper/2000/file/7503cfacd12053d309b6bed5c89de212-Paper.pdf.

classmethod create(y: Tensor, percent_cutoff: float = 0.95, max_evecs: int = 30, rescale: bool = True) Tuple[PCA, Tensor][source]

Initialize a pca decomposition and return the transformed code vectors

Parameters:
  • y (Tensor) – (N,D) input data

  • percent_cutoff (float) – what percentage to use as a cut off when getting a rough estimate for the rank of the covariance matrix

  • max_evecs (int) – maximum number of eigenvectors to compute

  • rescale (bool) – whether to rescale the code vectors so that variance of the max is 1

Returns:

pca_model, z

decode(z: Tensor) Tensor[source]

Decode code variables (bs, n_components) -> (bs, d )

Parameters:

z (Tensor) – code variables

Returns:

approximation to y = decode(encode(y))

encode(y: Tensor) Tensor[source]

Encode a set of inputs (bs, d) into the lower dimensional space

Parameters:

y (Tensor) – inputs

Returns:

reduced dimension inputs

forward(z: Tensor) Tensor[source]

Alias for decode

init_buffers(n_dim: int = 0, n_components: int = 0)[source]

Init empty buffers (useful for loading from memory when size of buffers might be unknown )

load_state_dict(state_dict: Mapping[str, Any], strict: bool = True)[source]

Standard call to load_state_dict where buffers are first made to be the correct size

transform_stdev(stdev: Tensor)[source]

Transform the standard deviation of the input data into a variance on hte reduced order space

Parameters:

stdev (Tensor) –

  1. standard deviation of the input data

Retruns:

(Tensor): (n_components, n_components) variance of the reduced order space