Calculus & Gradients interview question

Probability & MathCalculus & GradientsBackpropagationeasy

A churn network maps four features x ∈ ℝ⁴ through a = W₁x + b₁ (W₁ is 3 × 4), h = ReLU(a) and z = W₂h + b₂ (W₂ is 2 × 3). Writing the Jacobian with entry (i, j) = ∂zᵢ/∂xⱼ, what is ∂z/∂x?

  1. AW₁ᵀ·diag(1[a > 0])·W₂ᵀ, of shape 4 × 2: backpropagation runs from the output back to the input, so the Jacobian of z with respect to x is multiplied in reverse layer order.
  2. BW₂·diag(1[a > 0])·W₁, of shape 2 × 4: Jacobians multiply in chain order with the outer layer on the left, ReLU adds a 0/1 diagonal, and biases add no term of their own.
  3. CW₂·W₁, of shape 2 × 4: ReLU is piecewise linear with slope 1, so it passes derivatives through unchanged, and the Jacobian is the same constant matrix for every input x.
  4. DW₂·diag(a)·W₁, of shape 2 × 4: the derivative of ReLU at each hidden unit equals its pre-activation a, so each unit's path is scaled by how strongly that unit fired.
Show the answer and explanation

Correct answer: B, W₂·diag(1[a > 0])·W₁, of shape 2 × 4: Jacobians multiply in chain order with the outer layer on the left, ReLU adds a 0/1 diagonal, and biases add no term of their own.

Explanation Calculus & Gradients

The multivariable chain rule multiplies Jacobians in the order the functions are composed, with the outermost function on the left:

∂z/∂x = (∂z/∂h)·(∂h/∂a)·(∂a/∂x) = W₂ · diag(1[a > 0]) · W₁

FactorJacobianShape
z = W₂h + b₂W₂2 × 3
h = ReLU(a), elementwisediag(1[a > 0])3 × 3
a = W₁x + b₁W₁3 × 4
Product∂z/∂x2 × 4

Key facts: the Jacobian of y = Wx + b with respect to x is simply W (outputs × inputs); the bias adds no term because it does not depend on x, though b₁ still shifts a and so decides which units are active. An elementwise activation has a diagonal Jacobian; for ReLU the entries are 1 for active units and 0 for inactive ones (at exactly a = 0 frameworks use 0). So the network's Jacobian is piecewise constant: it changes whenever the activation pattern changes.

Why the others fail: W₁ᵀ·diag(1[a > 0])·W₂ᵀ is the transpose of the Jacobian. It is what backpropagation multiplies by the upstream gradient (∂L/∂x = Jᵀ·∂L/∂z, a vector-Jacobian product), but it is not ∂z/∂x in the stated convention. ReLU has slope 0, not 1, for negative inputs, so it cannot be dropped. And its derivative is an indicator, not the pre-activation value itself.

Interview tip: check shapes first: a 2-output, 4-input map must have a 2 × 4 Jacobian, and inner dimensions must match (2×3 · 3×3 · 3×4). Saying that backprop computes Jᵀv without ever forming J shows you understand why it is efficient.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Probability & Math questions

  1. Each week a loyalty-app user is either Engaged or Idle. An Engaged user becomes Idle the next week with probability 0.10, and an Idle user becomes Engaged…
  2. In RLHF you need the gradient of J(θ) = E[R(y)], where y is a token sequence sampled from the policy LLM π_θ and R is a reward model's score treated as a…
  3. What is the difference between independent events and mutually exclusive events?
  4. What is the dot product of the vectors [1, 2, 3] and [4, 5, 6]?
  5. In the gradient descent update θ ← θ − η∇L(θ), what is the role of the learning rate η?