d2l-ai/d2l-zh · error · AssertionError
train_loss < 0.5
Error message
train_loss < 0.5
What it means
This is a Python AssertionError raised by d2l.torch.train_ch3 after the training loop finishes. The function is a self-check from the D2L book (sec_softmax_scratch): after num_epochs of training a softmax regression / MLP on Fashion-MNIST, the final train_loss must drop below 0.5. If the model did not converge (bad lr, too few epochs, wrong loss/updater wiring), the assert fires with the actual loss value as the message.
Source
Thrown at d2l/torch.py:339
self.axes[0].cla()
for x, y, fmt in zip(self.X, self.Y, self.fmts):
self.axes[0].plot(x, y, fmt)
self.config_axes()
display.display(self.fig)
display.clear_output(wait=True)
def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):
"""训练模型(定义见第3章)
Defined in :numref:`sec_softmax_scratch`"""
animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs):
train_metrics = train_epoch_ch3(net, train_iter, loss, updater)
test_acc = evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, train_metrics + (test_acc,))
train_loss, train_acc = train_metrics
assert train_loss < 0.5, train_loss
assert train_acc <= 1 and train_acc > 0.7, train_acc
assert test_acc <= 1 and test_acc > 0.7, test_acc
def predict_ch3(net, test_iter, n=6):
"""预测标签(定义见第3章)
Defined in :numref:`sec_softmax_scratch`"""
for X, y in test_iter:
break
trues = d2l.get_fashion_mnist_labels(y)
preds = d2l.get_fashion_mnist_labels(d2l.argmax(net(X), axis=1))
titles = [true +'\n' + pred for true, pred in zip(trues, preds)]
d2l.show_images(
d2l.reshape(X[0:n], (n, 28, 28)), 1, n, titles=titles[0:n])
def evaluate_loss(net, data_iter, loss):
"""评估给定数据集上模型的损失
View on GitHub (pinned to e6b18ccea7)
Solutions
- Restore the book's hyperparameters: lr=0.1, num_epochs=10, batch_size=256, updater=d2l.sgd([{'params': net.parameters(), 'lr': 0.1}], batch_size) or torch.optim.SGD(net.parameters(), lr=0.1)
- Ensure the custom updater actually calls optimizer.step() (and zero_grad()) each batch
- Verify the net ends in nn.Linear(num_hidden, 10) and the loss is d2l.cross_entropy (mean-reduced)
- Watch the Animator: if loss oscillates, lr is too high; if flat near 2.3 (=ln 10), the model predicts uniform classes and the updater is a no-op
Example fix
# before
def updater(batch_size):
return d2l.sgd([{'params': net.parameters(), 'lr': 0.001}], batch_size)
train_ch3(net, train_iter, test_iter, loss, 10, updater) # train_loss ~2.0 -> AssertionError
# after
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
train_ch3(net, train_iter, test_iter, cross_entropy, 10, trainer.step) # loss ~0.4 Defensive patterns
Strategy: validation
Validate before calling
# pre-flight: updater must step and one batch must reduce loss
assert callable(updater)
for X, y in train_iter:
l = loss(net(X), y); l.backward(); updater(batch_size)
break
assert float(l) > 0
assert net[0].weight.grad().abs().sum() > 0 or True # params actually updated Type guard
def converged_setup(num_epochs: int, lr: float) -> bool:
return num_epochs >= 10 and 0.01 <= lr <= 0.5 Try / catch
try:
train_ch3(net, train_iter, test_iter, loss, 10, updater)
except AssertionError as e:
print(f'non-converged train_loss={e.args[0]}; check lr/epochs/updater wiring')
raise Prevention
- Use torch.optim.SGD(net.parameters(), lr=0.1) and pass trainer.step as the updater
- Never write an updater closure that omits optimizer.step()
- Keep num_epochs=10 for chapter 3 runs
- Ensure CrossEntropyLoss with default mean reduction
When it happens
Trigger: Calling train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, updater) with lr=0.001 when 0.1 is needed, num_epochs < 10, an updater that never steps (updater=lambda batch_size: None), or a torch net whose final Linear outputs the wrong number of classes for Fashion-MNIST (not 10).
Common situations: Running D2L chapter 3 notebooks with shortened epochs on slow machines; forgetting optimizer.zero_grad()/step() in a custom updater; passing an nn.CrossEntropyLoss reduction that is 'none' while train_epoch_ch3 expects a mean over the batch; mixing d2l.mxnet helpers into a torch session so the updater signature mismatches.
Related errors
- train_loss < 0.5
- train_acc <= 1 and train_acc > 0.7
- train_acc <= 1 and train_acc > 0.7
- test_acc <= 1 and test_acc > 0.7
- test_acc <= 1 and test_acc > 0.7
AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14).
Data as JSON: /api/errors/9e9aaf9baa85bf41.
Report an issue: GitHub.