d2l-ai/d2l-zh · error · AssertionError

train_acc <= 1 and train_acc > 0.7

Error message

train_acc <= 1 and train_acc > 0.7

What it means

Python AssertionError from d2l.torch.train_ch3's post-training sanity check: after the final epoch, training accuracy must lie in (0.7, 1]. It guards the book's promise that softmax regression on Fashion-MNIST reaches ~0.8+ train accuracy; a value outside that band means the model underfit, diverged, or the metric computation is off.

Source

Thrown at d2l/torch.py:340

        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):
    """评估给定数据集上模型的损失

    Defined in :numref:`sec_model_selection`"""

View on GitHub (pinned to e6b18ccea7)

Solutions

  1. Use torch.optim.SGD(net.parameters(), lr=0.1) and pass trainer.step as updater
  2. Ensure loss = nn.CrossEntropyLoss() (mean reduction) and y is a LongTensor of class indices
  3. Smoke-test accuracy: accuracy(torch.tensor([[0.9,0.1]]), torch.tensor([0])) should be 1.0
  4. If train_acc is NaN, lower the lr and re-initialize the net (net.apply(weight_reset) or rebuild)

Example fix

# before
updater = lambda batch_size: None  # forgot to step
train_ch3(net, train_iter, test_iter, loss, 10, updater)  # train_acc ~0.1 -> AssertionError
# after
trainer = torch.optim.SGD(net.parameters(), lr=0.1)
train_ch3(net, train_iter, test_iter, loss, 10, trainer.step)  # train_acc ~0.85
Defensive patterns

Strategy: validation

Validate before calling

# sanity-check the accuracy pipeline on a known case
y_hat = torch.tensor([[0.9, 0.1], [0.2, 0.8]])
y = torch.tensor([0, 1])
assert d2l.accuracy(y_hat, y) == 2
assert isinstance(y, torch.LongTensor) or y.dtype in (torch.int64, torch.long), 'labels must be long indices'

Type guard

def labels_valid(y: torch.Tensor, n_classes=10) -> bool:
    return y.dtype in (torch.int64, torch.long) and y.max() < n_classes

Try / catch

try:
    train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
except AssertionError as e:
    v = e.args[0]
    print('underfit (<=0.7)' if v <= 0.7 else f'implausible acc {v}')
    raise

Prevention

When it happens

Trigger: train_acc stuck near 0.1 because the updater never updates weights (missing optimizer.step(), empty param group); accuracy computed against one-hot labels with == instead of argmax comparison; labels are float and (y_hat.argmax(axis=1) == y) broadcasting produces wrong-shaped boolean sums; lr so high the net diverges to NaN.

Common situations: Custom updater closures that only call zero_grad(); using MSELoss on class indices instead of CrossEntropyLoss; y left as float dtype from a numpy conversion so comparisons miscount; converting the d2l accuracy accumulator with cmp.type as in the book but on torch>=2.0 where .type semantics changed subtly.

Related errors


AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14). Data as JSON: /api/errors/dc1d37bb5dc7c245. Report an issue: GitHub.