%matplotlib inline
from d2l import torch as d2l
import torch
import randomBefore we train a model we need data. For pedagogy, we’ll synthesize it — known weights, known noise, and a guaranteed correct answer to compare against:
\mathbf{y} = \mathbf{X} \mathbf{w} + b + \boldsymbol{\epsilon}, \quad \boldsymbol{\epsilon} \sim \mathcal{N}(0, \sigma^2 I).
This chapter:
DataModule to generate the synthetic batch.A DataModule subclass that draws features and computes labels in __init__:
class SyntheticRegressionData(d2l.DataModule):
"""Synthetic data for linear regression."""
def __init__(self, w, b, noise=0.01, num_train=1000, num_val=1000,
batch_size=32):
super().__init__()
self.save_hyperparameters()
n = num_train + num_val
self.X = d2l.randn(n, len(w))
noise = d2l.randn(n, 1) * noise
self.y = d2l.matmul(self.X, d2l.reshape(w, (-1, 1))) + b + noiseInstantiate with the true w = [2, -3.4], b = 4.2:
Each row of features is a vector in \mathbb{R}^2; the corresponding label is a scalar:
features: tensor([-1.4973, 0.0215])
label: tensor([1.1283])
get_dataloader shuffles indices, then yields minibatches of size batch_size:
def get_dataloader(self, train):
if train:
indices = list(range(0, self.num_train))
# The examples are read in random order
random.shuffle(indices)
else:
indices = list(range(self.num_train, self.num_train+self.num_val))
for i in range(0, len(indices), self.batch_size):
batch_indices = d2l.tensor(indices[i: i+self.batch_size])
yield self.X[batch_indices], self.y[batch_indices]For real work, wrap features and labels in the framework’s built-in dataset / dataloader (workers, prefetch, GPU pinning):
Identical iteration protocol from the caller’s POV:
X shape: torch.Size([32, 2])
y shape: torch.Size([32, 1])
w, b you can compare against later.DataModule subclasses encapsulate “where do batches come from?” once, reusable across models.