Five constructed points lie exactly on a parabola in the plane, so one number, the position along the curve, determines each of them. Yet the best one-dimensional summary that PCA can give, a straight line, passes through none of the five points. This lesson builds an encoder that compresses each point to one number and a decoder that rebuilds the point from it, measures the rebuilding error on points that were not used for fitting, and compares a curved decoder with PCA. It also shows why a small reconstruction error is not, by itself, evidence of a useful representation.
Key terms
Autoencoder, encoder and decoder
Definition An autoencoder consists of an encoder , which maps an input in to a code in , and a decoder , which maps the code back to a reconstruction in . Both are trained together to make the reconstruction close to the input itself, by minimizing the average reconstruction loss over the training inputs,
usually by gradient descent with backpropagation. No labels are needed, because each input is its own target. Any preprocessing, such as centering or scaling, is fitted on the training inputs, the architecture and settings are chosen by reconstruction error on validation inputs, and test inputs are used once at the end.
In plain words The encoder writes a short summary of each input and the decoder tries to rebuild the input from that summary alone. The model learns what to put into the summary only through the score of the rebuild.
Example The constructed training points are for , with dimensionless coordinates. A deliberately tiny autoencoder has the encoder , which reads only the first coordinate, and the decoder , with trainable parameters , , and . Squaring is its nonlinearity, so this model has a strong built-in preference for parabolas and is not a general-purpose autoencoder. With errors and , written and for point , the training loss is the mean over the five points of . The parameter affects both outputs, so its gradient adds two paths,
Starting from , , and , 4,000 gradient descent steps with step size 0.05 end at , , and within of 0. Then and , so the decoder returns up to rounding error.
Bottleneck and latent representation
Definition The latent representation, or code, of an input is the encoder’s output . A bottleneck is a code layer of smaller dimension than the input, , and an autoencoder with one is called undercomplete. When and the layers are flexible enough, copying is available. For example, the linear encoder that appends zeros to and the linear decoder that drops them reproduce every input exactly. The reconstruction objective then gives no reason to prefer any other code over plain copying, so such an overcomplete autoencoder relies on another constraint, such as corrupted inputs, a sparsity penalty on the code or a weight penalty, to discourage the identity. Codes are also not unique. If is any invertible map of to itself, the encoder and the decoder give exactly the same reconstructions as and .
In plain words The bottleneck is the narrow point that forces a summary, and the code is the summary. A bottleneck limits how much passes through, but the reconstruction objective decides what passes. Because many different codes rebuild the inputs equally well, a single latent coordinate has no fixed meaning merely because the reconstructions are good.
Example The curved autoencoder above has and . In the run described, the point with gets the code . Doubling , halving and dividing by 4 leaves and unchanged, so every reconstruction stays the same, while the code of the same point becomes about . Neither code is more correct than the other.
Reconstruction loss
Definition A reconstruction loss measures how far is from . For real-valued inputs the usual choice is squared error, reduced in one of several ways that differ by constant factors,
the sum of squared errors of one example and the mean squared error per coordinate, and half of either is also common because it simplifies derivatives. A constant factor leaves the minimizer unchanged but scales the gradients and the reported number, so the reduction has to be stated. Error that the code cannot predict sets a floor. Under squared loss, the best decoder output for a part of the input given the code is its conditional mean, and the expected conditional variance remains as error that no decoder can remove. Training loss describes fit to the training inputs, and only held-out inputs show how well new inputs are rebuilt.
In plain words Reconstruction loss scores how well the rebuild matches the original, in squared units of the input. Zero is not a reachable target when the inputs carry noise that the code does not keep, and a low score on training inputs says nothing by itself about new inputs.
Example An input rebuilt as has errors and , so its SSE is and its MSE per coordinate is . Half the SSE is also , because , which is why the training loss of the curved autoencoder equals its MSE per coordinate. If the second coordinate of the parabola data were plus independent noise with mean 0 and variance , a code that keeps only could at best rebuild , and the expected MSE per coordinate on new points could not fall below .
Linear autoencoders and PCA
Definition Let the training inputs in be centered at their mean, with covariance and eigenvalues . A linear autoencoder with code size has encoder and decoder , with of size and of size , and is trained on
The smallest possible value is . If , every pair that attains it gives each training input its PCA reconstruction with components. If in addition is invertible, then , where the columns of are orthonormal eigenvectors for the largest eigenvalues, and the code is for some invertible matrix . With bias terms and uncentered inputs, the same holds for the inputs minus their training mean, and the code may also be shifted by a constant vector. The theorem describes the minimum, and a training run has to reach it.
In plain words A linear autoencoder can do no better than PCA, and at its best it finds the same subspace, the one PCA with SVD and reconstruction projects onto. Its code is an invertible linear mixture of the principal component scores, so its coordinates need not equal those scores, need not be uncorrelated and need not be ordered by variance. When , even the subspace is not determined by the data.
Example For the five parabola points the mean is , and with denominator the covariance is
since the symmetric values of make the cross term equal to 0. The larger eigenvalue belongs to the horizontal direction, so the one-component PCA reconstruction of is , and the smallest average SSE is , or per coordinate. Here is invertible and , so a linear autoencoder with that reaches this minimum on the centered points has a code equal to a nonzero multiple of , and after the mean is added back it rebuilds every point as .
Nonlinear dimensionality reduction
Definition Dimensionality reduction maps each input in to a representation in with that keeps the structure needed for a stated purpose. It is linear when the encoder and decoder are linear, as in PCA, and then the reconstructions lie in an affine subspace of dimension at most . It is nonlinear when the encoder or the decoder is nonlinear, so that the reconstructions can lie on a curved surface of dimension , such as a curve when . An autoencoder with nonlinear layers and a bottleneck is one such method.
In plain words When data lie near a curve or a curved surface, a flat subspace of the same dimension cannot follow it, so PCA may need more components to reach the same error. A nonlinear decoder can bend. Whether that helps depends on the data, and the comparison should keep the same inputs, the same error definition and the same roles for training and held-out data while only the model family changes.
Example Evaluate both one-number summaries on the held-out values , none of which was used for fitting. PCA, fitted on the five training points, rebuilds as . Its first coordinates are exact, and its second-coordinate errors are at and at , so the eight squared errors sum to and
The trained curved autoencoder rebuilds the same points with an MSE of about , which is rounding error. That is possible only because the data come from a noise-free curve lying exactly inside the chosen model family. It is not evidence that autoencoders rebuild real data perfectly.
Denoising autoencoder
Definition A denoising autoencoder is trained on corrupted inputs , drawn from a chosen corruption distribution such as added Gaussian noise or randomly zeroed entries, with the clean input as the target,
where in practice the expectation is replaced by an average over training inputs and sampled corruptions. When clean inputs have finite second moments, the best reconstruction function of the corrupted input over all functions is the conditional mean . Because the network input and the target now differ, copying is no longer optimal for corruptions such as added independent noise with positive variance, even with an overcomplete code.
In plain words Damage the input on purpose and ask for the undamaged version. To undo the damage the model has to learn what typical clean inputs look like. The corruption process is a design choice, and the model learns to undo that kind of damage, not every kind.
Example Take the clean point on the parabola and a corruption that changes only its second coordinate to , a squared error of in that coordinate. The curved autoencoder trained above, given , returns up to rounding, because its encoder reads only the first coordinate. That cleanup comes from an architecture chosen by hand, not from denoising training. If the corruption moves the first coordinate to instead, the same model returns , which misses the clean point by and , an MSE per coordinate of .
Check yourself
- An input is rebuilt as . Find the sum of squared errors, the mean squared error per coordinate and half the sum of squared errors. Why must a report say which of these it calls the loss?
Show the answer
The errors are , , and , so the sum of squared errors is 2. Dividing by the four coordinates gives an MSE per coordinate of 0.5, and half the sum is 1.
All three are common, and they differ by constant factors that depend on the convention and on the input dimension. The same model would be reported as 2, 0.5 or 1, so a number without its reduction cannot be compared with another model’s number.
- An autoencoder for two-dimensional inputs uses a three-dimensional code and reaches a training reconstruction error of exactly 0. A colleague concludes that the code is an excellent representation. What is wrong with that conclusion, and what evidence would help?
Show the answer
A code of dimension 3 for inputs of dimension 2 allows plain copying. The linear encoder and the decoder that drops the last coordinate rebuild every input exactly, training and new inputs alike, while the code is just the input again. Zero error is therefore possible without learning anything about the data.
Better evidence starts with reconstruction error on held-out inputs for a model that cannot simply copy, for example one with a bottleneck, corrupted inputs or a penalty on the code, compared with a baseline such as PCA with the same code size. If the code is meant for a downstream task, freeze it, train a head on training data, choose settings on validation data, and compare with a head trained on the raw inputs. Low reconstruction error, even on held-out data, does not show that the code serves that task.
- Four centered training points are , , and . A linear autoencoder with a one-dimensional code is trained on them, and a classmate reports that it found the principal direction . Compute the covariance and the average SSE for that direction and for , and explain what the result means.
Show the answer
The covariance with denominator 4 is
so both eigenvalues equal . Projecting onto the unit vector sends to , an SSE of , and by symmetry each of the four points has SSE , so the average is . Projecting onto gives SSE 0 for the first two points and 1 for the other two, again an average of .
For any unit vector the average SSE is , the minimum of the theorem. Since , the condition fails, every direction is equally good, and no direction is the principal one. The direction the network ended with reflects its initialization and training, not a property of the data.