A VAE Is a Learnable, Curved SVD (Almost)

SVD changes the basis of your data with straight axes from a formula. A VAE learns curved axes from training examples, then adds one penalty that lets it generate new data. A step-by-step path from one to the other.
Machine Learning
Deep Learning
GenAI
Author
Published

October 23, 2026

While reading about variational autoencoders (VAEs), I noticed that the setup sounds a lot like SVD. You take the training data, squeeze it into a small number of values, expand it back, and penalise the model when the result doesn’t match the input. That’s what a truncated SVD does too. The difference is that a VAE learns how to do it.

The idea holds up, with one correction. A neural network doesn’t learn basis vectors the way SVD finds them. It learns something closer to a curved coordinate system. The clearest way I found to see this is to start at SVD and change one thing at a time until you arrive at a VAE.

Step 1: SVD changes the basis

Every data point is a list of numbers in some basis. For a 28×28 image that’s 784 pixel values, one per pixel axis. SVD finds a better set of axes for the same data:

\[ X \approx U_k \Sigma_k V_k^\top \]

The rows of \(V_k^\top\) are the new axes (the right singular vectors). They are orthogonal and sorted by how much of the data’s variation they explain. Keep the top \(k\) and drop the rest.

Read it as an encoder and a decoder:

  • Encode: \(z = V_k^\top x\). Project the 784 numbers onto \(k\) axes and keep \(k\) numbers.
  • Decode: \(\hat{x} = V_k z\). Rebuild the image from those \(k\) numbers.

The Eckart–Young theorem says no other rank-\(k\) linear map gets a smaller squared reconstruction error. So SVD is the best possible compression, as long as the axes have to be straight. Nothing is trained here. The axes come straight out of a matrix factorisation.

Step 2: a linear autoencoder learns the same thing

Now build a tiny neural network with no activation functions: one linear layer down to \(k\) numbers, one linear layer back up, trained with squared error.

encoder = nn.Linear(784, k, bias=False)
decoder = nn.Linear(k, 784, bias=False)

x_hat = decoder(encoder(x))
loss = ((x_hat - x) ** 2).sum(dim=1).mean()

Baldi and Hornik showed in 1989 that the best solution for this network spans the same subspace as the top \(k\) singular vectors. Gradient descent rediscovers SVD.

So “SVD, but learnable” is literally true at this step. There’s one small difference: the learned axes don’t have to be orthogonal or sorted. Any mix of the top \(k\) directions reconstructs equally well, so the network lands on one of them. It finds the same flat subspace but without SVD’s neat ordering.

Step 3: add nonlinearity and the axes bend

Add activation functions and a few hidden layers, and the encoder and decoder can now bend. This is a plain autoencoder.

To see what that buys, I made a toy dataset: 2D points scattered along an arc. Both methods get to describe each point with one number.

Left: SVD keeps one straight axis, and every point gets projected onto it. Right: an autoencoder with a 1-number bottleneck learns a curve that follows the data. The grey lines connect each point to its reconstruction.

SVD’s best single axis is a straight line through the middle of the arc, so the ends get reconstructed badly. The autoencoder’s single number moves you along the arc. Mean squared error drops from 0.129 to 0.006.

This is where “learns a basis” stops being literal. A nonlinear decoder has no fixed basis vectors. Moving the latent number traces out a curve, and with \(k\) latent numbers it traces a curved \(k\)-dimensional surface inside the data space. If you zoom in far enough, any small patch of that surface looks flat, and there you get local axes again. But they change from place to place.

So the better picture is: SVD lays a straight grid over the data, and an autoencoder learns a bent one from the training examples.

Step 4: why a plain autoencoder can’t generate

If compression were the only goal, we could stop at step 3. But a VAE exists to generate new data: pick a point in the latent space, decode it, get a new image.

A plain autoencoder is bad at this. Nothing in its loss says where the codes should go. It can scatter the training points across the latent space with big gaps between them. Pick a random point in a gap and the decoder has never seen anything like it, so you get noise.

(The linear case has a fix too. Probabilistic PCA, from Tipping and Bishop, turns SVD into a model you can sample from by adding Gaussian noise. A VAE plays the same role for the curved version.)

Step 5: the VAE adds one penalty

A VAE changes two things.

1. The encoder outputs a small cloud instead of a point. For each input it predicts a mean \(\mu\) and a spread \(\sigma\) per latent number, and the decoder gets a random sample from that cloud:

h = encoder(x)
mu, log_var = to_mu(h), to_log_var(h)
z = mu + torch.exp(0.5 * log_var) * torch.randn_like(mu)   # sample near mu
x_hat = decoder(z)

Writing the sample as mu + sigma * noise (the reparameterisation trick) keeps it differentiable, so normal backprop still works.

2. The loss gets a second term.

\[ \mathcal{L} = \underbrace{\lVert x - \hat{x} \rVert^2}_{\text{reconstruction}} + \underbrace{\mathrm{KL}\big(\mathcal{N}(\mu, \sigma^2) \,\|\, \mathcal{N}(0, I)\big)}_{\text{stay near a standard Gaussian}} \]

recon = ((x_hat - x) ** 2).sum(dim=1)
kl = -0.5 * (1 + log_var - mu.pow(2) - log_var.exp()).sum(dim=1)
loss = (recon + kl).mean()

The two terms pull in opposite directions:

  • Reconstruction wants each input to have its own sharp, separate code.
  • KL wants every cloud to be wide and centred at zero, like a standard bell curve.

The result is a compromise. Codes are packed close together around zero, and the clouds overlap. Since the decoder is trained on random samples from these clouds, it has to give sensible outputs for every point in the region, not just the exact training codes. The gaps from step 4 get filled in.

Two things follow:

  • Sampling works. Draw \(z\) from a standard Gaussian, decode it, and you land in a part of the space the decoder knows.
  • Interpolation works. Walk in a straight line between the codes of two images and the decoded images change smoothly from one to the other.

The ladder in one table

SVD Linear autoencoder Autoencoder VAE
Axes Straight Straight Curved Curved
How they’re found Matrix factorisation Gradient descent Gradient descent Gradient descent
Code for one input A point A point A point A small Gaussian cloud
Loss (none, it’s exact) Reconstruction Reconstruction Reconstruction + KL
Can generate new data No No Badly Yes

Where the analogy breaks

The SVD picture helped me a lot, but a few things don’t carry over:

  • No ranking. SVD tells you which axis matters most. VAE latent dimensions come in no particular order, and some can end up unused.
  • No unique answer. SVD always gives the same result for the same data. A VAE depends on initialisation, architecture and training, and different runs give different latent spaces.
  • No guarantee of the best fit. Eckart–Young proves SVD is optimal among straight-axis methods. A VAE is only as good as its training.
  • VAE outputs are often blurry. The KL term trades some reconstruction quality for a well-organised latent space. That’s the price of being able to generate.

If you already know SVD, you’re closer to understanding VAEs than it might seem. Bend the axes, learn them from examples, and give the latent space a shape. That’s most of a VAE.

Sources

  • Eckart, Young. The approximation of one matrix by another of lower rank. Psychometrika, 1936.
  • Baldi, Hornik. Neural networks and principal component analysis: Learning from examples without local minima. Neural Networks, 1989.
  • Tipping, Bishop. Probabilistic Principal Component Analysis. Journal of the Royal Statistical Society B, 1999.
  • Kingma, Welling. Auto-Encoding Variational Bayes. arXiv:1312.6114, 2013.