Diffusion Models Learn the Manifold Before They Memorize
Anyone who has used Stable Diffusion or a similar image generator has seen it produce images that exist nowhere else: a portrait of a person who does not exist, or a scene that no one has photographed. Few people find this surprising, since producing new images seems to be the purpose of the model. The training objective, however, does not ask for it. Stated precisely, the objective is a denoising regression over a finite set of \(N\) training images, and its exact minimizer is known in closed form. A model that reached this minimizer would generate only the training images themselves.
The models we use do not behave this way, so something prevents them from reaching the minimizer, or changes what they learn on the way to it. This post examines that gap in four steps:
- The optimal denoiser has a closed form, and sampling with it yields the empirical distribution of the training set, which is memorization.
- Practical models do not reach this minimizer. During training they pass through a regime in which they generate novel samples.
- In a low-dimensional example, the samples first lie on the structure that supports the data and then gradually collapse into clusters around individual training points. This suggests that the model first learns the geometry of the data manifold and only afterwards memorizes.
- Verifying that a model has learned this geometry is difficult in high dimensions, where the data cannot be visualized. We propose a diagnostic that estimates the dimension of the learned manifold from the Jacobian of the denoiser.
Generalization in Generative Models
The goal of a generative model is not to reproduce its training set. Given samples from an unknown distribution \(p_r\), we want a model \(q_\theta\) whose samples are indistinguishable from those of \(p_r\) yet are not near-copies of individual training examples. Three properties are commonly distinguished:
- Fidelity: individual samples are realistic.
- Diversity: the samples cover the variability present in the data.
- Novelty: the samples are not close to any training example.
No single metric captures all three. For images, the standard metric is the Fréchet Inception Distance (FID), which compares feature statistics of real and generated samples. It mainly reflects fidelity and diversity and cannot detect whether generated samples duplicate training images, so novelty must be assessed separately. The failure of novelty is memorization: the model reproduces its training examples instead of generating new ones. Prior work often treats memorization and generalization as a dichotomy. Yoon et al., in Diffusion Probabilistic Models Generalize when They Fail to Memorize, argue that the two are mutually exclusive. We return to this view later, since the experiment below suggests a more gradual picture.
The formulation of Alaa et al. (2022) makes the distinction geometric. It considers the support of the real distribution \(\mathbb{P}_r\) and the support of the synthetic distribution \(\mathbb{P}_g\). A desirable sample is authentic and high-quality, meaning that it lies within the support of \(\mathbb{P}_r\), while not coinciding with a training point.
Problem Setup
Let \(\{x_i\}_{i=1}^N \subset \mathbb{R}^D\) be a training set drawn from an unknown distribution \(p_r\). The forward process perturbs a sample with Gaussian noise,
\[x_\sigma = x + \sigma \boldsymbol{\epsilon}, \qquad \boldsymbol{\epsilon} \sim \mathcal{N}(0, I),\]and a denoiser \(D_\theta(x_\sigma, \sigma)\) is trained to recover \(x\) from \(x_\sigma\) by minimizing
\[\mathcal{L}(\theta) = \mathbb{E}_{x \sim \hat{p}, \; \boldsymbol{\epsilon}} \left\| D_\theta(x + \sigma\boldsymbol{\epsilon}, \sigma) - x \right\|^2,\]where \(\hat{p} = \frac{1}{N}\sum_{i=1}^N \delta_{x_i}\) is the empirical distribution of the training set. The expectation is taken over \(\hat{p}\) and not over \(p_r\), since \(p_r\) is accessible only through the samples.
By Tweedie’s formula, the denoiser is related to the score of the noised distribution \(p_\sigma\) by
\[D(x, \sigma) = x + \sigma^2 \nabla_x \log p_\sigma(x).\]Sampling integrates the reverse-time dynamics driven by this score, from a large noise level down to \(\sigma \to 0\).
The Optimal Denoiser
Because the loss is defined over the empirical distribution, its minimizer is available in closed form:
\[D^\star(x, \sigma) = \sum_{i=1}^N w_i(x, \sigma)\, x_i, \qquad w_i(x, \sigma) = \frac{\exp\left(-\|x - x_i\|^2 / 2\sigma^2\right)}{\sum_{j=1}^N \exp\left(-\|x - x_j\|^2 / 2\sigma^2\right)}.\]That is, \(D^\star\) returns a softmax-weighted average of the training points, with weights determined by the distance from \(x\) to each \(x_i\). As \(\sigma \to 0\) the weights concentrate on the nearest training point, so \(D^\star(x, \sigma) \to x_{i^\star(x)}\) with \(i^\star(x) = \arg\min_i \|x - x_i\|\). The associated score is that of a Gaussian mixture centered on the training points. Sampling with \(D^\star\) therefore converges to \(\hat{p}\) rather than to \(p_r\).
The exact minimizer is thus a memorizing solution. Trained networks nevertheless produce novel samples, which implies that they do not converge to \(D^\star\) in practice. Two factors contribute: the network class is biased toward functions that are smooth in \(x\), and training is stopped before \(D^\star\) is fit exactly. The question addressed here is what the network represents before it reaches \(D^\star\).
A Low-Dimensional Example
To observe this directly, we use a two-dimensional example in which the support of the data is known. The data lie on the parabola
\[y = 1 - a x^2,\]and the training set consists of four points on it, with \(x \in \{-1, -0.5, 0.5, 1\}\). A diffusion model is trained on these four points, and we draw samples from it at several training steps. The experiment is repeated for several values of \(a\).
Step 900. The samples form a diffuse cloud around the curve. The model has captured the approximate location of the data but not its shape.
Step 5000. The samples are distributed along the whole parabola, including the regions between the training points. The model therefore generates points that are not in the training set and that lie on the curve supporting the data. This is the generalizing regime.
Steps 7000 and 9000. With further training, the sampling distribution moves toward the training points gradually. Take \(a = 0.376\). At step 5000 the samples form a nearly continuous band along the parabola. At step 7000 the density has increased near the four training points and decreased between them. At step 9000 most of the mass lies in four compact clusters around the training points, and only a small number of samples remain on the curve between them. The other rows show the same progression, at different rates.
Two observations follow from the figure. First, at intermediate steps the samples lie on the curve that supports the data, including between the training points, so the model represents the curve and not only the four points. Second, the departure from this behavior is continuous: probability mass leaves the regions between the training points and accumulates around them progressively, with no abrupt switch. Generalization and memorization therefore lie on a single continuum.
The curve in this example is an instance of a data manifold. The next section makes this notion precise and uses it to explain why the intermediate samples are novel.
The Manifold Hypothesis
Natural data such as images have very high ambient dimension \(D\), but the set of valid samples occupies a much smaller region. Formally, the manifold hypothesis assumes
\[x = g(z), \qquad z \in \mathbb{R}^d, \quad d \ll D,\]so that the data concentrate near a \(d\)-dimensional manifold \(\mathcal{M} \subset \mathbb{R}^D\). In the example above, \(D = 2\), \(d = 1\), and \(\mathcal{M}\) is the parabola.
Under this assumption, a novel sample is a point of \(\mathcal{M}\) that is not among the training points. A model that represents \(\mathcal{M}\) can produce such points, as at step 5000. A model that represents only the finite set \(\{x_i\}\) cannot, as at step 9000.
Assessing Whether the Model Has Learned the Geometry
In two dimensions the answer can be read from the figure. For real data, whose ambient dimension is in the thousands or more, direct visualization is not possible.
A common alternative is to project the data and the samples to two or three dimensions, for example with PCA, and compare their distributions. This approach cannot certify that the geometry has been learned. Agreement in a particular projection constrains the distributions only along the chosen directions. A projection can exist in which the sample distribution and the data distribution coincide, even though they differ in the full space. Conversely, the choice of projection is arbitrary and can inadvertently hide the discrepancy.
A suitable diagnostic should therefore:
- operate in the full ambient space, without a choice of projection;
- examine the model itself and not only its samples;
- measure a geometric property, which a scalar distance between distributions does not do.
Diagnostic: The Jacobian of the Denoiser
If the model has learned \(\mathcal{M}\), its score field must encode the location of \(\mathcal{M}\). Consider a data point \(x_0 \in \mathcal{M}\) perturbed by small isotropic noise,
\[x = x_0 + \sigma \boldsymbol{\epsilon}, \qquad \sigma \ \text{small}.\]The perturbation has components in the tangent space \(T_{x_0}\mathcal{M}\) and in the normal space \(N_{x_0}\mathcal{M}\). For small \(\sigma\), the score of the noised distribution satisfies approximately
\[\nabla_x \log p_\sigma(x) \;\approx\; -\frac{x - \pi_{\mathcal{M}}(x)}{\sigma^2},\]where \(\pi_{\mathcal{M}}\) denotes the orthogonal projection onto \(\mathcal{M}\). Stanczuk et al. (2024) prove that, as the noise level decreases, the score points toward the manifold, so that it approximates the normal space of \(\mathcal{M}\). By Tweedie’s formula, \(\nabla_x \log p_\sigma(x) = (D(x, \sigma) - x)/\sigma^2\), so this is equivalent to \(D(x, \sigma) \approx \pi_{\mathcal{M}}(x)\). The correction \(D(x, \sigma) - x\) is then normal to the manifold: the denoiser removes the normal component of the noise and leaves the tangential component unchanged. This agrees with Kadkhodaie et al. (2024), who observe that for small noise an optimal denoiser projects a noisy image onto the tangent space of the manifold.
The same approximation determines the Jacobian \(J(x) = \partial D(x, \sigma) / \partial x\). The map \(\pi_{\mathcal{M}}\) is locally the identity along tangent directions and constant along normal directions, so \(J(x)\) approximates the orthogonal projector onto the tangent space \(T_{x_0}\mathcal{M}\). It is therefore symmetric, with \(d\) eigenvalues equal to \(1\) (directions along the manifold) and the remaining \(D - d\) eigenvalues equal to \(0\) (normal directions).
The memorizing solution behaves differently. For \(D^\star\), the Jacobian is the posterior covariance of the training points divided by \(\sigma^2\),
\[J^\star(x) = \frac{1}{\sigma^2} \sum_{i} w_i (x_i - D^\star)(x_i - D^\star)^\top,\]which vanishes in every direction as the weights \(w_i\) concentrate on a single training point. The Jacobian of a trained model should therefore move from a rank-\(d\) projector toward zero as it transitions from the manifold to the training points.
Estimating the Dimension
The Jacobian of a trained network is only approximately a projector, so we measure how close it is to one. Write \(J = A + S\) with \(A = (J + J^\top)/2\) and \(S = (J - J^\top)/2\), and let \(\lambda_1 \ge \dots \ge \lambda_D\) be the eigenvalues of \(A\). For each \(k\), we compute the squared Frobenius distance from \(J\) to the nearest rank-\(k\) orthogonal projector,
\[E(k) = \min_{P \in \mathcal{P}_k} \|J - P\|_F^2, \qquad \mathcal{P}_k = \{P : P = P^\top = P^2, \ \operatorname{rank} P = k\}.\]Because \(P\) is symmetric, \(\|J - P\|_F^2 = \|A - P\|_F^2 + \|S\|_F^2\), and the minimizer projects onto the top-\(k\) eigenvectors of \(A\), which gives the closed form
\[E(k) = \sum_{i \le k} (1 - \lambda_i)^2 + \sum_{i > k} \lambda_i^2 + \|S\|_F^2.\]We define the estimated dimension as \(\hat{k} = \arg\min_k E(k)\). Increasing \(k\) by one changes \(E\) by \(1 - 2\lambda_k\), so \(\hat{k}\) equals the number of eigenvalues of \(A\) that exceed \(1/2\). The antisymmetric part \(S\) adds a constant to \(E(k)\) and does not affect \(\hat{k}\). For an exact score, \(J = I + \sigma^2 \nabla^2 \log p_\sigma\) is symmetric, so \(\|S\|_F\) also measures how far the network deviates from a gradient field.
For a model that has learned a \(d\)-dimensional manifold, \(\hat{k} \approx d\). For a model that has memorized, \(\hat{k} \to 0\). Tracked over training, \(\hat{k}\) is the quantitative counterpart of the sample plots in the low-dimensional example.
Protocol
We apply the diagnostic to a class-conditional diffusion model trained on MNIST. The network predicts the noise \(\boldsymbol{\epsilon}\), with \(x_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \boldsymbol{\epsilon}\). Writing \(x_\sigma = x_t / \sqrt{\bar\alpha_t}\) with \(\sigma^2 = (1 - \bar\alpha_t)/\bar\alpha_t\), the denoiser is \(D = (x_t - \sqrt{1 - \bar\alpha_t}\, \epsilon_\theta(x_t))/\sqrt{\bar\alpha_t}\) and its Jacobian is
\[J = I - \sqrt{1 - \bar\alpha_t}\; \frac{\partial \epsilon_\theta}{\partial x_t}.\]The procedure is as follows:
- Train on a stratified subset of MNIST, padded to \(32 \times 32\).
- At evenly spaced training steps, take a fixed set of \(n\) training images per digit, without augmentation, and perturb them at a fixed small noise level \(t\) with a fixed random seed.
- For each noised image, compute the full \(784 \times 784\) Jacobian over the pixels of the \(28 \times 28\) interior, using one backward pass per output pixel, conditioned on the image’s digit label.
- Compute the singular values of \(J\) and the curve \(E(k)\), and average both over the \(n\) images of each digit. The estimate \(\hat{k}\) is the minimizer of the averaged \(E(k)\).
Results are reported per digit, since the model is class-conditional and each digit has its own manifold.
Results
We apply the protocol at noise level \(t = 0.005\) to two MNIST models: one in the generalizing regime and one overfit to its training subset.
Generalizing model. For every digit, the leading eigenvalues of the symmetrized Jacobian form a plateau at 1, followed by a decay to 0 over the remaining directions (Figure 1, left). The distance \(E(k)\) has a clear minimum at an intermediate \(k\) (Figure 1, right), so \(\hat{k}\) is far below the ambient dimension of 784. It ranges from 61 for digit 1 to 133 for digit 0 (Figure 2). In Figure 2, \(\hat{k}\) falls where the mean spectrum crosses \(1/2\), as implied by \(\hat{k} = \#\{i : \lambda_i > 1/2\}\).
The Jacobian is nevertheless not an exact projector. The transition from 1 to 0 is gradual, and a small number of leading eigenvalues exceed 1, reaching about 1.5 to 2 (Figure 1, left). As a result, the minimum of \(E(k)\) lies between roughly 7 and 17 and not near zero.
Overfit model. The spectrum of the overfit model is qualitatively different (Figure 3). There is no plateau at 1: the largest eigenvalue is about 0.57, the next are below 0.4, and the spectrum reaches 0 within roughly 150 directions. The distance \(E(k)\) increases with \(k\) from its value at \(k = 0\), so \(\hat{k}\) is at most 1 and the best-fitting projector is essentially zero. This is the behavior predicted for \(J^\star\), which vanishes as the weights concentrate on a training point.
Between the two models, \(\hat{k}\) drops from the range 61 to 133 to at most 1, which is the collapse in dimension that the transition from the manifold to the training points implies.
Open Questions
- Choice of noise level. The noise level must be small enough that the manifold is approximately flat over the perturbation scale, but large enough that the network is accurate at that level.
- No reference dimension. The dimension of the MNIST manifold is unknown, so \(\hat{k}\) is interpreted through its change over training and across digits and not against a ground truth. A check on data of known dimension, such as the parabola above, would calibrate the estimate.
- Threshold. The estimator counts eigenvalues above \(1/2\). The MNIST spectra decay gradually around this value instead of showing a sharp gap, so the sensitivity of \(\hat{k}\) to the threshold remains to be examined.