logo Mr.Robota 具身智能学习手册 中文
Chapter 17: BWM: Tongji Boundless Team's Action Condition World Model

Introduction to the Mathematical Basis of Diffusion and Problem Analysis

The core issue of the generation model is:

We hope the model learns a data distribution Pdata(x)P_{data}(x), and then can sample new samples from this distribution xx.

For example: facial image distribution, handwritten digit distribution, circular cloud distribution, text, audio, and video distribution, etc.

However, data distributions are usually complex. We cannot directly describe their distribution and density functions, so we need some techniques to solve these problems.

Many people are intimidated by a bunch of formulas when getting started with diffusion models. In fact, the principle of diffusion is quite simple. This content uses simple code to explain the diffusion model, allowing everyone to get familiar with VAE and ddpm without having to derive complex formulas.

In the first part of this chapter, we will explain the mathematical foundations required for diffusion models, which are mainly used for subsequent formula derivations. If you want to directly experience the diffusion model, you can directly refer to the content in the second part. It is a simplified version of VAE and DDPM, providing a complete and simple diffusion experience. In the third part, based on the content in the second part, we explain that some functions in the second part cannot be obtained, which is why we need a diffusion model.

I. Mathematical Foundations

1. Bayesian Formula

When learning about Bayesian formulas, we often see the following form:

p(z∣x)=p(x∣z)p(z)p(x) p(z \mid x) = \frac{p(x \mid z)p(z)}{p(x)}

The core idea of this formula is: After seeing a result, infer the most likely cause of it.

In tasks such as machine learning, statistical inference, image recognition, and natural language processing, the Bayesian formula is very important. Let’s understand each symbol in the formula one by one.

Symbol Name Meaning
zz Hidden variable / Underlying cause Represents a cause that we cannot see but want to infer
xx Observed data Represents data or phenomena that we have seen
p(z)p(z) Prior probability The probability that the cause zz occurs before observing the data xx
p(x∣z)p(x \mid z) Likelihood The probability of observing the data xx given that the cause zz is known
p(z∣x)p(z \mid x) Posterior probability The probability that the cause zz occurs after observing the data xx
p(x)p(x) Evidence / Marginal likelihood The total probability of observing the data xx itself

zz: Potential causes

zz represents the object we want to infer.

It is usually not directly observed, but rather the reason hidden behind the data, hence it is also called hidden variables.

For example: When observing a patient’s symptoms, we want to determine what disease they have.

xx: Observation data

xx indicates the data we have observed.

For example: a set of symptoms. In the Bayesian formula, we infer the underlying cause zz based on the observed xx.

That is to say: xx is the result, and zz is a possible cause of this result.


p(x)p(x): Evidence or marginal likelihood

p(x)p(x) represents the total probability of the observed data xx, also known as evidence or edge likelihood.

For example, if someone shows symptoms such as “fever, cough, and runny nose,” it can be caused by many factors: cold, influenza, allergies, pneumonia, or other diseases.

Therefore, p(x)p(x) will consider the probabilities of all possible causes leading to this observation result.

If the possible cause is discrete, it can be written as: p(x)=∑zp(x∣z)p(z)p(x) = \sum_z p(x \mid z)p(z)

Its main function is to normalize the probabilities, so that the posterior probabilities of all possible causes add up to 1.

p(z)p(z): Prior Probability

p(z)p(z) refers to our initial judgment on the cause zz before seeing the observation data xx, also known as prior probability.

For example, when determining whether a person has a cold, if it is the flu season, the probability of the cause “cold” occurring is relatively high; if it is not the flu season, the probability may be lower.

Therefore, p(z)p(z) can be understood as our judgment of the cause based on prior experience before seeing the evidence.

p(x∣z)p(x \mid z): Likelihood

p(x∣z)p(x \mid z) represents the probability of observing data xx under the condition that the cause zz has been identified. It is also known as likelihood.

For example, if someone really has a cold, what is the probability of symptoms such as “fever, cough, and runny nose”?

The relationship here is: p(症状∣感冒)p(\text{症状} \mid \text{感冒})

It measures whether a certain cause can well explain the phenomenon we observe. If a cause can well explain the observational data, the likelihood is relatively high; otherwise, the likelihood is low.

p(z∣x)p(z \mid x): Posterior Probability

p(z∣x)p(z \mid x) represents the probability of cause zz occurring after data xx has been observed. It is also known as posterior probability.

This is what the Bayes’ theorem ultimately aims to achieve.

For example, when we see a person showing symptoms such as “fever, cough, and runny nose,” we want to know the probability that they are really sick: p(感冒∣症状)p(\text{感冒} \mid \text{症状})

Therefore, posterior probability can be understood as: a re-evaluation of the cause after seeing the result.

The role of the Bayes’ theorem is to combine the prior probability and observation data to obtain the posterior probability.


Bayes’ Theorem:

p(z∣x)=p(x∣z)p(z)p(x) p(z \mid x) = \frac{p(x \mid z)p(z)}{p(x)}

It can be understood as: Posterior probability = Likelihood × Prior probability ÷ Evidence

That is:

Formula Item Intuitive Explanation
p(z∣x)p(z \mid x) How many possible causes exist after seeing the result
p(x∣z)p(x \mid z) How many possible results if the cause is valid
p(z)p(z) How many possible causes originally
p(x)p(x) How common the results are overall

Therefore, the core function of the Bayes’ theorem is: to update our judgment about a cause based on the observed data and existing experience.

2. Jensen’s Inequality

For the concave function ff, we have:

f(E[X])≥E[f(X)] f(\mathbb{E}[X]) \geq \mathbb{E}[f(X)]

For convex functions ff, the following holds:

f(E[X])≤E[f(X)] f(\mathbb{E}[X]) \leq \mathbb{E}[f(X)]

In machine learning, usually log⁡x\log x represents ln⁡x\ln x as a concave function, so:

log⁡E[X]≥E[log⁡X] \log \mathbb{E}[X] \geq \mathbb{E}[\log X]

In many models, what we want to maximize is: log⁡p(x)\log p(x). However, p(x)p(x) is often a score:

p(x)=∫p(x,z) dz p(x)=\int p(x,z)\,dz

So:

log⁡p(x)=log⁡∫p(x,z) dz \log p(x)=\log \int p(x,z)\,dz

This thing is usually not easy to optimize directly. Jensen’s inequality can help us construct a lower bound, that is:

$ \log p(x) \geq \text{某个可优化的目标} $

This lower bound is the core of many generative models.

3. Gaussian Distribution

The Gaussian distribution, also known as the normal distribution, is the most commonly used distribution in generative models.

One-dimensional Gaussian distribution

One-dimensional Gaussian distribution is written as:

x∼N(μ,σ2) x \sim \mathcal{N}(\mu,\sigma^2)

We can calculate his probability density function below.

We can start with the simplest standard normal distribution:

X∼N(0,1) X\sim N(0,1)

The density function of the standard normal distribution can be written as:

f(x)=Ce−x22 f(x)=Ce^{-\frac{x^2}{2}}

Here, CC is a variable constant. Since f(x)f(x) is the probability density function, the normalization condition must be satisfied:

∫−∞+∞f(x) dx=1 \int_{-\infty}^{+\infty}f(x)\,dx=1

Substitute f(x)=Ce−x22f(x)=Ce^{-\frac{x^2}{2}}:

∫−∞+∞Ce−x22 dx=1 \int_{-\infty}^{+\infty}Ce^{-\frac{x^2}{2}}\,dx=1
C∫−∞+∞e−x22 dx=1 C\int_{-\infty}^{+\infty}e^{-\frac{x^2}{2}}\,dx=1

And the Gaussian integral is:

∫−∞+∞e−x22 dx=2π \int_{-\infty}^{+\infty}e^{-\frac{x^2}{2}}\,dx=\sqrt{2\pi}

So: C2π=1C\sqrt{2\pi}=1

Solution:

C=12π C=\frac{1}{\sqrt{2\pi}}

Therefore, the density function of the standard normal distribution is:

f(x)=12πe−x22 f(x)=\frac{1}{\sqrt{2\pi}}e^{-\frac{x^2}{2}}

Let the random variable XX follow a normal distribution:

X∼N(μ,σ2) X\sim N(\mu,\sigma^2)

For a one-dimensional Gaussian distribution, it can be normalized to a standard normal distribution.

Let:

u=x−μσ u=\frac{x-\mu}{\sigma}

Then the standard normal density function is:

φ(u)=12πe−u22 \varphi(u)=\frac{1}{\sqrt{2\pi}}e^{-\frac{u^2}{2}}

Because:

u=x−μσ u=\frac{x-\mu}{\sigma}

So:

dudx=1σ \frac{du}{dx}=\frac{1}{\sigma}

According to the density transformation formula for continuous random variables:

fX(x)=φ(u)∣dudx∣ f_X(x)=\varphi(u)\left|\frac{du}{dx}\right|

Substituting u=x−μσu=\frac{x-\mu}{\sigma}, we get:

fX(x)=φ(x−μσ)⋅1σ f_X(x) = \varphi\left(\frac{x-\mu}{\sigma}\right)\cdot \frac{1}{\sigma}

Continue to substitute the standard normal density function:

fX(x)=12πe−12(x−μσ)2⋅1σ f_X(x) = \frac{1}{\sqrt{2\pi}} e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2} \cdot \frac{1}{\sigma}

Organized:

fX(x)=12πσe−(x−μ)22σ2 f_X(x) = \frac{1}{\sqrt{2\pi}\sigma} e^{-\frac{(x-\mu)^2}{2\sigma^2}}

Therefore, the density function of the normal distribution is:

fX(x)=12πσexp⁡(−(x−μ)22σ2) f_X(x)=\frac{1}{\sqrt{2\pi}\sigma} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)

Among them, μ\mu is the mean, σ2\sigma^2 is the variance, and σ\sigma is the standard deviation. The mean controls the center of the distribution, while the variance controls the width of the distribution.

Multidimensional Gaussian Distribution

In the embodied domain, images, hidden variables, and 2D points are usually not one-dimensional variables, but multi-dimensional vectors.

Multidimensional Gaussian distribution notation:

x∼N(μ,Σ) x \sim \mathcal{N}(\mu,\Sigma)

The density function is:

p(x)=1(2π)d∣Σ∣exp⁡(−12(x−μ)TΣ−1(x−μ)) p(x)= \frac{1} {\sqrt{(2\pi)^d|\Sigma|}} \exp \left( -\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu) \right)

Where: dd is the dimension, μ∈Rd\mu \in \mathbb{R}^d is the mean vector, and Σ∈Rd×d\Sigma \in \mathbb{R}^{d \times d} is the covariance matrix.

If the covariance is a unit matrix:

Σ=I \Sigma=I

Then it is standard multi-dimensional Gaussian:

x∼N(0,I) x \sim \mathcal{N}(0,I)

4. KL Divergence

KL divergence is used to measure the difference between two probability distributions, and is also commonly referred to as relative entropy.

Assume there are two probability distributions: true distribution: PP, approximate distribution: QQ.

Then, the KL divergence from QQ to approximately PP is defined as:

DKL(P∥Q)=∑xP(x)log⁡P(x)Q(x) D_{KL}(P \parallel Q)=\sum_x P(x)\log \frac{P(x)}{Q(x)}

If it is a continuous random variable, then:

DKL(P∥Q)=∫P(x)log⁡P(x)Q(x) dx D_{KL}(P \parallel Q)=\int P(x)\log \frac{P(x)}{Q(x)}\,dx

The KL divergence can be understood as: when the true distribution is PP, but you use QQ for encoding or modeling, the average amount of information added is greater.

That is to say: PP represents the actual situation, while QQ is your model assumption. The smaller the KL divergence, the closer QQ is to PP. If the KL divergence is 00, it indicates that the two distributions are identical.

Give a simple example

Assumption:

P=(0.5,0.5) P=(0.5,0.5)
Q=(0.9,0.1) Q=(0.9,0.1)

Then:

DKL(P∥Q)=0.5log⁡0.50.9+0.5log⁡0.50.1 D_{KL}(P \parallel Q) =0.5\log\frac{0.5}{0.9}+0.5\log\frac{0.5}{0.1}
=0.5log⁡59+0.5log⁡5 =0.5\log\frac{5}{9}+0.5\log 5

If natural logarithm is used, the result is approximately:

DKL(P∥Q)≈0.5108 D_{KL}(P \parallel Q)\approx 0.5108

The relationship between KL divergence and cross-entropy

Cross-entropy is defined as:

H(P,Q)=−∑xP(x)log⁡Q(x) H(P,Q)=-\sum_x P(x)\log Q(x)

Entropy is defined as:

H(P)=−∑xP(x)log⁡P(x) H(P)=-\sum_x P(x)\log P(x)

KL divergence satisfies:

DKL(P∥Q)=H(P,Q)−H(P) D_{KL}(P \parallel Q)=H(P,Q)-H(P)

Therefore, in machine learning, minimizing cross-entropy is equivalent to minimizing the KL divergence when PP is fixed.

Thus, it can be seen that the KL divergence measures: how much information is lost on average when approximating the true distribution PP with the distribution QQ.

II. Generate Basic Introduction Cases

Talking directly about VAE, DDPM, and FM might confuse people. I was also confused when I was learning at first. So, with a simple example, I aim to give everyone a basic understanding of the generation process, which will be very helpful for learning about diffusion models later on. Now, I will explain the basic generation steps using circular distribution. Let’s get started~

This example is ideal for beginners, as the functions, density, and sampling process of the ring are all able to be explicitly constructed. It also introduces the basics based on the generation structures of vae and ddpm. Later on, in the third part of this chapter, we will briefly explain why vae, ddpm, or other diffusion models are necessary.

We will not train any neural network yet, but use handwritten rules to simulate three generation approaches:

  1. Direct sampling: Sampling directly from the mathematical definition of the circle.

  2. Simulate VAE sampling: First, sample from the latent variable prior, and then generate circular points in one step using the decoder.

  3. Simulate DDPM sampling: Start with Gaussian noise and then remove noise step by step until it reaches the circle.

The target distribution is a two-dimensional circle: x12+x22=R2x_1^2+x_2^2=R^2, which is a circle with a radius of RR.

We want to generate a batch of 2D points:

x=(x1x2) x= \begin{pmatrix} x_1 \\ x_2 \end{pmatrix}

And these points lie on the circle with radius RR:

x12+x22=R2 x_1^2+x_2^2=R^2

For example, let’s say:

R=5 R=5

Then the goal is to generate a distribution like this:

text        * * * * *
     *             *
   *                 *
  *                   *
  *                   *
   *                 *
     *             *
        * * * * *

Method 1: Directly sample the circumference

A point on a circle can be parametrized using an angle:

x1=Rcos⁡θ x_1=R\cos\theta
x2=Rsin⁡θ x_2=R\sin\theta

If we want the points to be evenly distributed on the circle, we just need to distribute the angles uniformly:

θ∼Uniform(0,2π) \theta \sim Uniform(0,2\pi)

Then calculate:

x=(Rcos⁡θRsin⁡θ) x= \begin{pmatrix} R\cos\theta \\ R\sin\theta \end{pmatrix}

This is the most direct circular sampling. As long as you know the explicit form of the target distribution, you can sample directly.

The implementation refers to the direct.py file in the reference code, and the effect is as follows:

Method 2: Simulate VAE sampling around the circle

The generation process of VAE is usually as follows:

z∼p(z) z \sim p(z)
x∼pθ(x∣z) x \sim p_\theta(x|z)

The most common prior is the standard normal distribution:

z∼N(0,I) z \sim \mathcal{N}(0,I)

If the decoder is deterministic, it can be simplified to:

x=fθ(z) x=f_\theta(z)

In other words, the core generation logic of VAE is: first sample the latent variable z, and then generate x through the decoder.

We can design a handwritten decoder:

z∼N(0,I) z \sim \mathcal{N}(0,I)

Among them:

z=(z1z2) z= \begin{pmatrix} z_1 \\ z_2 \end{pmatrix}

Then project this 2D Gaussian point onto the circle with radius RR:

x=Rz∥z∥ x=R\frac{z}{\|z\|}

Among them:

∥z∥=z12+z22 \|z\|=\sqrt{z_1^2+z_2^2}

Expanded writing is:

x1=Rz1z12+z22 x_1=R\frac{z_1}{\sqrt{z_1^2+z_2^2}}
x2=Rz2z12+z22 x_2=R\frac{z_2}{\sqrt{z_1^2+z_2^2}}

The implementation refers to the vae_imitat.py file in the reference code, and the effect is as follows:

When vae is actually generated, it is produced in one step. This may lead to a biased average result, which is why ddpm is used. This is something that everyone should have a basic understanding of; we will discuss it in detail when we cover vae separately.

Differences between DDPM and VAE

VAE is:

z∼N(0,I) z \sim \mathcal{N}(0,I)
x=fθ(z) x=f_\theta(z)

It is generated in one step.

DDPM is:

xT∼N(0,I) x_T \sim \mathcal{N}(0,I)
xT→xT−1→⋯→x0 x_T \rightarrow x_{T-1} \rightarrow \cdots \rightarrow x_0

It is multi-step generation.

Method 3: Simulate DDPM sampling around the circle

The generation of DDPM is not completed in one step.

Its generation process is:

xT∼N(0,I) x_T \sim \mathcal{N}(0,I)

Then, perform noise reduction step by step:

xT→xT−1→xT−2→⋯→x0 x_T \rightarrow x_{T-1} \rightarrow x_{T-2} \rightarrow \cdots \rightarrow x_0

The data samples are finally obtained:

x0∼pdata x_0 \sim p_{\text{data}}

For the circular distribution, the goal is to end up with:

x12+x22≈R2 x_1^2+x_2^2 \approx R^2

The true DDPM has two processes: 1. Forward noise addition, 2. Reverse denoising.

For beginners, we can first write a manual denoising rule to simulate the sampling process of DDPM.

Core idea is:

  1. Start with Gaussian noise.

  2. Each prediction should produce a “clean circular point”.

  3. The current point approaches the predicted circular point.

  4. Add a little more noise at each step, with the noise getting smaller over time.

  5. Finally, the point falls near the circumference.

For any current point xtx_t, we can project it onto a circle with radius RR:

x^0=Rxt∥xt∥ \hat{x}_0=R\frac{x_t}{\|x_t\|}

This can be regarded as a handwritten denoising model. In the actual DDPM, x^0\hat{x}_0 is predicted by a neural network.

Let the current point be xtx_t. The predicted clean points are:

x^0=Rxt∥xt∥ \hat{x}_0=R\frac{x_t}{\|x_t\|}

Then update:

xt−1=(1−γt)xt+γtx^0+σtη x_{t-1} = (1-\gamma_t)x_t+\gamma_t\hat{x}_0+\sigma_t\eta

Among them:

η∼N(0,I) \eta \sim \mathcal{N}(0,I)

Here: γt\gamma_t controls how close each step gets to the circle, σt\sigma_t controls how much random noise is added each step. As sampling progresses, σt\sigma_t should become smaller and smaller.

The implementation refers to the ddpm_imitat.py file in the reference code, and the effect is as follows:

You will see a process:

text标准高斯噪声 -> 点逐渐被推向圆周 -> 最后形成半径为 R 的圆

III. Simplified Parts of the Actual Model

1、VAE

The true VAE is:

z∼N(0,I) z \sim \mathcal{N}(0,I)
x∼pθ(x∣z) x \sim p_\theta(x|z)

Among them, pθ(x∣z)p_\theta(x|z) is the distribution defined by the neural network decoder.

Common forms are:

pθ(x∣z)=N(μθ(z),σ2I) p_\theta(x|z)=\mathcal{N}(\mu_\theta(z),\sigma^2I)

During sampling:

x=μθ(z)+σϵ x=\mu_\theta(z)+\sigma\epsilon

Among them:

ϵ∼N(0,I) \epsilon \sim \mathcal{N}(0,I)

In our simplified version:

μθ(z) \mu_\theta(z)

Replaced with:

μ(z)=Rz∥z∥ \mu(z)=R\frac{z}{\|z\|}

2、DDPM

The forward process of the true DDPM is:

xt=αˉtx0+1−αˉtϵ x_t=\sqrt{\bar{\alpha}_t}x_0+\sqrt{1-\bar{\alpha}_t}\epsilon

Train neural network:

ϵθ(xt,t)≈ϵ \epsilon_\theta(x_t,t)\approx \epsilon

During sampling, from:

xT∼N(0,I) x_T \sim \mathcal{N}(0,I)

Start and gradually remove noise.

In our simplified version, the neural network prediction is replaced by a geometric projection:

x^0=Rxt∥xt∥ \hat{x}_0=R\frac{x_t}{\|x_t\|}

In other words, in the actual DDPM, the neural network learns to denoise x_t into x_0. In our simplified version, we directly project x_t onto a circle using handwriting rules.

In real tasks, we cannot directly obtain distributions that are similar to the circular distribution and can be expressed by mathematical formulas. Therefore, we need to use techniques such as neural network encoding-decoding and re-parameterization to learn the distribution.

How vae, ddpm, and fm specifically learn distributions, generate data, and implement cutting-edge embodied diffusion generation applications such as dreamdojo and dreamzero, as well as other embodied generation technologies like jepa and lewm, will be explained in more detail in subsequent chapters. I hope you gain some insights from this chapter and have a basic understanding of diffusion models.

Thank you all for reading. Fasten your seat belts! Let’s continue to the next chapter~