Unit 4 of 6 · about 3 hours · derive

Gradients of logistic, softmax cross-entropy and regularized losses

These three gradients are the output layer of almost every classifier you will train. Derive them once and later frameworks only change the packaging.

You'll be able to

  • Derive the logistic sigmoid derivative, the binary-cross-entropy-with-logits gradient , and the softmax cross-entropy gradient minus one-hot.
  • Implement the three gradients in NumPy and confirm each against centered differences with a stated tolerance.
  • Explain why max-subtraction leaves softmax probabilities unchanged and where the naive formula overflows.

Covers

Gradients of logistic, softmax cross-entropy and regularized losses
1

Read

In this order, only the parts listed

0 of 2 done

  1. Sigmoid, quadratic and regularizer gradientsMathematics for Machine Learning - Ch. 5

    Exercise 5.2 setup (p. 170): derivative of the logistic sigmoid . Identities (5.103)-(5.108) (p. 158): , , and the weighted least-squares gradient (5.108). Least-squares linear-model pattern (5.75)-(5.78) (p. 154)

  2. Softmax classifier and numeric stabilityCS231n - Linear Classification

    Softmax classifier section: cross-entropy loss with the information-theory and MLE views. Practical issues: Numeric stability: max-subtraction trick with the [123, 456, 789] overflow example

    Read the loss formula and the stability trick only. Derive the gradient yourself in Reproduce; skip the SVM sections and the interactive demo.

2

Focus on

  • falls out of ; the factor comes from cancelling .
  • The softmax cross-entropy gradient is p minus one-hot because the logsumexp derivative is the softmax itself.
  • adds under the convention and under . State the convention before comparing numbers.
  • Max-subtraction is exact in real arithmetic since it multiplies top and bottom by one constant; it only changes float rounding, which the check measures.
Common mistakes · 3
  • Averaging versus summing the batch loss changes the gradient by . A mismatched convention looks like a wrong derivation.
  • Feeding probabilities into a logsumexp. Softmax cross-entropy needs logits; probabilities double-apply the exponential.
  • Reading softmax outputs as calibrated probabilities. The CS231n notes show that a stronger regularization shrinks the weights and makes the same example's probabilities more diffuse, so only their ordering is meaningful.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive , the binary-cross-entropy-with-logits gradient , and the softmax cross-entropy gradient . Check in float64 NumPy: with gives , loss 0.417030, grad [-0.340999, 0.242433, 0.098566]; max-subtracted softmax agrees with naive to 2.8e-17. A single logistic example (, , , ) gives and .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Derive the logistic sigmoid derivative. Which rule and fact?Answer

    . Chain rule with gives .

  2. 2.One logistic example with and : show .Answer

    and , so the product is ; times . The cancellation needs strictly inside (0, 1).

  3. 3.Derive for softmax cross-entropy with true class .Answer

    and , so the gradient is .

  4. 4.The loss uses in one source and in another. What changes in the gradient?Answer

    versus . The minimizers agree only after rescaling , so report the convention with every number.

5

Practice

You're done when

Your hand-derived sigmoid, binary-cross-entropy and softmax gradients all match centered differences to 1e-9 relative, and you can state the L2 convention from memory.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 4 more in the section
  • Mathematics for Machine Learning - Ch. 5Deisenroth, Faisal & Ong · reference

    Use Chapter 5 as the compact derivative, Jacobian, chain-rule and backpropagation reference.

  • CS231n - Linear ClassificationStanford University · support

    Read the Softmax classifier section for the cross-entropy loss formula with both the information-theory and MLE views, and Practical issues: Numeric stability for the max-subtraction trick. Derive the softmax gradient by hand; do not implement the course assignment here.

Also in this section

  • MIT 18.S096 - Matrix Calculus for MLMIT OpenCourseWare · primary

    Use lectures 1-5, the Hessian portion of lecture 7 and computational-graph portion of lecture 8; do the supplied problems and notebooks.

  • Hands-On ML - Autodiff notebookAurelien Geron · required

    Work through numerical differentiation and both AD modes, then rebuild the essential reverse-mode engine independently.

  • PyTorch Autograd mechanicsPyTorch documentation · required

    Consult after the handwritten engine for accumulation, graph behavior, disabled tracking and vector-Jacobian products.

  • MIT 18.02SC - Multivariable Calculus Unit 2MIT OpenCourseWare · optional

    Use sessions 24-38 only if partial derivatives, gradients, Hessians or the multivariable chain rule are weak.