liveBook · Manning

Mean squared error

As explained in Build a Text-to-Image Generator (from Scratch)

Mean squared error is the average of the squared differences between predicted and reference numerical values.

Mean squared error (MSE) averages the squared differences between predicted and reference numerical values. In diffusion training, it compares noise predicted by a denoising U-Net with the noise actually injected into images. section 6.3 section 6.3.2

A DDPM training step uses MSE in a short sequence:

  • Sample a time step and add noise to a batch of clean images. section 6.3.2
  • Give the noisy images, labels, and time information to the denoising U-Net. section 6.3
  • Compute MSE between injected noise and predicted noise, then adjust model parameters to reduce the loss. section 6.3.3

Fashion-MNIST diffusion training

A conditional DDPM trains for 20 epochs on Fashion-MNIST images. Each batch supplies clean clothing images and labels; the model predicts the added noise, and MSE supplies the loss used during optimization. section 6.3.3

Listing 6.5 Creating the DDPM
device="cuda" if torch.cuda.is_available() else "cpu"
class DDPM(nn.Module):
    def __init__(self, model, n_T, device=device, drop_prob=0.1):
        super().__init__()
        self.model = model.to(device)
        for k, v in noise_scheduler(n_T).items():
            self.register_buffer(k, v)
        self.n_T = n_T
        self.device = device
        self.drop_prob = drop_prob
        self.loss_mse = nn.MSELoss()
    def forward(self, x, c):
        _ts=torch.randint(1,self.n_T+1,
           (x.shape[0],)).to(self.device)  #1
        noise = torch.randn_like(x)     
        x_t = (self.sqrtab[_ts, None, None, None] * x
            + self.sqrtmab[_ts, 
            None, None, None] * noise)  #2
        context_mask = torch.bernoulli(torch.zeros_like(c)+
               self.drop_prob).to(self.device)   
        return self.loss_mse(noise, 
                 self.model(x_t, c, _ts / self.n_T,
                         context_mask))  #3

Inspect the DDPM implementation to see MSE instantiated as nn.MSELoss and applied to predicted and actual noise. section 6.3.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains the loss through image noising, U-Net prediction, and parameter updates, but does not derive its mathematical gradient.
Examples
Uses Fashion-MNIST clothing images, injected Gaussian noise, predicted noise, and a 20-epoch training loop.
What it assumes
Assumes familiarity with neural-network training, batches, labels, and optimization while introducing the diffusion loss workflow.
What you can do after
Build it from scratch by implementing and optimizing an MSE-based DDPM training step in PyTorch.
How it connects
Connects MSE with diffusion models, DDPMs, denoising U-Nets, noise prediction, and conditional generation.

Also explained in

Compare how 13 books explain mean squared error →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.