Unit 1 of 7 · about 4 hours · explain, derive

Affine maps, nonlinearities and logits

Every network is affine maps plus nonlinearities, and the raw affine outputs (logits) feed every loss in this section. Wrong shapes or a dead activation here break everything downstream.

You'll be able to

  • Write the batched affine map with exact shapes, and derive its weight, bias and input gradients.
  • Derive ReLU, sigmoid and tanh derivatives from their definitions, and state where each saturates or dies.
  • Explain why stacked affine maps without a nonlinearity collapse to one affine map, and what logits are.

Covers

Affine maps, nonlinearities, logits and tensor shapesReLU, GELU, sigmoid and tanh behavior
1

Read

In this order, only the parts listed

0 of 2 done

  1. Shallow and deep affine structureUnderstanding Deep Learning - Chapters 3 to 7

    Chapter 3, Sections 3.1 to 3.5 (Eq 3.1 to 3.12, printed pp. 25 to 35) for the scalar, multivariate and general affine-plus-activation forms; Chapter 4, Sections 4.1 to 4.4 (Eq 4.1 to 4.16, printed pp. 41 to 49) for composed maps and matrix notation

  2. MLP math and activation curvesD2L - tensors, autograd, MLP and backpropagation

    Hidden Layers: the linear-collapse derivation , with , ; Activation Functions: ReLU, sigmoid and tanh definitions, plots and derivative maxima

    Skip the Universal Approximators discussion and the Exercises until practice. For the convention that this book calls affine maps linear, read the Chapter 3 Notes paragraph "Linear, affine, and nonlinear functions" in UDL (p. 39).

2

Focus on

  • Batched affine: , , broadcasts over rows, so has shape . The bias gradient is the row sum of the upstream gradient.
  • Without a nonlinearity, two affine layers collapse to one: substituting into gives and . Depth without activation adds no function class.
  • Sigmoid slope peaks at 0.25 at input 0; tanh peaks at 1 at input 0; both decay toward 0 for large . ReLU has slope 0 below 0 and 1 above; D2L takes the left derivative, 0, exactly at 0.
  • GELU is times the standard Gaussian CDF, smooth rather than piecewise linear, with and slope 0.5 at 0. UDL Figure 3.13 (p. 37) names it Gaussian error linear unit.
Common mistakes · 3
  • Returning itself, or its mean, as the bias gradient. The bias is broadcast over the rows, so , the sum over the batch with shape . A mean belongs only to an explicit mean in the loss.
  • Reading ReLU slope at 0 as mathematically settled. Frameworks pick a subgradient (D2L uses 0); the kink matters for gradient checks in a later unit.
  • Assuming tanh fixes the sigmoid saturation problem. It is zero-centered but still saturates, and its per-layer factor is at most 1.
3

Try it yourself

Closed-book, before checking anything

Closed-book, for , , (5,) and upstream : write shape and derive , = sum of over rows, . Then derive the sigmoid, tanh and ReLU derivatives and state each maximum.
4

Quiz yourself

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

0 got · 0 missed

  1. 1. is , is , has 5 entries. What is the shape of , and of in terms of ?Answer

    is . is times = . is the sum of over the 4 rows.

  2. 2.Why do two stacked affine layers without an activation collapse?Answer

    Substitution gives a single affine map with and , so the pair expresses nothing a single layer cannot.

  3. 3.What are the maximum slopes of the sigmoid and tanh, and where?Answer

    Sigmoid peaks at 0.25 at input 0; tanh peaks at 1 at input 0. Both approach 0 for large positive or negative inputs.

  4. 4.What are GELU(0) and the GELU slope at 0?Answer

    since 0 times is 0. The derivative is , so at 0 it equals .

5

Practice

You're done when

You write the example gradients and all three activation derivatives from memory, and both TensorTonic problems pass with failures, if any, limited to sign conventions you can fix on sight.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 11 more in the section
  • Understanding Deep Learning - Chapters 3 to 7Simon J. D. Prince · primary

    Read Chapters 3-7, emphasizing Chapter 5 for likelihood-derived losses and Chapter 7 for backpropagation and initialization. Use notebooks 3.1, 4.1, 5.1, 6.1 and 7.1 only as equation and plot companions.

  • D2L - tensors, autograd, MLP and backpropagationD2L authors · required

    Use chapter_preliminaries ndarray and autograd, then MLP, backpropagation, and numerical stability and initialization. Recreate scalar operations, affine maps, activation, reduction, stable cross-entropy, topological backward and unbroadcast in a blank file.

Also in this section

  • TensorTonic - Build Micrograd from ScratchTensorTonic · required

    Use the 25-problem Micrograd study plan in order for derivatives, gradient checks, value graphs, reverse mode, accumulation, operators, modules, losses and training. Preserve local tests as the evidence instead of a platform score.

  • Deep-ML candidate - autograd and loss problemsDeep-ML · optional

    After the five-sample platform check passes, use at most one of 26 Basic Autograd Operations, 997 Manual Cross-Entropy Backprop or 1227 Stable Softmax as a changed-prompt drill.

  • CS231n Linear Classification - Softmax classifier and numeric stabilityStanford CS231n · reference

    Read the Softmax classifier section for the cross-entropy form , then the Practical issues: Numeric stability part for the max-subtraction code f -= np.max(f).

  • CS231n Neural Networks 3 - Gradient checksStanford CS231n · reference

    Read the Gradient Checks section only: centered formula with error, relative error with thresholds, double precision, step size near 1e-5, kinks, few datapoints, burn-in, regularization and dropout discipline, and checking a few dimensions per parameter.

  • PyTorch Broadcasting semanticsPyTorch · reference

    Read General semantics for the trailing-dimension rules, prepend-1 alignment and max-size result, with the (5, 1, 4, 1) plus (3, 1, 1) examples; read In-place semantics for why an in-place tensor may not change shape.

  • `torch.autograd.gradcheck` API and tolerancesPyTorch · reference

    Read the gradcheck signature defaults (eps 1e-6, atol 1e-5, rtol 1e-3) and the note that inputs with overlapping memory, such as expand outputs, will likely fail the check.

  • `torch.nn.functional.cross_entropy` - `reduction` and `ignore_index`PyTorch · reference

    Read the ignore_index and reduction parameters: ignored targets contribute no gradient, and mean reduction averages over the non-ignored targets only.

  • PyTorch Tensor ViewsPyTorch · reference

    Read the view and storage sharing example (data_ptr, mutating the view changes the base), the transpose non-contiguous example, the full view-ops list, and the special notes: reshape may copy, contiguous copies only when needed, basic indexing returns views while advanced indexing copies.

  • `torch.Tensor.stride` - stride and storage offsetPyTorch · reference

    Read the stride definition: strides are per-dimension element steps, and with storage_offset they locate every element. Combine with the Tensor Views page for transposed and expanded layouts.

  • PyTorch Autograd mechanics - in-place operationsPyTorch · reference

    Read In-place operations with autograd (why in-place rewrites the graph and is discouraged) and In-place correctness checks (the version counter and the error on accessing a modified saved tensor).

  • `torch.nn.Module` - parameters, buffers and statePyTorch · reference

    Read the parameters and buffers documentation: modules collect parameters recursively for the optimizer, while register_buffer holds persistent non-parameter tensors saved alongside them.