Week 11 · Generative models and denoising
Learn the direction from a noisy input toward its training distribution.
← Week 10 · Subject contents · Week 12 →
Learn the direction from a noisy input toward its training distribution.
Work through the problem
Draw x from two clusters and corrupt it as z=x+σε with ε∼N(0,I). A network trained on squared error ‖f(z)−x‖² learns an approximation to E[X|Z=z]. In a cluster centered at (2,0), a noisy observation at (2.4,0.3) is likely to move toward the cluster centre.
The lab measures held-out denoising error. It also applies the denoiser to noisy draws and shows where those points move. A single denoising pass tends to average uncertain cases. A diffusion sampler instead uses a sequence of noise levels and a stochastic reverse process to model a distribution.
Run the Python lab
import torch
from torch import nn
torch.set_num_threads(1)
torch.manual_seed(8)
n = 650
sign = (torch.randint(0,2,(n,))*2-1).float()
clean = torch.randn(n,2)*.18
clean[:,0] += 2*sign
noisy = clean + .6*torch.randn_like(clean)
model = nn.Sequential(nn.Linear(2,24),nn.Tanh(),nn.Linear(24,2))
opt = torch.optim.Adam(model.parameters(),lr=.025)
for _ in range(160):
opt.zero_grad()
(model(noisy[:500])-clean[:500]).square().mean().backward()
opt.step()
with torch.no_grad():
pred = model(noisy[500:])
before = (noisy[500:]-clean[500:]).square().mean()
after = (pred-clean[500:]).square().mean()
print("Held-out noisy and denoised MSE:",round(before.item(),3),round(after.item(),3))
print("First five outputs:",pred[:5].round(decimals=2))
assert after<before
Output from the lab
Held-out noisy and denoised MSE: 0.329 0.03
First five outputs: tensor([[ 1.9700, -0.0400],
[ 2.0200, -0.0400],
[-1.5400, -0.1000],
[ 2.0900, 0.0800],
[ 2.0200, -0.0600]])
Practice
- Why is denoising MSE alone insufficient to evaluate a sampler?
Check your reasoning
- It scores point estimates under a particular noise process. It does not check whether generated draws have the right diversity or frequencies.
← Week 10 · Subject contents · Week 12 →
Further reading · Goodfellow, Bengio, and Courville · Deep Learning