Rao-Blackwellized Score Matching on Manifolds
What do generative modeling techniques actually recover, and can we do it better? [arXiv]
(Part of a series of short writeups covering recent work.)
Setup#
Generally, the paradigm of generative modeling is as follows: given a dataset, can we find some way to generate new data that seems as if it was pulled from the original dataset? A common example of this is images; a generative modeling task here might be that one is given a set of 100 images of cats and after some "learning," is asked to generate a new image of a cat. More formally, what we usually want here is to approximate some kind of probability distribution given samples from that distribution, then generating new data is as simple as sampling from our distribution.
Score Matching#
At first glance, this is quite a difficult task. In particular, one of the main obstacles here is normalization. Recall that a probability distribution must always have total probability mass 1 over its entire domain (Kolmogorov's unit measure axiom). But, when we sample, we are only given relative weightings between regions of the domain (e.g. I might notice that a calico cat is roughly twice as likely as a black cat). This is easy to get around conceptually, we can just renormalize (in our cat example, if calico and black are the only allowed cat colors, it follows immediately that the probability of a given cat being calico is 2/3 and black 1/3), but this is often difficult to do in an efficient manner. This obviously presents a pretty big problem for our generative modeling task: if we can't normalize the distribution efficiently, we can't learn/sample from it efficiently!
So, to get around this, [Hyv05] proposed the following paradigm: instead of approximating the distribution, just approximate its gradient (or its "score"). So now, instead of us learning some probability law so that (here is the true probability distribution) for every in our domain, instead, we want for every , where of course is the gradient with respect to . This method, called score matching, gets rid of our earlier problem: since normalization must sum over all , it drops out in the derivative and isn't something we need to worry about.
Manifolds#
Okay so now we've got a handle on the score matching part of the title, now let us turn our attention to manifolds. Manifolds are a really tricky subject and the formal definition of them is quite nasty1, but intuitively, they're just a type of space that looks flat if you zoom in enough. A really good example here is Earth; Earth is a sphere, but to us, it seems flat since we are local enough. So now why do we care about manifolds? Well, manifolds tend to be really useful for representing structure.2 And generally, the data we care about has some kind of structure to it (e.g. in our cat example, a form of structure is that every cat has whiskers). This structure imposes constraints on our data, and oftentimes, we can represent this as our data living on some manifold (in fact, a pretty well known theory in machine learning is that the data we learn is drawn from a manifold, the aptly named manifold hypothesis). So in these structured settings, we know that our data is drawn from a manifold, and thus, our probability law is defined as 0 off manifold. A concrete example is your computer screen. Your computer screen has only 2 dimensions (height and width), but every pixel technically exists in 3 dimensions. Because the pixels all exist on your computer screen, in the local coordinate system of the computer screen, we need only 2 dimensions to describe its position (height and width), but in the coordinate system of the room, we need 3 (height, width, and length; here we have fixed some reference point). But now, if I ask you to draw the distribution of where the pixels are located in the room, it makes no sense to assign any probability to the chair; a pixel can't exist there.
A careful reader will have noticed an issue here: if our data lives on a manifold, and we take the gradient with respect to ambient coordinates, there is no density in the ambient space at all, so the score is undefined everywhere on the manifold: the density falls off a cliff in every direction pointing off of it, and there is nothing to differentiate. Indeed, returning to our computer example, the screen has no thickness: on the screen, there is a nonzero probability of finding an on pixel, but immediately in front of or behind the screen, the probability is zero. So how do we deal with that? The standard method is just by corrupting the data — we just add some noise to the data so it lies slightly off the manifold, and then we no longer have this issue (intuitively, you can think of this as almost smoothing a peak). The problem is, now we're not learning the true law, we're learning a corrupted surrogate!
Thus, the natural question is What exactly is this method learning?
Rao-Blackwell Theorem#
So far, we know that score matching is a way of approximating a distribution by matching its derivatives and that manifolds are some type of mathematical way of representing structured data. Now, let's take a brief detour into statistics to study the Rao-Blackwell Theorem.
Consider the following setup: we have a (possibly unfair) coin that lands heads with probability . Given that we observe flips of the coin, we wish to estimate . Any rule that takes in these observations and outputs a prediction is an estimator. There's two main desirable properties that we want from an estimator:
- Unbiasedness, i.e. it should be correct on average;
- Low Variance, i.e. it shouldn't vary wildly from one dataset to the next.
Getting unbiasedness is quite easy, and there's a number of rules that allow us to get an unbiased estimator. The harder part here is finding an estimator with low variance.
To illustrate this point, let's consider a deliberately bad rule. Write if flip came up heads and otherwise, and consider : look at the first flip, report 1 if heads and 0 if tails, and throw the other flips away. This is an unbiased estimator since the first flip is heads with probability . However, its variance is quite large: it only outputs 0 or 1 and its variance is regardless of .
On the other hand, the natural estimator here is to count the heads and report the fraction. Writing for the number of heads, the sensible rule is , which is also unbiased but has variance . The main point of Rao-Blackwell is that the sensible rule can be constructed by "cleaning up" the bad rule.
A full record of flips, say , contains two separable pieces of information: how many heads there were, and in what order they arrived. The count obviously says something about . The order is totally irrelevant: if I tell you 2 of 5 flips were heads, all arrangements are equally likely, regardless of what is. So the ordering is pure noise as far as is concerned. A summary like that contains all the relevant information so that the remaining information is randomness whose distribution doesn't depend on the quantity we are estimating is called a sufficient statistic.
Our bad estimator is contaminated by the irrelevant part: it asks which flip came first, so it inherits variance from the ordering. The fix is to average that dependence away. Freeze the count at , look at every arrangement of flips consistent with it, and average over all of them. By symmetry, each of the positions is equally likely to hold one of the heads, so Averaging our bad rule over all the irrelevant information (the permutations) gives back our good estimator.
That's the Rao-Blackwell Theorem. Given an unbiased estimator and a sufficient statistic , define the average of over all datasets sharing the same summary. Then is still unbiased, and , with equality exactly when was already a function of alone.3
Intuitively, it just says to find the part of your data that is pure noise with respect to the thing you want, and integrate it out.
Rao-Blackwellization on Manifolds#
Equipped with an understanding of the main ideas, we can now start to answer the question we posed.
The first step is to identify our analog of a sufficient statistic here. Recalling that our probability law is defined only on the manifold, we really only care about points on the manifold. So, a natural sufficient statistic type thing to condition on here is the nearest point projection: the map that sends every noisy point back to whichever point on the manifold it's closest to.

In order to condition on anything, we must first define our estimator. In denoising score matching (score matching on our corrupted data), we take a clean sample from our data distribution (which lives on the manifold ), corrupt it into with a bit of Gaussian noise , and train a network to predict the direction that undoes the corruption, . Observe that this direction splits into two pieces: the part pointing along the manifold, and the part pointing off of it. The off-manifold piece is, to leading order, , which we can compute from without knowing . All of the signal is in the tangent piece: where keeps the component pointing along at .
This is very similar to our coin flipping example. Our observation carries two kinds of information: roughly where on we came from, and how far off the noise shifted us. The second kind is irrelevant for what we want, just like the ordering of the flips was pure noise with respect to . So, we just average over it. Define the Rao-Blackwellized target the average of over every noisy point that projects back to the same . Averaging over a fiber is our analog of averaging over the permutations.
In fact, this reduces the variance just like in the Rao-Blackwell Theorem. isn't a sufficient statistic in the classical sense, but we don't need it to be: the law of total variance already gives . In the below image, is the Rao-Blackwellized target, while is the non-RBized target.

The main benefit here is that we are able to estimate what we want with significantly less variance, and that becomes more important as the amount we corrupt our data by (represented by ) gets smaller.
What Does DSM Learn?#
We now have a target that has low variance, but we still have not answered the question we posed earlier. Because we had to corrupt the data to make score matching well defined at all, we aren't learning the true law, we're learning some corrupted surrogate. So, what's the surrogate?
One can show (via a lot of tedious math), that expanding as a power series in the noise level gives
The first term is exactly what we want to see: is the intrinsic score: the gradient of the log density taken along the manifold, which is what we wanted to learn. So at leading order, the naive ambient approach of corrupting with ordinary Gaussian noise and then cleaning up with Rao-Blackwellization recovers the right thing. All good there.
Things get a bit messier with the term; in some sense, the "price" of corrupting the data. It splits neatly into three pieces, but before we break them down, we need to introduce a useful idea from differential geometry.
Curvature. In differential geometry, there are two notions of curvature. Extrinsic curvature describes how bends inside the ambient space it sits in. Intrinsic curvature is what one could measure while living on , with no knowledge of the ambient space at all. These are quite different. For example, take a cylinder: it obviously bends in , so it has extrinsic curvature, but you can unroll it flat onto a table without stretching or tearing anything, so an ant living on it could never distinguish it from a plane. The cylinder is extrinsically curved and intrinsically flat. Alternatively, a sphere is neither intrinsically nor extrinsically flat (which is why you can't flatten an orange peel without tearing it4). It's the exact reason that every flat world map ends up distorting something.
With that distinction in mind, let's characterize the three corrections.
- Tweedie bias . This part actually has nothing to do with the embedding. Gaussian smoothing blurs the density a little bit, and that changes its score [Efr11], so this is just the flat correction, transplanted onto by using the gradient and Laplacian along the manifold. It is the price of smoothing that any method pays, intrinsic ones included.
- Extrinsic curvature term . This is a linear operator built from ways of measuring both types of curvature and applied to the score.5 This is the term that measures how the embedding of in the ambient space affects our process.
- Inhomogeneity drift . This term measures how much the curvature of changes as you move around it, so it vanishes when the manifold curves the same way everywhere.6 A canonical example is the sphere, since every point looks like every other.
Both geometric terms are built from the embedding alone: the curvature operator in and the drift are determined by how sits inside the ambient space, never by . The data distribution enters only through the score that the operator is applied to, which is the thing we already trained a model to predict. So given a trained score model , we can simply subtract the bias off afterwards: apply the curvature operator to , add the drift, scale by , subtract. No retraining, and no access to required.7
A Happy Coincidence on #
Let's now restrict to spheres, a really good toy example. On (the sphere in dimensions), the curvature is the same in every direction at every point, and plugging the values in collapses the entire extrinsic operator down to a single scalar: The inhomogeneity drift is already zero, since a sphere curves identically everywhere. So on a sphere the entire geometric correction is one scalar times the score:
- : ;
- : ;
- : ;
- : you get the point.
A neat result here is that at the coefficient is exactly zero. On the round 2-sphere both geometric terms vanish, and ambient DSM recovers the intrinsic score up to only the flat Tweedie bias that every method pays anyway.
This is demonstrated in the top row of the below figure. The bottom row shows the sample-complexity gain from Rao-Blackwellization: the RB target gives lower score error at small and moderate training set sizes, with the gap closing as grows.

Ambient DSM is often observed to work about as well as intrinsic methods on data living on (e.g. earth-science event data like earthquake or wildfire locations), even though score matching on manifold-supported data is ill-posed. The cancellation offers an explanation: on the round 2-sphere, the leading-order geometric bias vanishes, so ambient and intrinsic methods agree up to the Tweedie term.
P.S. Apparently some account also made one of those AI generated reels of voiceovers with Minecraft gameplay in the background, if that's more your style, you can check it out here :).
- See e.g. these notes for an example of a very formal definition. β©
- If you're interested in how exactly they are useful for representing structures, take a look at Lie theory: a good intro is in these notes. β©
- Conditioning on any summary keeps the estimator unbiased, but if the leftover randomness still depends on the quantity we're estimating, then so does the conditional expectation, and we end up with an "estimator" we can't actually compute without already knowing the answer. β©
- Maybe this isn't the most natural example, but it came to mind since I'm eating an orange as I write this. β©
- For anyone who wants the actual objects: the operator is , built from the Weingarten map (which measures extrinsic curvature) and the Ricci endomorphism (which measures intrinsic curvature). Both are linear maps on the tangent space, which is what lets us compare them and apply them directly to the score. do Carmo [doC92] is a fantastic reference. β©
- Formally, this term is linear in , the covariant derivative of the second fundamental form, and an embedding with is called parallel. β©
- Strictly speaking, the score estimate does have to be reasonably accurate at the points where we apply the correction. But we never need access to itself, which is usually the real obstacle. β©
References#
- [Efr11]Bradley Efron. "Tweedieβs Formula and Selection Bias". Journal of the American Statistical Association Vol. 106, pp. 1602 - 1614 2011. [link] β©
- [Hyv05]Aapo HyvΓ€rinen. "Estimation of non-normalized statistical models by score matching". 2005. [link] β©
- [doC92]Manfredo PerdigΓ£o do Carmo. Riemannian Geometry. 1992. β©