TorchLean API

NN.Spec.Models.Pca

PCA (spec model) #

Principal Component Analysis is represented as a linear projection onto learned components, plus an explicit mean for centering.

This file models only the transform (and inverse transform), not the procedure that learns principal components from data.

PyTorch / ecosystem analogies:

References (background, not required to read the code):

structure Spec.PCASpec (α : Type) (inDim outDim : ) :

Parameters for PCA as a linear map plus centering.

We store:

  • components : outDim × inDim (rows are principal directions),
  • mean : inDim (for centering),
  • explained_variance : outDim (eigenvalues for the selected components).

This matches the typical PCA API: you can transform to outDim coordinates and inverse back to inDim.

Instances For
    def Spec.pcaForwardSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (input : Tensor α (Shape.dim inDim Shape.scalar)) :

    Forward pass: center and project: y = components · (x - mean).

    Instances For
      def Spec.pcaBatchedForwardSpec {α : Type} [Context α] {batch inDim outDim : } (m : PCASpec α inDim outDim) (input : Tensor α (Shape.dim batch (Shape.dim inDim Shape.scalar))) :

      Batched forward pass: apply pca_forward_spec to each row.

      Instances For
        def Spec.pcaInverseSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (reduced : Tensor α (Shape.dim outDim Shape.scalar)) :

        Inverse transform: reconstruct x ≈ componentsᵀ · y + mean.

        Instances For
          def Spec.pcaComponentsDerivSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (input : Tensor α (Shape.dim inDim Shape.scalar)) (grad_output : Tensor α (Shape.dim outDim Shape.scalar)) :

          VJP contribution for components: outer product dL/dy ⊗ (x - mean).

          Instances For
            def Spec.pcaMeanDerivSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (grad_output : Tensor α (Shape.dim outDim Shape.scalar)) :

            VJP contribution for mean: dL/dmean = -componentsᵀ · dL/dy.

            Instances For
              def Spec.pcaInputDerivSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (grad_output : Tensor α (Shape.dim outDim Shape.scalar)) :

              VJP contribution for input: dL/dx = componentsᵀ · dL/dy.

              Instances For
                def Spec.pcaBackwardSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (input : Tensor α (Shape.dim inDim Shape.scalar)) (grad_output : Tensor α (Shape.dim outDim Shape.scalar)) :

                Full backward pass returning (dComponents, dMean, dInput).

                Instances For
                  def Spec.pcaFitSpec {α : Type} [Context α] {nSamples inDim : } (data : Tensor α (Shape.dim nSamples (Shape.dim inDim Shape.scalar))) (nComponents : ) (h1 : 0 < nComponents) (h2 : nComponents inDim) (h3 : nSamples 0) :
                  PCASpec α inDim nComponents

                  Fit PCA using the (scaled) covariance matrix and eigendecomposition.

                  Algorithm:

                  1. compute the mean and center the data,
                  2. form the covariance matrix C = (1/(n-1)) Xᵀ X,
                  3. compute eigenpairs of C,
                  4. take the top nComponents eigenvectors,
                  5. orient eigenvectors deterministically (sign convention) so results are reproducible.

                  Note: this is a spec/reference implementation. In numerical libraries, PCA is often implemented via SVD for stability and performance.

                  Instances For
                    def Spec.pcaTransformSpec {α : Type} [Context α] {nSamples inDim outDim : } (m : PCASpec α inDim outDim) (data : Tensor α (Shape.dim nSamples (Shape.dim inDim Shape.scalar))) :
                    Tensor α (Shape.dim nSamples (Shape.dim outDim Shape.scalar))

                    Apply a fitted PCA transform to a batch of samples.

                    Instances For
                      def Spec.pcaReconstructionErrorSpec {α : Type} [Context α] {inDim outDim : } (m : PCASpec α inDim outDim) (input : Tensor α (Shape.dim inDim Shape.scalar)) (h : inDim 0) :
                      α

                      Reconstruction error: ||x - inverse(transform(x))||_2^2 (sum of squared coordinates).

                      PyTorch analogy: torch.sum((x - x_hat) ** 2).

                      Instances For
                        def Spec.pcaExplainedVarianceRatioSpec {α : Type} {inDim outDim : } (m : PCASpec α inDim outDim) :

                        Explained variance (eigenvalues of the selected components).

                        If you want the ratio (normalized to sum to 1), you need to divide by the total variance of the original data; this file keeps just the raw eigenvalues.

                        Instances For
                          def Spec.pcaCumulativeVarianceSpec {α : Type} [Add α] [Zero α] {inDim outDim : } (m : PCASpec α inDim outDim) :

                          Cumulative explained variance (prefix sums of explained_variance).

                          Instances For
                            @[irreducible]
                            def Spec.pcaCumulativeVarianceSpec.sum_to_index {α : Type} [Add α] {outDim : } (f : Fin outDimTensor α Shape.scalar) (i : Fin outDim) (j : ) (acc : α) :
                            α
                            Instances For