Skip to content
ML Academy

Neural networks, deep learning, NLP · insight-0405

Feed raw logits to CrossEntropyLoss, never softmax outputs

Evidence type
documented fact
Difficulty
easy
Roles
Data scientist, ML engineer, Applied AI
Industries
cross-industry

Summary: situation, action, reason, boundary

  1. Situation

    A multi-class model ends in nn.Softmax and its output goes straight into nn.CrossEntropyLoss.

  2. Action

    Return logits from forward, pass them to the loss, and apply softmax only in a separate predict function used for reporting, calibration and thresholds.

  3. Reason

    PyTorch's cross-entropy expects unnormalized logits and applies log-softmax internally in a numerically stable way.

  4. Boundary

    PyTorch classification heads; check the equivalent contract in other frameworks, where some losses take a from-logits flag.

Symptom or risk

Softmax is applied twice. Gradients shrink, training is slow and plateaus early, and reported probabilities are squeezed towards uniform.

Exceptions

If you deliberately work in log-probabilities, use log_softmax with NLLLoss, which is equivalent.

Trade-offs

A predict wrapper adds a code path that must stay consistent with training and serving.

Pitfalls

Exporting the training forward to serving and reading logits as probabilities; thresholds tuned on probabilities then fail silently.

Lessons and concepts

Claims and their sources