Course contentsShow
← Machine Learning and Deep Learning
Lesson 1552 of 3,53834. Diffusion ModelsFree lesson

Implementing DDPM Sampling in PyTorch

Coding the reverse diffusion sampling process to generate new images from random noise.

Implementing DDPM Sampling in PyTorch

You'll learn how to code the reverse diffusion process that transforms pure noise into realistic images.

The Core Idea

After training your DDPM model to predict noise at each timestep, sampling is where the magic happens: you start with random Gaussian noise and iteratively denoise it over T steps, gradually revealing a generated image. This implements the ancestral sampling algorithm you learned earlier, but now in actual PyTorch code.

The Sampling Loop

The reverse process walks backward from timestep T to 1, at each step:

  1. Feed the current noisy image and timestep into your trained denoising network
  2. Predict the noise component
  3. Remove the predicted noise (with careful scaling)
  4. Add a small amount of random noise (except at the final step)

Here's the essential implementation:

@torch.no_grad()
def sample(model, n_samples, image_size, timesteps, betas):
    device = next(model.parameters()).device
    
    # Start from pure noise
    x = torch.randn(n_samples, 3, image_size, image_size).to(device)
    
    # Reverse diffusion: T → 1
    for t in reversed(range(timesteps)):
        t_batch = torch.full((n_samples,), t, device=device, dtype=torch.long)
        
        # Predict noise
        predicted_noise = model(x, t_batch)
        
        # Compute denoising coefficients
        alpha_t = 1 - betas[t]
        alpha_cumprod_t = torch.prod(1 - betas[:t+1])
        
        # Denoise
        x = (x - ((1 - alpha_t) / torch.sqrt(1 - alpha_cumprod_t)) * predicted_noise) / torch.sqrt(alpha_t)
        
        # Add noise (except final step)
        if t > 0:
            noise = torch.randn_like(x)
            x += torch.sqrt(betas[t]) * noise
    
    return x

Key Takeaway: DDPM sampling is a backward walk through the noise schedule, where your trained network guides gradual denoising from random noise to structured images, one carefully calibrated step at a time.