Chapter 20 — Assignment Project: Three Ways to Generate Digits
Overview¶
A team needs synthetic handwritten digits to test a reading system. You will train the three families of this chapter on the same 1 797 digits, measure what each one produces, and recommend one. Each model trains in a minute or two on a CPU.
| Part | Task | Points |
|---|---|---|
| A | An evaluation protocol | 15 |
| B | A VAE | 20 |
| C | A GAN | 20 |
| D | A diffusion model | 20 |
| E | Comparison and recommendation | 25 |
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
torch.set_num_threads(4)
X, y = load_digits(return_X_y=True)
Xn = X / 16
Xt = torch.tensor(Xn, dtype=torch.float32)
def show(imgs, title, n=12):
fig, axes = plt.subplots(1, n, figsize=(n, 1.3))
for ax, im in zip(axes, imgs[:n]): ax.imshow(np.asarray(im).reshape(8, 8), cmap='gray_r', vmin=0, vmax=1); ax.axis('off')
fig.suptitle(title, fontsize=10); plt.show()
print(f'{len(X)} digits')Part A — An Evaluation Protocol (15 points)¶
Write evaluate(samples) for 1 000 generated digits (an array (1000, 64) in ). It returns a dictionary with:
recognisable: the share of samples that a logistic-regression classifier, trained on the real digits, labels with probability above 0.9;
classes: the number of digit classes that receive at least 5% of the samples (diversity: a collapsed model covers few);
balance: the entropy of the predicted class distribution divided by (1 = perfectly balanced);
novelty: the median distance from each sample to its nearest real digit.
Check it on 1 000 real digits and on 1 000 copies of the mean digit.
# Your code hereWhat does each number catch, and what does it miss?
Part B — A VAE (20 points)¶
Train a VAE with an 8-dimensional code (encoder and decoder with one hidden layer of 128 units, 150 epochs, Adam 10-3). Generate 1 000 digits from , show 12, and evaluate them.
# Your code herePart C — A GAN (20 points)¶
Train a GAN (generator 16 → 128 → 128 → 64 with a sigmoid output; discriminator 64 → 128 → 128 → 1 with LeakyReLU; Adam , ; label smoothing 0.9; 6 000 steps of batches of 64). Generate, show and evaluate 1 000 digits. Then train it again with two other seeds and report how much the evaluation changes.
# Your code hereHow stable is the GAN across seeds?
Part D — A Diffusion Model (20 points)¶
Train a denoiser (an MLP 65 → 256 → 256 → 64, the step as an extra input) with steps, from 10-4 to 0.1, on digits scaled to , for 8 000 steps of batches of 256. Generate 1 000 digits with the full DDPM sampler and with a 20-step DDIM sampler; show and evaluate both, and time them.
# Your code herePart E — Comparison and Recommendation (25 points)¶
One table: for each model, the four numbers of Part A, the training time and the time to generate 1 000 digits.
Which model would you give the team, and why? What would change your answer if they needed 10 million digits a day? Digits of a chosen class?
Name one risk of using synthetic digits to test a reading system.
# Your code hereYour recommendation:
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| A (15) | four measures, sanity checks on real and constant data, limits discussed | measures only | missing |
| B (20) | trained, samples shown, evaluated and interpreted | numbers only | missing |
| C (20) | three seeds, instability discussed | one seed | missing |
| D (20) | both samplers, quality and time compared | one sampler | missing |
| E (25) | complete table, a justified choice, scenarios and a risk | table only | missing |