Practice / Regression

Regression gradients: linear, logistic and softmax

Ten problems on the gradients of linear, logistic and softmax regression in matrix form: the 2/N of the mean squared error, the normal equations and ridge, the logistic gradient Xᵀ(σ(Xw) − y)/N, its Hessian and convexity, and the softmax-regression gradient Xᵀ(S − Y)/N with shapes, with worked solutions and the mistakes that lose a factor or a transpose.

Before you start

Linear, logistic and softmax regression are one-layer networks, and their gradients are the first place the rules of the previous two pages meet a whole dataset at once. These ten problems derive each gradient in matrix form with the 1/N1/N of a mean loss kept in view, solve the linear case in closed form with and without a ridge penalty, show why the logistic loss is convex, and then extend to many classes. The five mistakes at the end are the ones that survive a quick check: a constant dropped from the gradient, a closed form copied from a loss without the 1/N1/N, an extra sigmoid derivative, a Hessian over examples instead of weights, and a weight gradient in the other library's layout.

  • The conventions are those of the previous pages: vectors are columns, Jacobians are in numerator layout, a gradient has the shape of the variable it is taken with respect to, and for a scalar LL of uu with uu a function of ww, ∇wL=(∂u/∂w)⊤∇uL\nabla_w L = (\partial u/\partial w)^\top \nabla_u L.
  • The data: NN examples with dd features each. Rows are examples: X∈RN×dX \in \mathbb{R}^{N \times d} has row nn equal to x(n)⊤x^{(n)\top}, so x(n)∈Rdx^{(n)} \in \mathbb{R}^d is example nn as a column, and XnjX_{nj} is feature jj of example nn.
  • Linear regression: targets y∈RNy \in \mathbb{R}^N, weights w∈Rdw \in \mathbb{R}^d, predictions XwXw, and the mean squared error L(w)=1N∥Xw−y∥2=1N∑n(x(n)⊤w−yn)2L(w) = \tfrac1N\|Xw - y\|^2 = \tfrac1N\sum_n (x^{(n)\top}w - y_n)^2. Ridge adds λ∥w∥2\lambda\|w\|^2 with λ>0\lambda > 0.
  • σ(t)=1/(1+e−t)\sigma(t) = 1/(1 + e^{-t}) is the logistic sigmoid; on a vector it acts entry by entry, and ⊙\odot is the elementwise product.
  • Logistic regression: labels yn∈{0,1}y_n \in \{0, 1\}, probabilities p=σ(Xw)∈RNp = \sigma(Xw) \in \mathbb{R}^N with entries pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w), per-example loss ℓn=−[ynlog⁡pn+(1−yn)log⁡(1−pn)]\ell_n = -\big[y_n\log p_n + (1 - y_n)\log(1 - p_n)\big] (binary cross-entropy, natural log), and L=1N∑nℓnL = \tfrac1N\sum_n \ell_n. D=diag⁡(p⊙(1−p))D = \operatorname{diag}(p \odot (1 - p)) is the N×NN \times N diagonal matrix of the weights pn(1−pn)p_n(1 - p_n).
  • Softmax regression with kk classes: W∈Rd×kW \in \mathbb{R}^{d \times k} has one column wiw_i per class, b∈Rkb \in \mathbb{R}^k, the logits are Z=XW+1b⊤∈RN×kZ = XW + \mathbf{1}b^\top \in \mathbb{R}^{N \times k} with 1\mathbf{1} the all-ones vector, SS is the softmax of ZZ taken row by row, YY holds the one-hot targets as rows, and L=−1N∑n,iYnilog⁡SniL = -\tfrac1N\sum_{n,i} Y_{ni}\log S_{ni}.
  • ∇w2L\nabla_w^2 L is the Hessian, the d×dd \times d matrix of second derivatives ∂2L/∂wi ∂wj\partial^2 L/\partial w_i\,\partial w_j; it is the Jacobian of ∇wL\nabla_w L.

Builds on: Matrix calculus conventions, Jacobians and the chain rule

Problems

  1. ·

    Let L(w)=1N∥Xw−y∥2L(w) = \tfrac1N\|Xw - y\|^2. Compute ∇wL\nabla_w L and check its shape.

  2. ··

    Set the gradient of Problem 1 to zero. Derive the normal equations, say when their solution is unique, and show it is a minimum. Does the 1/N1/N change the answer?

  3. ··

    Ridge: Lλ(w)=1N∥Xw−y∥2+λ∥w∥2L_\lambda(w) = \tfrac1N\|Xw - y\|^2 + \lambda\|w\|^2 with λ>0\lambda > 0. Compute ∇wLλ\nabla_w L_\lambda, solve ∇wLλ=0\nabla_w L_\lambda = 0, and show the solution exists even when the columns of XX are dependent.

  4. ·

    Show that σ′(t)=σ(t)(1−σ(t))\sigma'(t) = \sigma(t)(1 - \sigma(t)) and that 1−σ(t)=σ(−t)1 - \sigma(t) = \sigma(-t).

  5. ··

    One example: x∈Rdx \in \mathbb{R}^d, y∈{0,1}y \in \{0, 1\}, p=σ(x⊤w)p = \sigma(x^\top w) and ℓ=−[ylog⁡p+(1−y)log⁡(1−p)]\ell = -\big[y\log p + (1 - y)\log(1 - p)\big]. Compute ∇wℓ\nabla_w \ell.

  6. ··

    Batch: L=1N∑nℓnL = \tfrac1N\sum_n \ell_n with ℓn\ell_n as in Problem 5 for example nn. Write ∇wL\nabla_w L in matrix form.

  7. ···

    Compute the Hessian ∇w2L\nabla_w^2 L of the logistic loss of Problem 6. Show that it is positive semidefinite, so that LL is convex.

  8. ···

    Softmax regression: Z=XW+1b⊤Z = XW + \mathbf{1}b^\top, SS the row-wise softmax of ZZ, YY one-hot rows, L=−1N∑n,iYnilog⁡SniL = -\tfrac1N\sum_{n,i} Y_{ni}\log S_{ni}. Compute ∇ZL\nabla_Z L, ∇WL\nabla_W L and ∇bL\nabla_b L, with shapes.

  9. ···

    Take k=2k = 2 and no bias, so Z=XWZ = XW with columns w1,w2w_1, w_2. Show that Sn1=σ(x(n)⊤(w1−w2))S_{n1} = \sigma\big(x^{(n)\top}(w_1 - w_2)\big) and that the loss is the logistic loss of Problem 6 with w=w1−w2w = w_1 - w_2 and yn=Yn1y_n = Y_{n1}. What does that say about WW?

  10. ···

    Newton's method updates wnew=w−(∇w2L)−1∇wLw_{\text{new}} = w - (\nabla_w^2 L)^{-1}\nabla_w L. Write the step for logistic regression and show that the 1/N1/N cancels.

Worked solutions

Problem 1

Let L(w)=1N∥Xw−y∥2L(w) = \tfrac1N\|Xw - y\|^2. Compute ∇wL\nabla_w L and check its shape.

  1. Let r=Xw−y∈RNr = Xw - y \in \mathbb{R}^N, the residuals, so L=1Nr⊤r=1N∑nrn2L = \tfrac1N r^\top r = \tfrac1N\sum_n r_n^2 with rn=x(n)⊤w−ynr_n = x^{(n)\top}w - y_n.Naming the residual separates the square from the affine map inside it, so each can be differentiated on its own.
  2. ∇rL=2Nr\nabla_r L = \tfrac2N r, N×1N \times 1.Only the nn-th term of the sum contains rnr_n, and rn2r_n^2 differentiates to 2rn2r_n.
  3. ∂r/∂w=X\partial r/\partial w = X, N×dN \times d.rr is XX times ww minus a constant, and the Jacobian of AxAx is AA (the matrix-calculus page, Problem 2).
  4. ∇wL=(∂r/∂w)⊤∇rL=X⊤ 2Nr\nabla_w L = (\partial r/\partial w)^\top\nabla_r L = X^\top\,\tfrac2N r.LL depends on ww only through rr, so the chain rule for gradients applies.
  5. ∇wL=2NX⊤(Xw−y)\nabla_w L = \tfrac2N X^\top(Xw - y)(d×N)(N×1)=d×1(d \times N)(N \times 1) = d \times 1, the shape of ww. Entry jj is 2N∑nXnjrn\tfrac2N\sum_n X_{nj} r_n: every example's residual, weighted by its feature jj, averaged over the data.

Problem 2

Set the gradient of Problem 1 to zero. Derive the normal equations, say when their solution is unique, and show it is a minimum. Does the 1/N1/N change the answer?

  1. 2NX⊤(Xw−y)=0  ⟺  X⊤X w=X⊤y\tfrac2N X^\top(Xw - y) = 0 \iff X^\top X\,w = X^\top y.Multiply both sides by N/2≠0N/2 \neq 0. Scaling a loss by a positive constant does not move its minimiser, so the 1/N1/N changes the size of gradient steps, never where they lead.
  2. ∇w2L=2NX⊤X\nabla_w^2 L = \tfrac2N X^\top X, d×dd \times d.The gradient 2NX⊤Xw−2NX⊤y\tfrac2N X^\top X w - \tfrac2N X^\top y is affine in ww, so its Jacobian is the matrix that multiplies ww.
  3. v⊤X⊤Xv=(Xv)⊤(Xv)=∥Xv∥2≥0v^\top X^\top X v = (Xv)^\top(Xv) = \|Xv\|^2 \ge 0 for every v∈Rdv \in \mathbb{R}^d.Associativity groups the product into a vector with itself, and a squared norm cannot be negative.
  4. If the columns of XX are independent, Xv=0Xv = 0 only for v=0v = 0, so v⊤X⊤Xv>0v^\top X^\top X v > 0 for v≠0v \neq 0 and X⊤XX^\top X is invertible.XvXv is the combination of the columns of XX with coefficients vv; independence means only the zero combination vanishes. A positive definite matrix has no null space.
  5. X⊤X w=X⊤yX^\top X\,w = X^\top y; when the columns of XX are independent, w∗=(X⊤X)−1X⊤yw^* = (X^\top X)^{-1}X^\top y, the unique minimiser, because the Hessian 2NX⊤X\tfrac2N X^\top X is positive definiteA function whose Hessian is positive definite everywhere is strictly convex, so its one stationary point is the global minimum. With dependent columns (fewer examples than features, or a repeated feature) X⊤XX^\top X is singular and the minimisers form a whole affine set; Problem 3 removes that.

Problem 3

Ridge: Lλ(w)=1N∥Xw−y∥2+λ∥w∥2L_\lambda(w) = \tfrac1N\|Xw - y\|^2 + \lambda\|w\|^2 with λ>0\lambda > 0. Compute ∇wLλ\nabla_w L_\lambda, solve ∇wLλ=0\nabla_w L_\lambda = 0, and show the solution exists even when the columns of XX are dependent.

  1. ∇w(λ∥w∥2)=2λw\nabla_w\big(\lambda\|w\|^2\big) = 2\lambda w.λ∥w∥2=λ∑jwj2\lambda\|w\|^2 = \lambda\sum_j w_j^2, and only the jj-th term contains wjw_j.
  2. ∇wLλ=2NX⊤(Xw−y)+2λw\nabla_w L_\lambda = \tfrac2N X^\top(Xw - y) + 2\lambda w.The gradient of a sum is the sum of the gradients; the first term is Problem 1.
  3. Setting it to zero and multiplying by N/2N/2: X⊤X w−X⊤y+Nλ w=0X^\top X\,w - X^\top y + N\lambda\,w = 0, so (X⊤X+NλI) w=X⊤y(X^\top X + N\lambda I)\,w = X^\top y.The data term carries the 1/N1/N of the mean and the penalty does not, so clearing the 1/N1/N leaves an NN on the penalty.
  4. v⊤(X⊤X+NλI) v=∥Xv∥2+Nλ∥v∥2>0v^\top(X^\top X + N\lambda I)\,v = \|Xv\|^2 + N\lambda\|v\|^2 > 0 for every v≠0v \neq 0.The first term is ≥0\ge 0 (Problem 2, step 3) and the second is >0> 0 because Nλ>0N\lambda > 0, whatever XX is; so the matrix is positive definite and invertible.
  5. ∇wLλ=2NX⊤(Xw−y)+2λw\nabla_w L_\lambda = \tfrac2N X^\top(Xw - y) + 2\lambda w; w∗=(X⊤X+NλI)−1X⊤yw^* = (X^\top X + N\lambda I)^{-1}X^\top yStep 4 holds for every XX, including N<dN < d, so the solution always exists and is unique. For the summed loss ∥Xw−y∥2+λ′∥w∥2\|Xw - y\|^2 + \lambda'\|w\|^2 the same steps give the familiar (X⊤X+λ′I)−1X⊤y(X^\top X + \lambda' I)^{-1}X^\top y; the two agree when λ′=Nλ\lambda' = N\lambda.

Problem 4

Show that σ′(t)=σ(t)(1−σ(t))\sigma'(t) = \sigma(t)(1 - \sigma(t)) and that 1−σ(t)=σ(−t)1 - \sigma(t) = \sigma(-t).

  1. σ(t)=(1+e−t)−1\sigma(t) = (1 + e^{-t})^{-1}, so σ′(t)=e−t(1+e−t)2\sigma'(t) = \dfrac{e^{-t}}{(1 + e^{-t})^2}.Chain rule: u−1u^{-1} differentiates to −u−2-u^{-2}, and 1+e−t1 + e^{-t} to −e−t-e^{-t}; the two minus signs cancel.
  2. 1−σ(t)=1+e−t−11+e−t=e−t1+e−t1 - \sigma(t) = \dfrac{1 + e^{-t} - 1}{1 + e^{-t}} = \dfrac{e^{-t}}{1 + e^{-t}}.Put 11 over the common denominator.
  3. σ′(t)=11+e−t⋅e−t1+e−t=σ(t)(1−σ(t))\sigma'(t) = \dfrac{1}{1 + e^{-t}}\cdot\dfrac{e^{-t}}{1 + e^{-t}} = \sigma(t)(1 - \sigma(t)).Split the square in step 1's denominator; the second factor is step 2.
  4. e−t1+e−t=1et+1=σ(−t)\dfrac{e^{-t}}{1 + e^{-t}} = \dfrac{1}{e^{t} + 1} = \sigma(-t).Multiply the numerator and denominator by ete^{t}; the result is the definition of σ\sigma at −t-t.
  5. σ′(t)=σ(t)(1−σ(t))\sigma'(t) = \sigma(t)(1 - \sigma(t)); 1−σ(t)=σ(−t)1 - \sigma(t) = \sigma(-t)Both factors lie in (0,1)(0, 1) and sum to 11, so σ′(t)≤14\sigma'(t) \le \tfrac14, with equality at t=0t = 0. The second identity says the probability of class 00 is the sigmoid of the negated logit, so the two classes are treated symmetrically.

Problem 5

One example: x∈Rdx \in \mathbb{R}^d, y∈{0,1}y \in \{0, 1\}, p=σ(x⊤w)p = \sigma(x^\top w) and ℓ=−[ylog⁡p+(1−y)log⁡(1−p)]\ell = -\big[y\log p + (1 - y)\log(1 - p)\big]. Compute ∇wℓ\nabla_w \ell.

  1. Let t=x⊤wt = x^\top w, the logit, so p=σ(t)p = \sigma(t).ℓ\ell depends on ww only through the scalar tt, so the chain rule has one link per variable.
  2. ∂ℓ/∂p=−yp+1−y1−p=p−yp(1−p)\partial\ell/\partial p = -\dfrac yp + \dfrac{1 - y}{1 - p} = \dfrac{p - y}{p(1 - p)}.log⁡u\log u differentiates to 1/u1/u, and log⁡(1−p)\log(1 - p) to −1/(1−p)-1/(1 - p); over the common denominator the numerator is −y(1−p)+(1−y)p=p−y-y(1 - p) + (1 - y)p = p - y.
  3. ∂p/∂t=p(1−p)\partial p/\partial t = p(1 - p).Problem 4 at tt.
  4. ∂ℓ/∂t=p−y\partial\ell/\partial t = p - y.Chain rule, steps 2 and 3. The p(1−p)p(1 - p) cancels, which is allowed because 0<p<10 < p < 1 for every finite tt.
  5. ∇wt=x\nabla_w t = x.t=x⊤wt = x^\top w is linear in ww, and ∇w(a⊤w)=a\nabla_w(a^\top w) = a (the matrix-calculus page, Problem 3).
  6. ∇wℓ=(σ(x⊤w)−y) x\nabla_w \ell = (\sigma(x^\top w) - y)\,x∇wℓ=(∂ℓ/∂t) ∇wt\nabla_w\ell = (\partial\ell/\partial t)\,\nabla_w t, because tt is a scalar. The shape is d×1d \times 1. It is the prediction error times the input, the same form as linear regression's residual times input.

Problem 6

Batch: L=1N∑nℓnL = \tfrac1N\sum_n \ell_n with ℓn\ell_n as in Problem 5 for example nn. Write ∇wL\nabla_w L in matrix form.

  1. ∇wL=1N∑n(pn−yn) x(n)\nabla_w L = \tfrac1N\sum_n (p_n - y_n)\,x^{(n)} with pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w).The gradient of a mean is the mean of the gradients; each term is Problem 5 for example nn.
  2. For any c∈RNc \in \mathbb{R}^N, ∑ncn x(n)=X⊤c\sum_n c_n\,x^{(n)} = X^\top c.The columns of X⊤X^\top are the x(n)x^{(n)}, and a matrix times a vector combines its columns with the vector's entries as weights.
  3. p=σ(Xw)p = \sigma(Xw) has entries pnp_n.Entry nn of XwXw is row nn of XX times ww, that is x(n)⊤wx^{(n)\top}w, and σ\sigma acts entry by entry.
  4. ∇wL=1NX⊤(σ(Xw)−y)\nabla_w L = \tfrac1N X^\top(\sigma(Xw) - y)Step 2 with c=p−yc = p - y. The shape is (d×N)(N×1)=d×1(d \times N)(N \times 1) = d \times 1. It is Problem 1's gradient with the prediction XwXw replaced by σ(Xw)\sigma(Xw) and without the 22, which came from the square. Because σ\sigma is nonlinear, setting it to zero has no closed-form solution, so the weights are found iteratively (Problem 10).

Problem 7

Compute the Hessian ∇w2L\nabla_w^2 L of the logistic loss of Problem 6. Show that it is positive semidefinite, so that LL is convex.

  1. ∇wL=1N∑n(pn−yn) x(n)\nabla_w L = \tfrac1N\sum_n (p_n - y_n)\,x^{(n)}.Problem 6, step 1.
  2. ∇wpn=pn(1−pn) x(n)\nabla_w p_n = p_n(1 - p_n)\,x^{(n)}.pn=σ(tn)p_n = \sigma(t_n) with tn=x(n)⊤wt_n = x^{(n)\top}w: Problem 4 for ∂pn/∂tn\partial p_n/\partial t_n and Problem 5, step 5 for ∇wtn\nabla_w t_n.
  3. ∂∂w[(pn−yn) x(n)]=x(n) (∇wpn)⊤=pn(1−pn) x(n)x(n)⊤\dfrac{\partial}{\partial w}\big[(p_n - y_n)\,x^{(n)}\big] = x^{(n)}\,(\nabla_w p_n)^\top = p_n(1 - p_n)\,x^{(n)}x^{(n)\top}, d×dd \times d.x(n)x^{(n)} and yny_n do not depend on ww; for a scalar c(w)c(w) times a constant vector aa, entry (i,j)(i,j) of the Jacobian is ai ∂c/∂wja_i\,\partial c/\partial w_j, which is a(∇wc)⊤a(\nabla_w c)^\top.
  4. ∑nDnn x(n)x(n)⊤=X⊤DX\sum_n D_{nn}\,x^{(n)}x^{(n)\top} = X^\top D X, with Dnn=pn(1−pn)D_{nn} = p_n(1 - p_n).(X⊤DX)ij=∑nXni Dnn Xnj(X^\top D X)_{ij} = \sum_n X_{ni}\,D_{nn}\,X_{nj}, which is entry (i,j)(i,j) of the weighted sum of outer products, because Xni=xi(n)X_{ni} = x^{(n)}_i.
  5. v⊤X⊤DX v=∑npn(1−pn) (x(n)⊤v)2≥0v^\top X^\top D X\,v = \sum_n p_n(1 - p_n)\,(x^{(n)\top}v)^2 \ge 0 for every v∈Rdv \in \mathbb{R}^d.Entry nn of XvXv is x(n)⊤vx^{(n)\top}v, DD weights its square by pn(1−pn)p_n(1 - p_n), and every weight is positive because 0<pn<10 < p_n < 1.
  6. ∇w2L=1NX⊤DX\nabla_w^2 L = \tfrac1N X^\top D X with D=diag⁡(p⊙(1−p))D = \operatorname{diag}(p\odot(1 - p)), p=σ(Xw)p = \sigma(Xw); it is positive semidefinite, so LL is convexA function whose Hessian is positive semidefinite everywhere is convex, so every stationary point is a global minimum and gradient descent cannot be trapped in a worse one. With independent columns the inequality in step 5 is strict for v≠0v \neq 0 and LL is strictly convex. Unlike Problem 2's Hessian, this one changes with ww through DD.

Problem 8

Softmax regression: Z=XW+1b⊤Z = XW + \mathbf{1}b^\top, SS the row-wise softmax of ZZ, YY one-hot rows, L=−1N∑n,iYnilog⁡SniL = -\tfrac1N\sum_{n,i} Y_{ni}\log S_{ni}. Compute ∇ZL\nabla_Z L, ∇WL\nabla_W L and ∇bL\nabla_b L, with shapes.

  1. For one example with logits z∈Rkz \in \mathbb{R}^k, softmax ss and one-hot target yy at class cc: ℓ=−zc+log⁡∑iezi\ell = -z_c + \log\sum_i e^{z_i}, so ∇zℓ=s−y\nabla_z\ell = s - y.log⁡sc=zc−log⁡∑iezi\log s_c = z_c - \log\sum_i e^{z_i}; the log-sum-exp differentiates to ezj/∑iezi=sje^{z_j}/\sum_i e^{z_i} = s_j, and zcz_c to 11 exactly at j=cj = c, which is yjy_j. The next page derives this result three ways.
  2. ∇ZL=1N(S−Y)\nabla_Z L = \tfrac1N(S - Y), N×kN \times k.Row nn of ZZ feeds only ℓn\ell_n, through its own row of the softmax, and the mean puts 1N\tfrac1N on each ℓn\ell_n; row nn is step 1 for example nn.
  3. Zni=∑jXnjWji+biZ_{ni} = \sum_j X_{nj}W_{ji} + b_i, so ∂L∂Wji=∑n∂L∂Zni Xnj\dfrac{\partial L}{\partial W_{ji}} = \sum_n \dfrac{\partial L}{\partial Z_{ni}}\,X_{nj}.WjiW_{ji} sits in column ii of ZZ, in every row nn, with coefficient XnjX_{nj}: the loss reaches it through all NN examples, so their contributions add.
  4. ∑nXnj (∇ZL)ni=(X⊤∇ZL)ji\sum_n X_{nj}\,(\nabla_Z L)_{ni} = (X^\top\nabla_Z L)_{ji}.(X⊤)jn=Xnj(X^\top)_{jn} = X_{nj}, and the sum over nn is the definition of the matrix product.
  5. ∂L/∂bi=∑n(∇ZL)ni\partial L/\partial b_i = \sum_n (\nabla_Z L)_{ni}, so ∇bL=(∇ZL)⊤1\nabla_b L = (\nabla_Z L)^\top\mathbf{1}.1b⊤\mathbf{1}b^\top adds bib_i to every row of column ii with coefficient 11, and (∇ZL)⊤1(\nabla_Z L)^\top\mathbf{1} sums each column of ∇ZL\nabla_Z L.
  6. ∇WL=1NX⊤(S−Y)\nabla_W L = \tfrac1N X^\top(S - Y) (d×kd\times k); ∇bL=1N(S−Y)⊤1\nabla_b L = \tfrac1N(S - Y)^\top\mathbf{1} (kk)(d×N)(N×k)=d×k(d \times N)(N \times k) = d \times k and (k×N)(N×1)=k×1(k \times N)(N \times 1) = k \times 1, the shapes of WW and bb. Column ii of ∇WL\nabla_W L is 1NX⊤(S:,i−Y:,i)\tfrac1N X^\top(S_{:,i} - Y_{:,i}), Problem 6's form once per class, with the softmax column in place of σ(Xw)\sigma(Xw).

Problem 9

Take k=2k = 2 and no bias, so Z=XWZ = XW with columns w1,w2w_1, w_2. Show that Sn1=σ(x(n)⊤(w1−w2))S_{n1} = \sigma\big(x^{(n)\top}(w_1 - w_2)\big) and that the loss is the logistic loss of Problem 6 with w=w1−w2w = w_1 - w_2 and yn=Yn1y_n = Y_{n1}. What does that say about WW?

  1. For z∈R2z \in \mathbb{R}^2, softmax⁡(z)1=ez1ez1+ez2=11+e−(z1−z2)=σ(z1−z2)\operatorname{softmax}(z)_1 = \dfrac{e^{z_1}}{e^{z_1} + e^{z_2}} = \dfrac{1}{1 + e^{-(z_1 - z_2)}} = \sigma(z_1 - z_2).Divide the numerator and the denominator by ez1e^{z_1}.
  2. softmax⁡(z)2=1−σ(z1−z2)\operatorname{softmax}(z)_2 = 1 - \sigma(z_1 - z_2).The two entries of a softmax sum to 11.
  3. Row nn of XWXW is (x(n)⊤w1, x(n)⊤w2)\big(x^{(n)\top}w_1,\ x^{(n)\top}w_2\big), so z1−z2=x(n)⊤(w1−w2)z_1 - z_2 = x^{(n)\top}(w_1 - w_2).Column ii of XWXW is XwiXw_i, and x(n)⊤x^{(n)\top} distributes over the difference.
  4. With yn=Yn1y_n = Y_{n1} and Yn2=1−ynY_{n2} = 1 - y_n, −∑iYnilog⁡Sni=−[ynlog⁡pn+(1−yn)log⁡(1−pn)]-\sum_i Y_{ni}\log S_{ni} = -\big[y_n\log p_n + (1 - y_n)\log(1 - p_n)\big] with pn=σ(x(n)⊤w)p_n = \sigma(x^{(n)\top}w), w=w1−w2w = w_1 - w_2.A one-hot row with two classes is (yn,1−yn)(y_n, 1 - y_n); steps 1 to 3 give Sn1=pnS_{n1} = p_n and Sn2=1−pnS_{n2} = 1 - p_n.
  5. Sn1=σ(x(n)⊤(w1−w2))S_{n1} = \sigma\big(x^{(n)\top}(w_1 - w_2)\big), so two-class softmax regression is logistic regression with w=w1−w2w = w_1 - w_2Only the difference w1−w2w_1 - w_2 is determined by the data: adding the same vector to both columns leaves every probability, and so the loss, unchanged. Two-class softmax regression therefore has a whole set of minimisers where logistic regression may have one; a ridge penalty on WW picks the one with w2=−w1w_2 = -w_1. The next page meets the same shift invariance in the logits.

Problem 10

Newton's method updates wnew=w−(∇w2L)−1∇wLw_{\text{new}} = w - (\nabla_w^2 L)^{-1}\nabla_w L. Write the step for logistic regression and show that the 1/N1/N cancels.

  1. ∇wL=1NX⊤(p−y)\nabla_w L = \tfrac1N X^\top(p - y) and ∇w2L=1NX⊤DX\nabla_w^2 L = \tfrac1N X^\top D X, with p=σ(Xw)p = \sigma(Xw).Problems 6 and 7.
  2. (∇w2L)−1∇wL=N (X⊤DX)−1 1NX⊤(p−y)(\nabla_w^2 L)^{-1}\nabla_w L = N\,(X^\top D X)^{-1}\,\tfrac1N X^\top(p - y).(cA)−1=c−1A−1(cA)^{-1} = c^{-1}A^{-1} for a scalar c≠0c \neq 0. The inverse exists when the columns of XX are independent, because then X⊤DXX^\top D X is positive definite (Problem 7).
  3. wnew=w−(X⊤DX)−1X⊤(p−y)w_{\text{new}} = w - (X^\top D X)^{-1}X^\top(p - y)The NN and the 1N\tfrac1N cancel, so Newton's step is the same for the mean loss and the summed loss, while a gradient step changes by a factor of NN between them. Each step solves a weighted least-squares problem with weights DD, recomputed from the new pp: this is why the method is called iteratively reweighted least squares.

Where this goes wrong

1. Dropping the 2/N from the mean-squared-error gradient

Many texts write the least-squares loss as 12∥Xw−y∥2\tfrac12\|Xw - y\|^2, whose gradient carries no constant, and the clean form is the one that gets remembered.

  1. L(w)=1N∥Xw−y∥2L(w) = \tfrac1N\|Xw - y\|^2Right so far: the mean squared error as defined on this page.
  2. “The gradient of a least-squares loss is X⊤(Xw−y)X^\top(Xw - y).”The shortcut that causes the mistake: the gradient of 12∥Xw−y∥2\tfrac12\|Xw - y\|^2, recalled without the loss it belongs to.
  3. ∇wL=X⊤(Xw−y)\nabla_w L = X^\top(Xw - y)The gradient of the mean squared error is 2NX⊤(Xw−y)\tfrac2N X^\top(Xw - y) (Problem 1). Setting either to zero gives the same normal equations, which hides the slip; but a gradient step with this formula is N/2N/2 times too large, so the learning rate has to absorb the factor, and it changes whenever the batch size does.

2. Ridge closed form without the N

The ridge formula (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y is printed in every textbook, and a loss written as a mean looks like the same loss.

  1. Lλ(w)=1N∥Xw−y∥2+λ∥w∥2L_\lambda(w) = \tfrac1N\|Xw - y\|^2 + \lambda\|w\|^2Right so far: a mean data term plus a penalty, the form most libraries document.
  2. “The ridge solution is (X⊤X+λI)−1X⊤y(X^\top X + \lambda I)^{-1}X^\top y.”The shortcut that causes the mistake: the textbook formula belongs to the summed loss ∥Xw−y∥2+λ∥w∥2\|Xw - y\|^2 + \lambda\|w\|^2, which has no 1/N1/N on the data term.
  3. w∗=(X⊤X+λI)−1X⊤yw^* = (X^\top X + \lambda I)^{-1}X^\top yFor the mean loss the minimiser is (X⊤X+NλI)−1X⊤y(X^\top X + N\lambda I)^{-1}X^\top y (Problem 3): clearing the 1/N1/N multiplies the penalty by NN. With the formula above the penalty is NN times weaker than the one written down, and the effective regularisation shrinks as the dataset grows.

3. Logistic gradient with an extra p(1 − p)

A chain rule through a sigmoid output always produces a σ′\sigma' factor, and with a squared-error loss that factor really does stay in the gradient.

  1. ∂pn/∂tn=pn(1−pn)\partial p_n/\partial t_n = p_n(1 - p_n)Right so far: Problem 4, at the logit tn=x(n)⊤wt_n = x^{(n)\top}w.
  2. “The derivative of the loss with respect to the prediction is the error pn−ynp_n - y_n; multiply by σ′\sigma'.”The analogy that causes the mistake: p−yp - y is the derivative of the squared error 12(p−y)2\tfrac12(p - y)^2. For cross-entropy the derivative is (p−y)/(p(1−p))(p - y)/\big(p(1 - p)\big) (Problem 5, step 2), and its denominator cancels the σ′\sigma'.
  3. ∇wL=1NX⊤[(p−y)⊙p⊙(1−p)]\nabla_w L = \tfrac1N X^\top\big[(p - y)\odot p\odot(1 - p)\big]This is the gradient of the mean of 12(σ(x(n)⊤w)−yn)2\tfrac12(\sigma(x^{(n)\top}w) - y_n)^2, a different loss and not a convex one. The correct gradient is 1NX⊤(p−y)\tfrac1N X^\top(p - y) (Problem 6). The symptom: an example that is confidently wrong, with pnp_n near 11 when yn=0y_n = 0, gets a factor pn(1−pn)p_n(1 - p_n) near 00, so learning stalls exactly where the error is largest.

4. Hessian built as X D Xᵀ

Each example contributes pn(1−pn) x(n)x(n)⊤p_n(1 - p_n)\,x^{(n)}x^{(n)\top}, and an outer product xx⊤xx^\top looks as if it should become XX⊤XX^\top for the whole batch.

  1. ∇w2L=1N∑npn(1−pn) x(n)x(n)⊤\nabla_w^2 L = \tfrac1N\sum_n p_n(1 - p_n)\,x^{(n)}x^{(n)\top}Right so far: Problem 7, step 3, summed over the examples.
  2. “Stack the x(n)x^{(n)} into XX, and the sum of outer products becomes XDX⊤XDX^\top.”The analogy that causes the mistake: treating x(n)x^{(n)} as a column of XX, when with rows as examples x(n)⊤x^{(n)\top} is a row of XX.
  3. ∇w2L=1NXDX⊤\nabla_w^2 L = \tfrac1N X D X^\topIt is N×NN \times N, a matrix over pairs of examples, where the Hessian is d×dd \times d, over pairs of weights. A sum of outer products of the rows of XX is X⊤DXX^\top D X (Problem 7, step 4). When N=dN = d the shapes agree, and Newton's step (Problem 10) runs with the wrong matrix.

5. Softmax-regression gradient in the k × d layout

PyTorch's nn.Linear stores its weight as output features by input features and computes XW⊤+1b⊤XW^\top + \mathbf{1}b^\top. In that layout the weight gradient is (∇ZL)⊤X(\nabla_Z L)^\top X, and the formula travels with the reader to code where WW is d×kd \times k.

  1. ∇ZL=1N(S−Y)\nabla_Z L = \tfrac1N(S - Y), N×kN \times kRight so far: Problem 8, step 2.
  2. “The weight gradient is the upstream gradient, transposed, times the input.”The habit that causes the mistake: the rule for Z=XW⊤+1b⊤Z = XW^\top + \mathbf{1}b^\top, where WW is k×dk \times d, applied to Z=XW+1b⊤Z = XW + \mathbf{1}b^\top.
  3. ∇WL=1N(S−Y)⊤X\nabla_W L = \tfrac1N(S - Y)^\top XIt is k×dk \times d, the transpose of WW's d×kd \times k. In Z=XWZ = XW, WjiW_{ji} multiplies XnjX_{nj} into ZniZ_{ni}, so the gradient is 1NX⊤(S−Y)\tfrac1N X^\top(S - Y) (Problem 8). The entries are the right numbers transposed; when d=kd = k the update runs and silently applies the transpose.

Print this set: regression-gradients.pdf (problems, answers, and worked solutions on separate pages).