You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/week15/week15.do.txt
+197-1Lines changed: 197 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -345,12 +345,208 @@ estimated using only one random variable per timestep. As it is
345
345
computed by summing up $T-1$ consistency terms, the final estimated
346
346
value may have high variance for large $T$ values.
347
347
348
+
!split
349
+
===== PyTorch implementation of a Denoising Diffusion Probabilistic Model (DDPM) trained on the MNIST dataset =====
350
+
351
+
The code covers:
352
+
o Model definition (a simple U-Net-style convolutional network)
353
+
o Forward diffusion (adding noise over $T$ timesteps)
354
+
o Reverse denoising process
355
+
o Training loop
356
+
o Sampling from the trained model
357
+
358
+
359
+
This example is adapted from several open-source tutorials and
360
+
implementations, demonstrating how to build a diffusion model from
361
+
scratch in under 200 lines of PyTorch.
362
+
I have borrowed extensively from
363
+
o Jackson-Kang’s PyTorch diffusion tutorial, see URL:"https://github.com/Jackson-Kang/Pytorch-Diffusion-Model-Tutorial" and
364
+
o awjuliani’s PyTorch DDPM implementation, see URL:"https://github.com/awjuliani/pytorch-diffusion"
365
+
366
+
367
+
368
+
!split
369
+
===== Problem with diffusion models =====
370
+
371
+
372
+
Diffusion models gradually corrupt data by adding Gaussian noise over
373
+
a sequence of timesteps and then learn to reverse this noising process
374
+
with a neural network.
375
+
376
+
The corruption schedule is typically linear or cosine in variance.
377
+
378
+
During training, the network is optimized to predict the original
379
+
noise added at each timestep, using a mean-squared error loss.
380
+
381
+
At inference, one starts from random noise and iteratively applies the
382
+
learned denoising steps to generate new samples.
383
+
384
+
!split
385
+
===== Imports and Utilities =====
386
+
387
+
!bc pycod
388
+
import torch
389
+
import torch.nn as nn
390
+
import torch.nn.functional as F
391
+
from torchvision import datasets, transforms
392
+
from torch.utils.data import DataLoader
393
+
import matplotlib.pyplot as plt
394
+
import math
395
+
!ec
396
+
397
+
!split
398
+
===== Hyperparameters and schedules =====
399
+
400
+
!bc pycod
401
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
402
+
403
+
# Training settings
404
+
batch_size = 128
405
+
epochs = 5
406
+
lr = 2e-4
407
+
img_size = 28
408
+
channels = 1
409
+
410
+
# Diffusion hyperparameters
411
+
T = 300 # number of diffusion steps [oai_citation:5‡Medium](https://papers-100-lines.medium.com/diffusion-models-from-scratch-mnist-data-tutorial-in-100-lines-of-pytorch-code-a609e1558cee?utm_source=chatgpt.com)
412
+
beta_start, beta_end = 1e-4, 0.02
413
+
betas = torch.linspace(beta_start, beta_end, T, device=device) # linear schedule [oai_citation:6‡Medium](https://medium.com/data-science/diffusion-model-from-scratch-in-pytorch-ddpm-9d9760528946?utm_source=chatgpt.com)
0 commit comments