def synthetic_data(w, b, noise, n):
"""n examples with standard normal features and labels Xw + b + noise."""
X = d2l.randn(n, len(w))
eps = d2l.randn(n, 1) * noise
return X, d2l.matmul(X, d2l.reshape(w, (-1, 1))) + b + epsDive into Deep Learning · §2.2
Synthetic Regression Data: Ground-Truth Verification
known parameters · Gaussian generator · noise floor · statistical estimation error
The Debugging Dilemma
Mathematical Specification
\mathbf{y} = \mathbf{X}\mathbf{w}^* + b^* + \boldsymbol{\epsilon}, \qquad X_{ij} \sim \mathcal{N}(0, 1), \quad \epsilon^{(i)} \sim \mathcal{N}(0, \sigma^2)
Vectorized Data Generation
(-1, 1): Reshaping w to (d, 1) ensures matmul(X, w) produces an (n, 1) column matching eps of shape (n, 1), avoiding an (n, n) outer-sum broadcasting catastrophe.Sanity-Checking the Data
We draw 2{,}000 examples and inspect the first training pair (\mathbf{x}^{(1)}, y^{(1)}) alongside its realized noise \epsilon^{(1)} = y^{(1)} - (\mathbf{w}^{*\top}\mathbf{x}^{(1)} + b^*):
features: (1.2794, 1.2895)
label: 2.3786, noise: 0.0042
Verify the arithmetic directly: 2 x_1^{(1)} - 3.4 x_2^{(1)} + 4.2 matches the observed label y^{(1)} up to the tiny Gaussian perturbation \epsilon^{(1)} of order \sigma = 0.01.
Geometric Intuition
If \sigma = 0.01 is tiny, why does plotting y against x_1 and x_2 show thick vertical clouds several units wide rather than razor-thin lines?
Marginal 2D scatter plots of a multivariate function always fold the variation of the unplotted features into the vertical scatter.
Theoretical Benchmark
\mathbb{E}\bigl[L(\mathbf{w}^*, b^*)\bigr] = \frac{1}{2}\,\mathbb{E}\bigl[(-\epsilon)^2\bigr] = \frac{\sigma^2}{2} = 5 \times 10^{-5}
Statistical Recovery Analysis
\hat{\tilde{\mathbf{w}}} - \tilde{\mathbf{w}}^* = \bigl(\tilde{\mathbf{X}}^\top\tilde{\mathbf{X}}\bigr)^{-1}\tilde{\mathbf{X}}^\top\boldsymbol{\epsilon}
Law of Large Numbers on the Gram Matrix
\frac{1}{n}\tilde{\mathbf{X}}^\top\tilde{\mathbf{X}} \approx \mathbf{I}_3 \quad\Longrightarrow\quad \hat{\tilde{\mathbf{w}}} - \tilde{\mathbf{w}}^* \approx \frac{1}{n}\tilde{\mathbf{X}}^\top\boldsymbol{\epsilon}
The Statistical Limit
\operatorname{SE}(\hat{w}_j) = \operatorname{SE}(\hat{b}) \approx \frac{\sigma}{\sqrt{n}} = \frac{0.01}{\sqrt{1000}} \approx 3.16 \times 10^{-4}
Gold-Standard Benchmark
We solve the normal equations on our n = 1{,}000 training examples with lstsq and compare actual parameter errors against \sigma / \sqrt{n} \approx 3 \times 10^{-4}:
error in w: (3.3e-04, 5.7e-04)
error in b: 1.1e-04
standard error: 3.2e-04
Check Your Understanding
Predict the quantitative effect of each modification to our synthetic benchmark:
Answers:
Summary
| Benchmark Quantity | Formula / Definition | Value on Running Example | Diagnostic Role in Later Sections |
|---|---|---|---|
| Data Generator | \mathbf{y} = \mathbf{X}\mathbf{w}^* + b^* + \boldsymbol{\epsilon} | \mathbf{w}^* = (2, -3.4)^\top, b^* = 4.2, \sigma = 0.01 | Ground-truth linear testbed (n = 1{,}000 train, 1{,}000 val) |
| Irreducible Loss Floor | \mathbb{E}[L(\mathbf{w}^*, b^*)] = \frac{1}{2}\sigma^2 | 5 \times 10^{-5} (or \sigma^2 = 10^{-4} without \frac{1}{2}) | Target plateau for training and validation loss curves |
| Estimation Error | \hat{\tilde{\mathbf{w}}} - \tilde{\mathbf{w}}^* = (\tilde{\mathbf{X}}^\top\tilde{\mathbf{X}})^{-1}\tilde{\mathbf{X}}^\top\boldsymbol{\epsilon} | Exact zero when \sigma = 0 | Separates statistical noise floor from optimization error |
| Parameter Standard Error | \operatorname{SE}(\hat{w}_j) \approx \sigma / \sqrt{n} | 0.01 / \sqrt{1000} \approx 3.2 \times 10^{-4} | Sets 3\text{ SE} \approx 10^{-3} pass/fail tolerance for recovered weights |