liveBook · Manning

Principal component analysis

As explained in Inside Deep Learning: Math, Algorithms, Models

Principal component analysis, a linear dimensionality-reduction method used here to project data into a lower-dimensional space.

Principal component analysis (PCA) reduces a feature vector from D dimensions to D′ dimensions, then approximately reconstructs it. Its encoder is a weight matrix W; its decoder uses Wᵀ, making PCA a linear autoencoder with shared weights. The reconstruction objective uses squared error, while an orthogonality constraint keeps the projection well behaved. section 7.1.1

Implement PCA in PyTorch by mapping the mathematical pieces directly to modules:

  • Create an encoder with nn.Flatten followed by nn.Linear(D, n, bias=False), where n is the bottleneck dimension. section 7.1.2
  • Reuse the encoder's weights in a TransposeLinear module to implement Wᵀ; give this layer a separate bias only if one is needed. section 7.1.2
  • Decode with the transposed layer and reshape the result back to the original input shape, such as (B, 1, 28, 28). section 7.1.2
  • Train with mean squared reconstruction error plus a penalty comparing WWᵀ with the identity matrix; orthogonal initialization can support the intended constraint. section 7.1.2

From PCA to a nonlinear autoencoder

Add Tanh after a linear bottleneck, remove weight sharing, and use a separate linear decoder. Train with ordinary MSE, then inspect two-dimensional encodings and reconstructed images to compare artifacts and overlap. section 7.1.5

linearLayer = nn.Linear(D, n, bias=False) #1 

pca_encoder =
   nn.Sequential(                         #2 
   nn.Flatten(), linearLayer, 
)

pca_decoder = nn.Sequential(              #3 

   TransposeLinear(linearLayer, bias=False), 
   View(-1, 1, 28, 28)                    #4 
) 
pca_model = nn.Sequential(                #5
   pca_encoder, 
   pca_decoder 
)

Shows the compact encoder, shared-weight decoder, and reshaping pipeline used to express PCA as a PyTorch model. section 7.1.2

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Derives PCA as a constrained linear autoencoder and implements its shared transpose weights, stopping before broader nonlinear extensions.
Examples
Uses 28×28 grayscale images, two-dimensional encodings, scatterplots, reconstructed images, and a one-nonlinearity variant.
What it assumes
Assumes familiarity with linear layers, neural-network modules, reconstruction loss, and basic matrix notation.
What you can do after
Build it from scratch in PyTorch with shared transpose weights, an orthogonality penalty, and a reconstruction-loss training loop.
How it connects
Connects dimensionality reduction with autoencoders, weight sharing, mean squared error, orthogonality regularization, and linear regression.

Also explained in

Compare how 10 books explain principal component analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.