d2l-ai/d2l-zh · error · AssertionError
test_acc <= 1 and test_acc > 0.7
Error message
test_acc <= 1 and test_acc > 0.7
What it means
An AssertionError in d2l.tensorflow.train_ch3 that final test accuracy must be in (0.7, 1] for the Fashion-MNIST softmax-regression benchmark. It guards the evaluation half of the pipeline: evaluate_accuracy must run against the real test_iter, and the net must generalize past 70%. A value > 1 indicates the metric denominator is wrong; <= 0.7 indicates undertraining, divergence, or evaluating an untrained/different model.
Source
Thrown at d2l/tensorflow.py:321
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
class Updater():
"""用小批量随机梯度下降法更新参数
Defined in :numref:`sec_softmax_scratch`"""
def __init__(self, params, lr):
self.params = params
self.lr = lr
def __call__(self, batch_size, grads):
d2l.sgd(self.params, grads, self.lr, batch_size)
def predict_ch3(net, test_iter, n=6):
"""预测标签(定义见第3章)
Defined in :numref:`sec_softmax_scratch`"""
for X, y in test_iter:
breakView on GitHub (pinned to e6b18ccea7)
Solutions
- Make sure evaluate_accuracy(net, test_iter) is called on the same net object that was trained, after the training loop.
- Confirm loss matches output form: use from_logits=True only if the net returns logits (no softmax layer).
- Rebuild train_iter/test_iter with identical preprocessing (resize to 28x28, scale to [0,1]) via d2l.load_data_fashion_mnist.
- If accuracy is near 0.1 (chance), apply the fixes for the loss assert first (lr, updater scaling) — accuracy follows the loss.
Example fix
# before: net re-created before eval net = tf.keras.Model(...) # wipes trained weights train_ch3(net, ...) # test_acc <= 0.7 -> AssertionError # after: train once, evaluate the same net train_ch3(net, train_iter, test_iter, loss, 10, updater)
Defensive patterns
Strategy: validation
Validate before calling
test_acc = evaluate_accuracy(net, test_iter)
if not (0.7 < test_acc <= 1.0):
raise ValueError(f'test_acc out of expected (0.7, 1]: {test_acc:.3f}; '
f'verify same net is evaluated and preprocessing matches train') Try / catch
try:
train_ch3(net, train_iter, test_iter, loss, num_epochs, updater)
except AssertionError as e:
raise RuntimeError(f'train_ch3 test-accuracy check failed ({e}); '
f'check test_iter construction and from_logits setting') from e Prevention
- Evaluate the exact net object that was trained; never re-instantiate between train and eval.
- Build both iterators with d2l.load_data_fashion_mnist so preprocessing is identical.
- Match from_logits to your model's output (no softmax layer => from_logits=True).
- Baseline a fresh net with evaluate_accuracy — expect ~0.1 before training.
When it happens
Trigger: Passing an empty or wrong test_iter (accuracy 0/0 or NaN); evaluating a freshly re-initialized net instead of the trained one; label/shape mismatch making every prediction wrong; divergence from too-high lr dropping test accuracy to chance level (~0.1).
Common situations: Notebook cell reordering that re-creates net after training; using a test_iter built with a different preprocessing than train_iter; TF version differences in SparseCategoricalCrossentropy from_logits handling producing near-zero accuracy.
Related errors
- train_loss < 0.5
- train_acc <= 1 and train_acc > 0.7
- test_acc <= 1 and test_acc > 0.7
- train_loss < 0.5
- train_acc <= 1 and train_acc > 0.7
AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14).
Data as JSON: /api/errors/98853a7fe8050007.
Report an issue: GitHub.