Appendix F — Solutions to Exercises

Linear Algebra for Machine Learning

E1. Dot product (§4): multiply entry-by-entry and sum, \(\mathbf{u}\cdot\mathbf{s}=3\cdot2+4\cdot1=6+4=\boxed{10}\). It is positive, so the two arrows broadly point the same way (the dot product is positive when the angle between them is under \(90^\circ\), zero at a right angle, negative when they oppose).

E2. L2 norm (§1): \(\lVert\mathbf{g}\rVert=\sqrt{5^2+12^2}=\sqrt{25+144}=\sqrt{169}=\boxed{13}\). Normalizing divides by that length: \(\mathbf{g}/\lVert\mathbf{g}\rVert=\tfrac{1}{13}[5,12]=\boxed{[5/13,\,12/13]}\approx[0.385,\,0.923]\). Check it is a unit vector: its length is \(\sqrt{(5/13)^2+(12/13)^2}=\sqrt{(25+144)/169}=\sqrt{1}=1\). ✓

E3. Cosine similarity (§4). Dot product \(\mathbf{u}\cdot\mathbf{t}=3\cdot0+4\cdot5=0+20=20\); lengths \(\lVert\mathbf{u}\rVert=\sqrt{9+16}=5\) and \(\lVert\mathbf{t}\rVert=\sqrt{0+25}=5\). So \(\cos\theta=\dfrac{20}{5\cdot5}=\dfrac{20}{25}=\boxed{0.8}\) (an angle of about \(36.9^\circ\)). At \(0.8\) it sits between the two reference pairs but on the near-aligned side — closer to the chapter’s \(0.96\) than to its orthogonal \(0\).

E4. Matrix–vector product (§3): each output entry is a row of \(A\) dotted with \(\mathbf{y}=[2,1,3]\). \[ A\mathbf{y}=\begin{bmatrix}2\cdot2+0\cdot1+1\cdot3\\ 1\cdot2+3\cdot1+0\cdot3\end{bmatrix} =\begin{bmatrix}4+0+3\\ 2+3+0\end{bmatrix}=\boxed{[7,\,5]}. \] Column blend (picture (b), §3): \(2\,\mathrm{col}_1+1\,\mathrm{col}_2+3\,\mathrm{col}_3 =2[2,1]+1[0,3]+3[1,0]=[4,2]+[0,3]+[3,0]=[7,5]\) — the same vector. ✓ (A \(2\times3\) matrix took a 3-vector to a 2-vector: “3 in, 2 out.”)

E5. Characteristic equation (§5): \[ \begin{gathered} \det(B-\lambda I)=\det\begin{bmatrix}5-\lambda&2\\2&2-\lambda\end{bmatrix}=(5-\lambda)(2-\lambda)-4\\[4pt] =\lambda^2-7\lambda+6=(\lambda-1)(\lambda-6)=0\;\Rightarrow\;\boxed{\lambda=1,\ 6}. \end{gathered} \] Trace check: \(1+6=7=5+2\), the diagonal sum. ✓ For the larger eigenvalue \(\lambda=6\), solve \((B-6I)\mathbf{v}=\begin{bmatrix}-1&2\\2&-4\end{bmatrix}\mathbf{v}=\mathbf{0}\), i.e. writing \(\mathbf{v}=[x,y]\), \(-x+2y=0\Rightarrow x=2y\), so the eigenvector is \(\boxed{[2,1]}\). (Indeed \(B[2,1]=[12,6]=6[2,1]\); the other candidate \([1,-2]\) goes with \(\lambda=1\), since \(B[1,-2]=[1,-2]\), and the two are perpendicular, as for any symmetric matrix.)

E6. Eigenvalues (§5): \(R\) has trace \(0\) and determinant \(0\cdot0-1\cdot1=-1\), so \(\lambda^2-1=0\) and the eigenvalues are \(\boxed{1\ \text{and}\ -1}\) (the eigenvector \([1,1]\) is kept, \([1,-1]\) is sent to \([-1,1]\), its own negative). Singular values (§6): \(R^{\top}R=\begin{bmatrix}0&1\\1&0\end{bmatrix}\begin{bmatrix}0&1\\1&0\end{bmatrix}=\begin{bmatrix}1&0\\0&1\end{bmatrix}=I\), whose eigenvalues are \(1\) and \(1\), so the singular values are \(\boxed{1\ \text{and}\ 1}\). The sets \(\{1,-1\}\) and \(\{1,1\}\) do not coincide, and the eigenvalue responsible is \(\boxed{-1}\). The reason: eigenvalues keep their sign, while singular values are lengths of stretches, \(\sigma_i=\sqrt{\lambda_i(A^{\top}A)}\ge0\), so a flip (here the mirror that swaps the coordinates) shows up in \(A\mathbf{v}=\lambda\mathbf{v}\) as a negative \(\lambda\) but in the SVD as the output direction \(\mathbf{u}\) being the input direction reversed.

Because \(R\) is symmetric, \(R^{\top}R=R^2\) has eigenvalues \(\lambda^2\), so \(\sigma_i=|\lambda_i|\) here; the two sets agree in absolute value, and fully only when no \(\lambda\) is negative, that is, when \(R\) is positive-semidefinite (§6), which \(R\) is not.

E7. (a) The dot products are \(c_1=\mathbf{u}\cdot\mathbf{f}_1=3\cdot2+4\cdot0=6\) and \(c_2=\mathbf{u}\cdot\mathbf{f}_2=3\cdot0+4\cdot3=12\). The sum is \(6[2,0]+12[0,3]=[12,0]+[0,36]=[12,36]\), which is not \(\mathbf{u}=[3,4]\). (b) The true coordinates solve \(a_1[2,0]+a_2[0,3]=[3,4]\), so \(2a_1=3\) and \(3a_2=4\): \(a_1=\boxed{3/2}\) and \(a_2=\boxed{4/3}\) (check: \(\tfrac32[2,0]+\tfrac43[0,3]=[3,0]+[0,4]=[3,4]\) ✓). The dot products overshoot by \(c_1/a_1=6/1.5=\boxed{4}\) and \(c_2/a_2=12/(4/3)=\boxed{9}\), which are \(\lVert\mathbf{f}_1\rVert^2=2^2\) and \(\lVert\mathbf{f}_2\rVert^2=3^2\). Perpendicularity already keeps each axis from feeling the others (that is why a coordinate exists on each axis alone), but a dot product counts the length of \(\mathbf{f}_i\) twice, once in \(\mathbf{u}\cdot\mathbf{f}_i\) and once in the arrow it multiplies, and only a unit length makes that factor \(1\). Divide each \(\mathbf{f}_i\) by its length first (\(\mathbf{e}_1=[1,0]\), \(\mathbf{e}_2=[0,1]\)) and the plain formula \(\sum_i(\mathbf{u}\cdot\mathbf{e}_i)\mathbf{e}_i=[3,0]+[0,4]=\mathbf{u}\) works.

E8. Apply §6’s recipe to \(G=\begin{bmatrix}2&2\\2&2\end{bmatrix}\). Form \[ G^{\top}G=\begin{bmatrix}2&2\\2&2\end{bmatrix}\begin{bmatrix}2&2\\2&2\end{bmatrix}=\begin{bmatrix}8&8\\8&8\end{bmatrix}, \] a symmetric matrix with trace \(16\) and determinant \(8\cdot8-8\cdot8=0\), so its eigenvalues are \(\boxed{16\ \text{and}\ 0}\). The singular values are their square roots: \(\sigma_1=\sqrt{16}=\boxed{4}\) and \(\sigma_2=\sqrt{0}=0\). One nonzero singular value means \(G\) has \(\boxed{\text{rank }1}\) — as expected, since its two rows lie on one line: they are multiples of each other (here even identical; for the chapter’s \(M\), row 2 is \(2\times\) row 1). Its single direction is \(\mathbf{u}_1=\mathbf{v}_1=\tfrac{1}{\sqrt2}[1,1]\), and rebuilding the one rank-1 piece — a column times a row, with no dot between them — gives \(\sigma_1\mathbf{u}_1\mathbf{v}_1^{\top}=4\cdot\tfrac{1}{\sqrt2}\begin{bmatrix}1\\1\end{bmatrix}\tfrac{1}{\sqrt2}[1\ \ 1]=2\begin{bmatrix}1&1\\1&1\end{bmatrix}=G\). ✓

E9. The quadratic form (§6) is \(\mathbf{x}^{\top}E\mathbf{x}\) with \(E=\begin{bmatrix}1&3\\3&1\end{bmatrix}\) and \(\mathbf{x}=[2,-1]\). Compute \(E\mathbf{x}=\begin{bmatrix}1\cdot2+3(-1)\\3\cdot2+1(-1)\end{bmatrix}=[-1,5]\), then \(\mathbf{x}^{\top}E\mathbf{x}=[2,-1]\cdot[-1,5]=-2-5=\boxed{-7}\). Because this is negative, \(E\) is not PSD — and that single \(\mathbf{x}=[2,-1]\) is the witness that proves it (PSD requires \(\mathbf{x}^{\top}E\mathbf{x}\ge0\) for every \(\mathbf{x}\); one negative value is enough to disqualify it; here it exposes \(E\)’s eigenvalue \(-2\), the other being \(4\)). Contrast the healthy \(S=\begin{bmatrix}2&1\\1&2\end{bmatrix}\) at the same vector: \(S[2,-1]=[3,0]\), so \(\mathbf{x}^{\top}S\mathbf{x}=[2,-1]\cdot[3,0]=6\ge0\) — consistent with \(S\) being PSD (its eigenvalues \(1,3\) are both \(\ge0\)).

E10. Scoring by dot product (§4), with \(\mathbf{p}=[2,1,3]\). (a) \(\hat r_1=\mathbf{p}\cdot\mathbf{q}_1=2\cdot3+1\cdot3+3\cdot0=6+3+0=9\) and \(\hat r_2=\mathbf{p}\cdot\mathbf{q}_2=2\cdot0+1\cdot1+3\cdot2=0+1+6=7\). Since \(9>7\), dot product ranks \(\mathbf{q}_1\) above \(\mathbf{q}_2\) (\(\boxed{\mathbf{q}_1\succ\mathbf{q}_2}\)). (b) Cosine needs the norms: \(\lVert\mathbf{p}\rVert=\sqrt{4+1+9}=\sqrt{14}\approx3.742\), \(\lVert\mathbf{q}_1\rVert=\sqrt{9+9+0}=\sqrt{18}\approx4.243\), \(\lVert\mathbf{q}_2\rVert=\sqrt{0+1+4}=\sqrt5\approx2.236\). Then \(\cos(\mathbf{p},\mathbf{q}_1)=\dfrac{9}{\sqrt{14}\sqrt{18}}\approx\boxed{0.567}\) and \(\cos(\mathbf{p},\mathbf{q}_2)=\dfrac{7}{\sqrt{14}\sqrt5}\approx\boxed{0.837}\). The two rankings disagree: cosine puts \(\boxed{\mathbf{q}_2\succ\mathbf{q}_1}\), the reverse of (a). The reason is length. \(\mathbf{q}_1\) is the bigger vector (\(\sqrt{18}>\sqrt5\)) and wins on the dot product for that reason, not for pointing the user’s way — measured by direction alone it is the worse match (\(0.567\) against \(0.837\)). That is the popularity/length bias §4’s “serving trick” (L2-normalize first, which turns the dot product into cosine) is designed to remove.

E11. Rank-1 collapse (§2). (a) \(\det A=4\cdot3-2\cdot6=0\) — singular. (b) \(A[1,0]=[4,6]\), while \(A[1,-2]=[4\cdot1+2(-2),\,6\cdot1+3(-2)]=[0,0]\): the direction \([1,-2]\) is killed (mapped to the origin — the null space). (c) Both columns, \([4,6]\) and \([2,3]\), are multiples of \([2,3]\) (the first is \(2\times\) the second), so every output is too: the image is the line \(\{c\,[2,3]\}\), and \(\boxed{[2,3]}\) spans it — its multiples make up that whole line.

E12. Spot the singular matrix (§2). Determinants: \(\big[\begin{smallmatrix}1&0\\0&3\end{smallmatrix}\big]\to3\); \(\big[\begin{smallmatrix}2&1\\4&2\end{smallmatrix}\big]\to2\cdot2-1\cdot4=0\); \(\big[\begin{smallmatrix}1&1\\1&2\end{smallmatrix}\big]\to2-1=1\). Only the middle matrix has \(\det=0\), so it is the singular one (its rows \([2,1],[4,2]\) are dependent — one is twice the other).

E13. Eigenvalues from trace and determinant (§5). We need \(\lambda_1+\lambda_2=5\) and \(\lambda_1\lambda_2=6\), i.e. the roots of \(\lambda^2-5\lambda+6=(\lambda-2)(\lambda-3)=0\). So \(\lambda=\boxed{2\text{ and }3}\) — recoverable from the trace and determinant alone, no matrix needed.

E14. \(B\) is symmetric with eigenvalues \(6\) and \(1\) (trace \(7\), determinant \(6\)), so its singular values are \(\sigma_1=6\), \(\sigma_2=1\), with unit eigenvectors \(\mathbf{u}_1=\mathbf{v}_1=\tfrac{1}{\sqrt5}[2,1]\) and \(\mathbf{u}_2=\mathbf{v}_2=\tfrac{1}{\sqrt5}[1,-2]\). (a) The larger piece is \(B_1=\sigma_1\mathbf{u}_1\mathbf{v}_1^{\top}=6\cdot\tfrac15\begin{bmatrix}4&2\\2&1\end{bmatrix}=\boxed{\begin{bmatrix}4.8&2.4\\2.4&1.2\end{bmatrix}}\). (b) The error is \(B-B_1=\begin{bmatrix}0.2&-0.4\\-0.4&0.8\end{bmatrix}\), whose Frobenius norm is \(\sqrt{0.04+0.16+0.16+0.64}=\sqrt{1}=\boxed{1}\) — exactly the dropped singular value \(\sigma_2=1\), as Eckart–Young promises. (With nothing kept the error would be \(\lVert B\rVert_F=\sqrt{25+4+4+4}=\sqrt{37}=\sqrt{6^2+1^2}\approx6.08\).)

E15. \(K^{\top}=\begin{bmatrix}1&0\\2&1\end{bmatrix}\). \(K^{\top}K=\begin{bmatrix}1\cdot1+0\cdot0&1\cdot2+0\cdot1\\2\cdot1+1\cdot0&2\cdot2+1\cdot1\end{bmatrix}=\boxed{\begin{bmatrix}1&2\\2&5\end{bmatrix}}\) and \(KK^{\top}=\begin{bmatrix}1\cdot1+2\cdot2&1\cdot0+2\cdot1\\0\cdot1+1\cdot2&0\cdot0+1\cdot1\end{bmatrix}=\boxed{\begin{bmatrix}5&2\\2&1\end{bmatrix}}\). They are not equal (order matters, §3), but each is symmetric, as every \(A^{\top}A\) is (§6); for \(KK^{\top}\) the same one-line transpose works, \((KK^{\top})^{\top}=(K^{\top})^{\top}K^{\top}=KK^{\top}\). For the singular values: \(K^{\top}K\) has trace \(6\) and determinant \(1\cdot5-2\cdot2=1\), so \(\lambda^2-6\lambda+1=0\) and \(\lambda=3\pm2\sqrt2\approx5.828,\ 0.172\); taking square roots, \(\sigma=\sqrt{3\pm2\sqrt2}=\boxed{1+\sqrt2\approx2.414\ \text{and}\ \sqrt2-1\approx0.414}\) (because \((1+\sqrt2)^2=3+2\sqrt2\) and \((\sqrt2-1)^2=3-2\sqrt2\)).

E16. \(N\) is not symmetric (its off-diagonal entries are \(1\) and \(0\)). It is triangular, so its eigenvalues are its diagonal entries, \(\boxed{1\ \text{and}\ 2}\). For \(\lambda=1\): \((N-I)\mathbf{v}=\begin{bmatrix}0&1\\0&1\end{bmatrix}\mathbf{v}=\mathbf{0}\) forces \(v_2=0\), so \(\mathbf{v}=[1,0]\). For \(\lambda=2\): \((N-2I)\mathbf{w}=\begin{bmatrix}-1&1\\0&0\end{bmatrix}\mathbf{w}=\mathbf{0}\) forces \(w_1=w_2\), so \(\mathbf{w}=[1,1]\). Their dot product is \([1,0]\cdot[1,1]=1\neq0\): not perpendicular. §5’s argument for symmetric matrices used \((A\mathbf{v})\cdot\mathbf{w}=\mathbf{v}\cdot(A\mathbf{w})\), which is what symmetry buys; here \((N\mathbf{v})\cdot\mathbf{w}=[1,0]\cdot[1,1]=1\) but \(\mathbf{v}\cdot(N\mathbf{w})=[1,0]\cdot[2,2]=2\), so that step fails and the eigenvectors need not be perpendicular.

E17. (a) \(\det P=4\cdot2-2\cdot3=8-6=2\neq0\), so \(P\) is invertible. (b) Swap the diagonal, negate the off-diagonal, divide by \(\det P=2\): \(P^{-1}=\tfrac12\begin{bmatrix}2&-2\\-3&4\end{bmatrix}=\boxed{\begin{bmatrix}1&-1\\-1.5&2\end{bmatrix}}\). (c) Entry by entry, \(PP^{-1}=\begin{bmatrix}4\cdot1+2(-1.5)&4(-1)+2\cdot2\\3\cdot1+2(-1.5)&3(-1)+2\cdot2\end{bmatrix}=\begin{bmatrix}1&0\\0&1\end{bmatrix}=I\) ✓. (d) \(\mathbf{x}=P^{-1}[10,8]=[1\cdot10+(-1)\cdot8,\ -1.5\cdot10+2\cdot8]=\boxed{[2,1]}\); check: \(P[2,1]=[4\cdot2+2\cdot1,\ 3\cdot2+2\cdot1]=[10,8]\) ✓.

E18. (a) \(AW\) is \((2\times3)(3\times2)\), so \(2\times2\); \(WA\) is \((3\times2)(2\times3)\), so \(3\times3\). (b) Column 1 of \(AW\) is \(A[1,2,0]=[2\cdot1+0\cdot2+1\cdot0,\ 1\cdot1+3\cdot2+0\cdot0]=[2,7]\); column 2 is \(A[0,1,3]=[2\cdot0+0\cdot1+1\cdot3,\ 1\cdot0+3\cdot1+0\cdot3]=[3,3]\). So \(AW=\boxed{\begin{bmatrix}2&3\\7&3\end{bmatrix}}\). (c) No: the shapes differ (\(2\times2\) against \(3\times3\)), so they cannot be equal; order matters even more than in §3’s \(CB\neq BC\). (d) Entry \((3,2)\) of \(WA\) is row 3 of \(W\) times column 2 of \(A\): \([0,3]\cdot[0,3]=0+9=\boxed{9}\).

Calculus for Machine Learning

E1. Derivative from the limit (§1). Expand and cancel the \(h\) in the denominator: \[ \frac{(2+h)^2-2^2}{h}=\frac{4+4h+h^2-4}{h}=\frac{4h+h^2}{h}=4+h\xrightarrow[h\to0]{}\boxed{4}. \] So \(f'(2)=4\): near \(x=2\) the output of \(x^2\) grows about \(4\) units per unit of input. The power rule agrees — \(f'(x)=2x\) gives \(2\cdot2=4\). ✓

E2. Product rule (§2) on \(f=x^2(x+1)\), with \(f_1=x^2\) (\(f_1'=2x\)) and \(f_2=x+1\) (\(f_2'=1\)): \[ f'(x)=f_1'f_2+f_1f_2'=2x(x+1)+x^2\cdot1=2x^2+2x+x^2=3x^2+2x. \] At \(x=2\): \(f'(2)=3(4)+2(2)=12+4=\boxed{16}\). Check by expanding first: \(f(x)=x^3+x^2\), so \(f'(x)=3x^2+2x\) and \(f'(2)=12+4=16\) ✓ — the two routes agree.

E3. Chain rule (§3) on \(k=(2x+1)^3=f(g(x))\): the outer \(f(u)=u^3\) has derivative \(f'(u)=3u^2\), evaluated at the inner value \(u=g(x)\), times the inner derivative \(g'(x)=2\): \[ k'(x)=\underbrace{3(2x+1)^2}_{f'(g(x))}\times\underbrace{2}_{g'(x)}=6(2x+1)^2. \] At \(x=1\): \(k'(1)=6(2\cdot1+1)^2=6\cdot3^2=6\cdot9=\boxed{54}\). Check by expanding: \(k(x)=8x^3+12x^2+6x+1\), so \(k'(x)=24x^2+24x+6\) and \(k'(1)=24+24+6=54\) ✓.

E4. Sigmoid derivative (§3). At \(z=1\), \(\sigma(1)=\tfrac{1}{1+e^{-1}}\approx\boxed{0.7311}\), so \[ \sigma'(1)=\sigma(1)\big(1-\sigma(1)\big)\approx0.7311\cdot0.2689\approx\boxed{0.197}. \] At \(z=3\), \(\sigma(3)=\tfrac{1}{1+e^{-3}}\approx\boxed{0.9526}\), so \(\sigma'(3)\approx0.9526\cdot0.0474\approx\boxed{0.045}\). The sigmoid passes back more gradient at \(z=1\) (\(0.197\)) than at \(z=3\) (\(0.045\)): the factor \(\sigma(1-\sigma)\) is largest where \(\sigma=\tfrac12\) (at \(z=0\), \(0.25\)) and shrinks as the sigmoid saturates, here by more than a factor of \(4\) (the weights multiply in separately, §3).

E5. Partial derivatives and the gradient (§4) of \(f(x,y)=x^2y+y\). Holding the other variable fixed each time: \[ \frac{\partial f}{\partial x}=2xy\quad(\text{treat }y\text{ constant}),\qquad \frac{\partial f}{\partial y}=x^2+1\quad(\text{treat }x\text{ constant}). \] At \((x,y)=(2,3)\): \(\nabla f=\big[\,2\cdot2\cdot3,\ 2^2+1\,\big]=\boxed{[12,\ 5]}\).

E6. The second-derivative test (§6).

For \(f=x^2+3y^2\): \(\nabla f=[2x,\ 6y]\) is \(\mathbf 0\) only at \((0,0)\), and \(f_{xx}=2\), \(f_{yy}=6\), \(f_{xy}=0\), so \[ H_f=\begin{bmatrix}2&0\\0&6\end{bmatrix},\qquad D=2\cdot6-0^2=\boxed{12}>0\ \text{ and }\ f_{xx}=2>0 \ \Rightarrow\ \textbf{bowl}\text{ (a minimum)}. \] \(H_f\) is the same at every point, with \(f_{xx}>0\) and \(D>0\): positive definite everywhere, so \(f\) is a convex bowl with one bottom.

For \(g=xy\): \(\nabla g=[y,\ x]\) is \(\mathbf 0\) only at \((0,0)\), and \(g_{xx}=0\), \(g_{yy}=0\), \(g_{xy}=1\), so \[ H_g=\begin{bmatrix}0&1\\1&0\end{bmatrix},\qquad D=0\cdot0-1^2=\boxed{-1}<0 \ \Rightarrow\ \textbf{saddle}. \] Check directly: along the line \(y=x\), \(g=x^2\) curves up; along \(y=-x\), \(g=-x^2\) curves down. Up one way, down another — the Hessian is indefinite, so \(g\) is not convex (the horse-saddle shape).

(Optional eigenvalues, from \(\lambda^2-(a+d)\lambda+(ad-b^2)=0\): for \(H_f\), \(\lambda^2-8\lambda+12=0\) gives \(\lambda=2,\ 6\), with product \(12=D\); for \(H_g\), \(\lambda^2-1=0\) gives \(\lambda=\pm1\), with product \(-1=D\) ✓.)

E7. Stacked sigmoids (§3). Multiplying the per-layer activation ceilings of \(0.25\) over \(6\) sigmoid layers gives at most \[ 0.25^{6}=\Big(\tfrac14\Big)^{6}=\frac{1}{4^6}=\frac{1}{64\cdot64}=\frac{1}{4096}\approx0.00024,\qquad\text{so }\boxed{1/4096} \] — the most the activation factors alone let through. By hand, \(4^3=64\), so \(4^6=4^3\cdot4^3=64\cdot64=4096\). With a weight of \(3\) at every layer, each layer’s full factor is at most \(3\cdot0.25=0.75\), and \[ 0.75^{6}\approx\boxed{0.178}. \] The chain passes back at most about \(18\%\) of the gradient: it still shrinks, though far more slowly than with the activation factors alone, because \(0.75\) is only a little below \(1\) (a weight above \(4\) could lift the factor above \(1\)).

E8. Gradient descent on \(f(x)=x^2\) (so \(f'(x)=2x\)), \(x_0=2\), \(\eta=0.1\), via \(x\leftarrow x-\eta f'(x)\) (§5): \[ x_1=2-0.1(2\cdot2)=2-0.4=\boxed{1.6},\qquad x_2=1.6-0.1(2\cdot1.6)=1.6-0.32=\boxed{1.28}, \] and \(f(x_2)=1.28^2=\boxed{1.6384}\) — the value marched downhill \(4\to2.56\to1.6384\) toward the minimum at \(0\). Taylor check of the first step (§7): the step is \(\delta=-\eta f'(x_0)=-0.4\), and the first-order predicted change is \[ f'(x_0)\cdot\delta=4\times(-0.4)=-1.6<0, \] negative, so to first order the step lowers \(f\) (predicted \(f\approx4-1.6=2.4\) vs the actual \(f(1.6)=2.56\) — close, and both below the starting \(4\)). This is the §7 argument \(f(\theta-\eta\nabla f)\approx f(\theta)-\eta\lVert\nabla f\rVert^2\le f(\theta)\) in one number; exactly, one step multiplies \(f=x^2\) by \((1-2\eta)^2=0.8^2=0.64\), and \(4\times0.64=2.56\) ✓.

E9. Jacobian (§8) of \(F(x,y)=[\,xy,\ x+y^2\,]\). With \(F_1=xy\) and \(F_2=x+y^2\), fill \(J_{ij}=\partial F_i/\partial x_j\) (row = output, column = input): \[ J=\begin{bmatrix}\partial_x(xy)&\partial_y(xy)\\[2pt]\partial_x(x+y^2)&\partial_y(x+y^2)\end{bmatrix} =\begin{bmatrix}y&x\\1&2y\end{bmatrix} \;\xrightarrow{(2,3)}\;\boxed{\begin{bmatrix}3&2\\1&6\end{bmatrix}}. \] Row \(1\) is \([\,y,\ x\,]=[\,3,\ 2\,]\) at \((2,3)\), which is exactly \(\nabla(xy)=[\partial_x(xy),\partial_y(xy)]=[y,x]\) — the gradient of the first output, just as §8 says “row \(i\) is the gradient of output \(i\).” ✓

E10. One gradient-descent step on a squared-error loss (§3 chain rule + §5 step). With \(\mathcal{L}(w)=(2w-1)^2\), differentiate by the chain rule (outer: squaring, \(u^2\to2u\); inner \(\tfrac{d}{dw}(2w-1)=2\)): \[ \frac{d\mathcal{L}}{dw}=2(2w-1)\cdot2=4(2w-1)=8w-4. \] At \(w_0=0\): \(\dfrac{d\mathcal{L}}{dw}=8(0)-4=\boxed{-4}\). The step (\(\eta=0.1\)) is \[ w\leftarrow w_0-\eta\frac{d\mathcal{L}}{dw}=0-0.1(-4)=\boxed{0.4}. \] Loss before: \(\mathcal{L}(0)=(2\cdot0-1)^2=(-1)^2=\boxed{1}\); loss after: \(\mathcal{L}(0.4)=(2\cdot0.4-1)^2=(-0.2)^2=\boxed{0.04}\). The loss dropped \(1\to0.04\) in a single step — one literal turn of the training loop \(\theta\leftarrow\theta-\eta\nabla f\) behind most learned models in the series (here on one weight; in practice on a whole parameter vector at once).

E11. Linear-approximation error (§7). With the tangent line \(T(x)=4x-4\): at \(x=2.5\), \(T=6\) and \(f=6.25\), error \(0.25\); at \(x=3\), \(T=8\) and \(f=9\), error \(1.0\). Expanding, \(f(x)-T(x)=x^2-4x+4=(x-2)^2\), and both errors match it (\(0.5^2=0.25\), \(1^2=1\)): the error is the dropped curvature term, growing with the square of the step away from \(x_0=2\).

E12. Convexity (§6). (a) \(f''=12x^2\ge0\) everywhere → convex. (b) \(f''=20x^3\) is negative for \(x<0\) and positive for \(x>0\) → not convex (it changes bending at \(x=0\)). (c) \(f''=4e^{2x}>0\) everywhere → convex. (d) \(f''=-2<0\) everywhere → a cap (concave: an upside-down bowl), not convex.

E13. Convergent learning rate (§5). (a) \(|1-8\eta|<1\) means \(-1<1-8\eta<1\), i.e. \(0<\eta<\tfrac14\): any step in that range shrinks \(x\) toward \(0\). (b) \(\eta=0.25\) gives multiplier \(1-2=-1\), so \(x\) oscillates \(x\to-x\to x\) forever and never settles; \(\eta=0.3\) gives \(1-2.4=-1.4\), so \(|x|\) is multiplied by \(1.4\) every step and diverges. (c) Here \(f''=8\), so \(2/K=2/8=\tfrac14\), the same bound as in (a).

E14. \(\lVert\hat{\mathbf d}\rVert=\tfrac15\sqrt{3^2+4^2}=\tfrac55=1\) ✓. The directional derivative is \(\nabla f\cdot\hat{\mathbf d}=[4,1]\cdot\tfrac15[3,4]=\tfrac15(12+4)=\boxed{3.2}\): positive, so \(f\) is rising that way, but more slowly than along \(x\) (slope \(4\)) — the gradient direction is the steepest.

E15. \(f_{xx}v_1^2+2f_{xy}v_1v_2+f_{yy}v_2^2=2\cdot\tfrac{9}{25}+2\cdot1\cdot\tfrac{12}{25}+0=\tfrac{18+24}{25}=\boxed{1.68}\): positive, and it lies between the two eigenvalues \(1-\sqrt2\approx-0.41\) and \(1+\sqrt2\approx2.41\), as every unit-direction curvature must.

E16. \(\nabla f=[2x,6y]=[6,6]\) at \((3,1)\), so \((x,y)\leftarrow(3-0.1\cdot6,\ 1-0.1\cdot6)=\boxed{(2.4,\ 0.4)}\). \(f(3,1)=9+3=12\) before and \(f(2.4,0.4)=5.76+0.48=\boxed{6.24}\) after. \(x\) moved by \(0.6\) of \(3\) (\(20\%\)) and \(y\) by \(0.6\) of \(1\) (\(60\%\)). For the range of \(\eta\): the slopes are \(2x\) and \(6y\), so each step multiplies \(x\) by \(1-2\eta\) and \(y\) by \(1-6\eta\), and a coordinate shrinks only when its factor lies between \(-1\) and \(1\): \(0<\eta<1\) for \(x\) and \(0<\eta<\tfrac13\) for \(y\). Both shrink exactly when \(\boxed{0<\eta<\tfrac13}\), so the more sharply curved direction sets the limit (the \(2/K\) of §7 with \(K=6\); at \(\eta=\tfrac13\) the \(y\) factor is \(-1\) and \(y\) bounces forever).

Probability & Statistics for Machine Learning

E1. Expectation and variance (§1) of the fair four-sided die, \(p(x)=\tfrac14\) for \(x\in\{1,2,3,4\}\). Mean: \(\mathbb{E}[X]=\tfrac14(1+2+3+4)=\tfrac{10}{4}=\boxed{2.5}\) — the balance point of the four bars (a value you never roll). For the variance use the shortcut \(\operatorname{Var}[X]=\mathbb{E}[X^2]-\mu^2\): \(\mathbb{E}[X^2]=\tfrac14(1+4+9+16)=\tfrac{30}{4}=7.5\), so \(\operatorname{Var}[X]=7.5-2.5^2=7.5-6.25=\boxed{1.25}\) (exactly \(5/4\)). (Smaller than the six-sided die’s \(35/12\approx2.92\), as it should be — four faces spread less than six.)

E2. Naming the distributions (§2). (a) One yes/no outcome is a Bernoulli, parameter \(p\). (b) One pick out of \(5\) unordered labels is a Categorical, parameters \(p_1,\dots,p_5\) (summing to \(1\)). (c) A real value with a few wildly-off entries is a Laplace, parameters \(\mu,b\). (d) A belief about a probability is a Beta; the pseudo-count reading “\(\alpha-1\) heads, \(\beta-1\) tails already seen” with \(6\) heads and \(2\) tails gives \(\alpha-1=6,\ \beta-1=2\), i.e. \(\boxed{\text{Beta}(7,3)}\), whose mean is \(\mathbb{E}[\theta]=\dfrac{\alpha}{\alpha+\beta}=\dfrac{7}{10}=\boxed{0.7}\) (leans toward heads, as “6 heads vs 2 tails” should; not \(6/8=0.75\): that is the peak, the mode \((7-1)/(7+3-2)=0.75\), and the mean sits a little nearer the middle, as §2’s Beta\((2,5)\) showed). Finally, the Bernoulli of (a) with \(p=0.8\): \(\mathbb{E}[X]=p=\boxed{0.8}\) and \(\operatorname{Var}[X]=p(1-p)=0.8\cdot0.2=\boxed{0.16}\).

E3. Odds → logit → sigmoid (§3) for the \(p=0.7\) coin. (a) Odds \(=\dfrac{p}{1-p}=\dfrac{0.7}{0.3}=\dfrac73\approx\boxed{2.333}\) (\(2.33\times\) as likely to land heads as not). (b) Logit \(=\ln\!\big(\tfrac{p}{1-p}\big)=\ln\tfrac73\approx\boxed{0.847}\). (c) Feed that score \(z=\ln\tfrac73\) back through the sigmoid: \(e^{-z}=\tfrac37=0.4286\), so \(\sigma(z)=\dfrac{1}{1+0.4286}=\dfrac{1}{1.4286}=\boxed{0.7}\) ✓ — exactly the \(p\) we started from, because the sigmoid is the logit’s inverse (and the odds are exactly \(e^{z}=\tfrac73\)). (d) \(\sigma(3)=\dfrac{1}{1+e^{-3}}=\dfrac{1}{1+0.0498}=\boxed{0.953}\): a score of \(3\) is odds of \(e^{3}\approx20\) to \(1\), so a probability a little above \(0.95\).

E4. Softmax (§3) of \(\mathbf z=[1,0,0]\). Exponentiate: \(e^{1},e^{0},e^{0}=2.718,\,1,\,1\), summing to \(4.718\). Divide each by the total: \[ \operatorname{softmax}(\mathbf z)=\Big[\tfrac{2.718}{4.718},\,\tfrac{1}{4.718},\,\tfrac{1}{4.718}\Big] =\boxed{[0.576,\,0.212,\,0.212]}, \] and \(0.576+0.212+0.212=1.000\) ✓. Class \(A\) wins (it had the largest score), and \(B\) and \(C\) are equal because each class gets \(e^{z_c}\) over the same total \(4.718\), and \(z_B=z_C=0\) gives both \(e^{0}=1\), so both get \(1/4.718=0.212\). The top score takes the lion’s share but the others keep nonzero probability (a soft max, not a hard one).

E5. MLE for the coin (§4): 6 heads, 2 tails, \(L(\theta)=\theta^{6}(1-\theta)^{2}\), \(\log L(\theta)=6\ln\theta+2\ln(1-\theta)\). Set the derivative to zero, using the toolkit’s slopes \(\frac{d}{d\theta}\ln\theta=\frac1\theta\) and \(\frac{d}{d\theta}\ln(1-\theta)=-\frac{1}{1-\theta}\): \[ \frac{d\log L}{d\theta}=\frac{6}{\theta}-\frac{2}{1-\theta}=0\;\Rightarrow\;6(1-\theta)=2\theta\;\Rightarrow\;6=8\theta \;\Rightarrow\;\hat\theta_{\text{MLE}}=\frac{6}{8}=\boxed{0.75}. \] So the MLE is just the observed frequency \(6/8\). Hand-check at \(\theta=0.75\): \(\tfrac{6}{0.75}=8\) and \(\tfrac{2}{0.25}=8\) — the two terms cancel ✓.

E6. Bayes’ rule, a bot detector (§5’s test with new numbers: prior \(0.02\), sensitivity \(0.95\), false-positive rate \(0.10\)). (a) Evidence by the law of total probability: \[ \begin{aligned} \Pr(+)&=\Pr(+\mid S)\Pr(S)+\Pr(+\mid\neg S)\Pr(\neg S)\\[4pt] &=0.95(0.02)+0.10(0.98)=0.019+0.098=\boxed{0.117}. \end{aligned} \] Then the posterior: \[ \Pr(S\mid+)=\frac{\Pr(+\mid S)\Pr(S)}{\Pr(+)}=\frac{0.019}{0.117}\approx\boxed{0.162}. \] (b) Counting over \(10{,}000\) accounts: the \(2\%\) prior makes 200 bots, of whom the detector catches \(0.95\times200=\) 190 (true positives); of the 9,800 humans, \(10\%=\) 980 are flagged anyway (false positives). Total flagged \(=190+980=1170\), of which only \(190\) are bots: \(190/1170\approx\boxed{0.162}\) — the same \(\sim16\%\) ✓. It is far below the detector’s \(95\%\) sensitivity because bots are rare (low prior), so the \(980\) false positives from the large human majority swamp the \(190\) true ones — the base-rate effect. (Its accuracy, the share of all accounts it labels correctly, is \(0.019+0.882=90.1\%\), a different number again.)

E7. MLE vs MAP — the prior as a regularizer (§6). Same coin as E5 (6 heads, 2 tails) with a Beta\((4,4)\) prior. (a) Beta is conjugate to Bernoulli, so the posterior just adds the data counts to the prior exponents: \(\theta^{6}(1-\theta)^{2}\cdot\theta^{3}(1-\theta)^{3}=\theta^{9}(1-\theta)^{5}\), i.e. \(\boxed{\text{Beta}(10,6)}\) (\(\alpha_{\text{post}}=4+6=10,\ \beta_{\text{post}}=4+2=6\)). (b) The MAP estimate is the mode of Beta\((10,6)\): \(\dfrac{\alpha_{\text{post}}-1}{\alpha_{\text{post}}+\beta_{\text{post}}-2}=\dfrac{9}{14}\approx\boxed{0.643}\). (c) The three numbers — MLE \(=0.75\), prior mean \(=0.5\), MAP \(\approx0.643\) — show the prior pulled the estimate down from \(0.75\) toward \(0.5\), landing at \(0.643\).

That is the same kind of pull toward a center as an L2 regularizer, which pulls a weight from its data-driven value toward \(0\) (here the “toward \(0.5\)” prior plays the role of “toward \(0\)”). With far more data the likelihood would dominate and MAP would slide back toward the MLE — a prior is a finite nudge, washed out by enough evidence.

  1. With the Beta\((10,10)\) prior, \(k\) heads and \(8-k\) tails give the posterior Beta\((10+k,\,18-k)\), whose mode is \(\dfrac{9+k}{26}\). It exceeds \(0.6\) once \(9+k>15.6\), so the MAP needs \(\boxed{7}\) heads: \(16/26=0.615\), while \(6\) heads give \(15/26=0.577\). A firm prior takes more evidence to move.

E8. Entropy, cross-entropy, KL (§7) for truth \(p=[0.5,0.5]\), model \(q=[0.7,0.3]\). (a) Entropy in bits: \(H(p)=-0.5\log_2 0.5-0.5\log_2 0.5=-\log_2 0.5=\boxed{1}\) bit — the maximal uncertainty of a fair coin. (b) Cross-entropy in nats: \[ \begin{aligned} H(p,q)&=-\big(0.5\ln 0.7+0.5\ln 0.3\big)=-\big(0.5(-0.357)+0.5(-1.204)\big)\\[4pt] &=0.178+0.602=\boxed{0.780}\ \text{nats}. \end{aligned} \] (c) KL divergence in nats: \[ D_{\mathrm{KL}}(p\Vert q)=0.5\ln\frac{0.5}{0.7}+0.5\ln\frac{0.5}{0.3} =0.5(-0.336)+0.5(0.511)=-0.168+0.255=\boxed{0.087}\ \text{nats}. \] (d) Identity check: \(H(p,q)-H(p)=0.780-0.693=0.087=D_{\mathrm{KL}}(p\Vert q)\) ✓ (using \(H(p)=0.693\) nats, which is the same as the \(1\) bit from (a)). KL is the extra surprise from coding with the wrong \(q\) instead of the truth \(p\); this \(q\) is closer to the truth than §7’s \([0.8,0.2]\) (\(0.223\) nats), so it pays less.

E9. Paired \(t\)-test (§8) on \(d=[3,5,4,6,2]\), \(n=5\). (a) \(\bar d=\dfrac{3+5+4+6+2}{5}=\dfrac{20}{5}=\boxed{4}\) milli-NDCG@10. (b) Deviations from the mean: \([-1,1,0,2,-2]\); sum of squares \(=1+1+0+4+4=10\), so \(s_d^2=\dfrac{10}{5-1}=2.5\) and \(s_d=\sqrt{2.5}\approx\boxed{1.581}\). (c) Standard error \(=\dfrac{s_d}{\sqrt n}=\dfrac{1.581}{\sqrt5}\approx0.707\), so \(t=\dfrac{\bar d}{s_d/\sqrt n}=\dfrac{4}{0.707}\approx\boxed{5.657}\) with \(df=n-1=4\). Since \(5.657>2.776\) (the critical value for df \(=4\)), we reject \(H_0\) — the gain is statistically significant (two-sided \(p\approx0.0048\)). (d) 95% CI: \(\bar d\pm 2.776\,(s_d/\sqrt n)=4\pm 2.776(0.707)=4\pm1.963=\boxed{[2.04,\,5.96]}\) milli-NDCG@10, which excludes \(0\) ✓ — the same significant verdict as (c), now with the effect size and its uncertainty in one object.

E10. The standard error and how it shrinks (§8.3). (a) The standard error is \(s_d/\sqrt n\), so going from \(n=5\) to \(n=10\) (twice the pairs, \(s_d\) and \(\bar d\) unchanged) shrinks it by \(\sqrt{10/5}=\sqrt2\approx\boxed{1.41}\) (from \(\approx0.707\) to \(0.500\)). Because \(t=\bar d/(s_d/\sqrt n)\) has that shrinking quantity in the denominator, \(t\) grows by the same factor (here \(5.66\to8.00\); the critical value also falls as df rises from \(4\) to \(9\), from \(2.776\) to \(2.262\), which is what scipy.stats.t.ppf(0.975, 9) returns, as in §8.3), so a real gain sits more standard errors above \(0\) and is easier to detect — i.e. more seeds give the test more power.

  1. The variances of independent differences add, so ten of them, each with variance \(4\), sum to variance \(10\times4=40\); dividing the sum by \(10\) to get the average divides that variance by \(10^2=100\), leaving \(\boxed{0.4}\), which is \(4/10\): the variance shrinks by the factor \(n=10\). The standard deviation is the square root, \(\sqrt{0.4}\approx\boxed{0.632}=2/\sqrt{10}\): it shrinks only by \(\sqrt n=\sqrt{10}\approx3.16\), from \(2\) to \(0.632\). That \(\sigma/\sqrt n\) is the standard error \(s_d/\sqrt n\), with the unknown \(\sigma\) estimated by \(s_d\). (The Central Limit Theorem, §8.3’s Fact 2, is a separate fact: it says the average is approximately bell-shaped, and it does not supply the \(\sqrt n\).)

E11. Wilcoxon by hand (§8.7). (a) \(|d|=\{3,1,5,2,4,6\}\) sorted is \(1,2,3,4,5,6\), so matched back to the data, \(d=+3\) gets rank \(3\), \(+1\) rank \(1\), \(+5\) rank \(5\), \(-2\) rank \(2\), \(+4\) rank \(4\), \(+6\) rank \(6\). (b) The only negative is \(-2\) (rank \(2\)), so \(W^{-}=2\) (and \(W^{+}=1+3+4+5+6=19\)).

  1. \(W=\min(19,2)=2\). Because one sign disagrees, \(W\neq0\), so we are above the \(n=6\) floor — and already \(W=1\) has \(p=4/64=0.0625>0.05\), so \(W=2\) cannot be significant. Exactly: of the \(64\) equally likely sign patterns, \(6\) are at least as extreme as \(W=2\) (two each with \(W=0\), \(1\) and \(2\), counting both directions), so the two-sided \(p\) is \(6/64\approx0.094\) — not significant at \(\alpha=0.05\). (Only an all-same-sign set, \(W=0\), reaches \(p=0.031\).) Five wins were promising, but six seeds with one loss cannot clear the bar — collect more seeds.

E12. Ship-or-not with multiplicity (§8.7). (a) Yes, barely — the \(95\%\) CI \([+0.0003,+0.0077]\) excludes \(0\), so judged as a single test the \(+0.004\) gain clears the bar, consistent with its raw \(p=0.04<0.05\); but its lower end allows a gain of only \(0.0003\) (\(0.3\) milli-NDCG@10), §8.6 Step 4’s warning about width. (b) With \(m=4\) tests, Bonferroni compares each \(p\) to \(\alpha/m=0.05/4=0.0125\); since \(0.04>0.0125\), it does not survive.

  1. Do not ship — and this is the case where the single-test reading (a) and the multiplicity-corrected reading (b) disagree. Taken alone, that interval says the gain is positive at \(\alpha=0.05\); but that interval and that \(p\) were the best of four tries: if none of the four models were better, at least one of the four tests would still give \(p<0.05\) about \(19\%\) of the time (\(1-0.95^{4}\), independent tests, §8.5), so luck is a live explanation — the gain may be real, but these data cannot tell it apart from luck. Re-run the single most promising model on fresh seeds before believing it.

(A check beyond this chapter: a CI excludes \(0\) exactly when the test at the same level rejects (§8.6, Step 4), so Bonferroni’s level \(0.05/4=0.0125\) goes with a \(98.75\%\) interval, whose critical value on \(4\) df is \(t^{*}=4.315\), scipy.stats.t.ppf(1-0.0125/2, 4). With the standard error \(0.0037/2.776=0.001333\), where \(0.0037\) is the half-width of \([0.0003,\,0.0077]\), it becomes \(0.004\pm4.315\times0.001333=[-0.00175,+0.00975]\), straddling \(0\).)

Loss Functions & Regularizers

E1. MAE/MSE/Huber on the residual set \(r=\{0.5,-2,1\}\) (§2.1), with \(N=3\) and \(\delta=1\). The MAE is the mean of \(|r|\): \(\tfrac13(0.5+2+1)=\tfrac{3.5}{3}=\boxed{1.167}\). The MSE is the mean of \(r^2\): \(\tfrac13(0.25+4+1)=\tfrac{5.25}{3}=\boxed{1.75}\). For Huber (\(\delta=1\)), the two small residuals \(|r|\le1\) use the quadratic \(\tfrac12 r^2\) (\(0.5\mapsto0.125\), \(1\mapsto0.5\)), while the outlier \(r=-2\) has \(|r|>1\) so it uses the linear arm \(\delta|r|-\tfrac12\delta^2=1\cdot2-0.5=1.5\); the mean is \(\tfrac13(0.125+1.5+0.5)=\tfrac{2.125}{3}=\boxed{0.708}\). The single point \(r=-2\) is what blows MSE up — squaring turns its miss into \(4\) (more than the other two combined) — and it is exactly the point Huber treats as linear, limiting its influence (which is why mean-Huber \(0.708\) is below half the MSE, \(0.875\): the quadratic arm of Huber is \(\tfrac12r^2\), and on the linear arm the outlier’s cost grows only linearly, \(1.5\) against \(\tfrac12 r^2=2\)).

E2. Binary cross-entropy (§2.2), \(\hat p=0.65\). For a positive (\(y=1\)) only the first term lives: \(-[1\cdot\ln 0.65+0]=-\ln 0.65=\boxed{0.431}\). For a negative (\(y=0\)) only the second term lives: \(-[0+1\cdot\ln(1-0.65)]=-\ln 0.35=\boxed{1.050}\). The negative costs more (\(1.050>0.431\), about \(2.4\times\)): predicting \(\hat p=0.65\) is a fair guess when the truth is class 1 (small surprise \(-\ln 0.65\)) but a poor guess when the truth is class 0 (larger surprise \(-\ln 0.35\)). BCE is precisely \(-\log\) of the probability the model assigned to the right answer — \(\hat p\) for a positive, \(1-\hat p\) for a negative — so the worse-matched label is punished more.

E3. BCE rewards correct confidence (§2.2; natural logs). (a) At \(z=1\), \(\hat p=\sigma(1)=0.7311\): \(y{=}1\to-\ln0.7311=0.313\) (confident and right — small loss); \(y{=}0\to-\ln(1-0.7311)=-\ln0.2689=1.313\) (confident and wrong — large loss). (b) At \(z=-1\), \(\hat p=\sigma(-1)=0.2689\): \(y{=}1\to-\ln0.2689=1.313\) (the model leaned the wrong way); \(y{=}0\to-\ln(1-0.2689)=-\ln0.7311=0.313\) (it leaned the right way). (c) At \(z=2.5\), \(\hat p=0.9241\): \(y{=}1\to-\ln0.9241=0.0789\), which is \(0.313-0.0789=0.234\) nats lower than at \(z=1\); \(y{=}0\to-\ln(1-0.9241)=-\ln0.0759=2.579\), which is \(2.579-1.313=1.266\) nats higher. The wrong-way loss changed by far more (\(1.266\) against \(0.234\)): more confidence earns a small gain when right and costs a large amount when wrong.

E4. One gradient step (§3) on the point \((x{=}1,\,y{=}4)\), then with L2. Unregularized: \(\hat y=wx=1\cdot1=1\); loss \((\hat y-y)^2=(1-4)^2=9\); gradient \(\tfrac{\partial\ell}{\partial w}=2(\hat y-y)x=2(-3)(1)=-6\); update \(w\leftarrow 1-0.1(-6)=\boxed{1.6}\). Now add \(\lambda w^2\) with \(\lambda=0.25\) (§5.1): the penalty contributes gradient \(2\lambda w=2(0.25)(1)=0.5\), so the total gradient is \(-6+0.5=-5.5\) and \(w\leftarrow 1-0.1(-5.5)=\boxed{1.55}\). The regularized step lands \(0.05\) short of the unregularized \(1.6\) — the penalty pulled \(w\) back toward \(0\) by exactly \(\eta\cdot2\lambda w=0.05\) on this single step. That nudge-toward-zero, repeated every step, is weight decay.

E5. Weight decay step by step (§5.1). (a) Factor \(=1-2(0.05)(2)=0.8\). (b) Multiplying by \(0.8\) each step: \(w: 2\to1.6\to1.28\to1.024\). (c) \(w_k=2\times0.8^k\). After \(34\) steps \(w=2\times0.8^{34}=0.00101\), still above \(0.001\); after \(35\) steps \(w=2\times0.8^{35}=0.00081\), the first value below it, so \(\boxed{35}\) steps.

E6. BPR loss and gradient (§9), positive \(\hat r_{ui}=1.0\), negative \(\hat r_{uj}=0.2\). Gap \(d=1.0-0.2=0.8\); sigmoid \(\sigma(0.8)=\tfrac{1}{1+e^{-0.8}}=\tfrac{1}{1.4493}=\boxed{0.6900}\); per-triple loss \(-\ln\sigma(0.8)=-\ln 0.6900=\boxed{0.3711}\); gradient \(-(1-\sigma(d))=-(1-0.6900)=\boxed{-0.3100}\). Swapping the scores (a wrong triple, \(\hat r_{ui}=0.2,\hat r_{uj}=1.0\)) gives \(d=-0.8\), \(\sigma(-0.8)=0.3100\), loss \(-\ln 0.3100=\boxed{1.1711}\), gradient \(-(1-0.3100)=\boxed{-0.6900}\). The loss grows by \(\tfrac{1.1711}{0.3711}\approx3.2\times\), but the gradient grows by only \(\tfrac{0.6900}{0.3100}\approx2.2\times\). The two factors differ because the loss scales with \(-\ln\sigma(d)\) while the gradient scales with \(1-\sigma(d)\) — two different functions of \(d\): the gradient factor is exactly \(e^{d}\) (\(e^{0.8}=2.23\), \(e^{2.5}=12.18\)), while the loss factor grows roughly like \(d\,e^{d}\) once the gap is large (\(30.5\) against the exact \(32.7\) at \(d=2.5\)), so it pulls away. (Either way, the more-wrong triple gets both the bigger penalty and the bigger corrective push.)

  1. For scores \(1.5\) and \(-1.0\) (\(d=2.5\)): correct, \(\sigma(2.5)=0.9241\), loss \(0.0789\), gradient \(-0.0759\); wrong (\(d=-2.5\)), loss \(2.5789\), gradient \(-0.9241\). The loss grows \(\tfrac{2.5789}{0.0789}\approx32.7\times\) and the gradient \(\tfrac{0.9241}{0.0759}\approx12.2\times\). Both factors grow, but the loss factor grows faster: \(32.7\) against \(12.2\) here, \(3.2\) against \(2.2\) at \(d=0.8\). A confidently correct triple has both a tiny loss and a tiny gradient, while a confidently wrong one has a loss that keeps growing with the gap but a gradient whose size levels off toward \(1\).

E7. InfoNCE (§2.4), similarities \(0.6\) (positive), \(0.3,-0.1\) (negatives), \(\tau=0.5\). Divide by \(\tau\): \(\tfrac{0.6}{0.5}=1.2\), \(\tfrac{0.3}{0.5}=0.6\), \(\tfrac{-0.1}{0.5}=-0.2\). Exponentiate: \(e^{1.2}=3.320\), \(e^{0.6}=1.822\), \(e^{-0.2}=0.819\). The positive’s softmax share is \(\tfrac{3.320}{3.320+1.822+0.819}=\tfrac{3.320}{5.961}=0.5570\), so \(\ell_{\text{InfoNCE}}=-\ln 0.5570=\boxed{0.585}\). If the positive’s similarity collapses to \(-0.1\), its scaled score is \(-0.2\), its exponential is \(0.819\), and its share falls to \(\tfrac{0.819}{0.819+1.822+0.819}=\tfrac{0.819}{3.460}=0.2367\), so the loss jumps to \(-\ln 0.2367=\boxed{1.441}\). The loss grows by \(\tfrac{1.441}{0.585}\approx\boxed{2.5\times}\): the positive is no longer the most-similar item — it now ties the weakest negative, owns only a small fraction of the softmax mass, and the gradient pulls the anchor back toward its positive.

E8. L1 (lasso) is the one that drives weights to exactly zero; L2 (ridge) only shrinks them smoothly toward zero but never to zero (§5.1). The reason is geometric (§5.1’s figure Why L1 is sparse and L2 is not): the penalized solution is where the growing loss contour (an ellipse around the unregularized optimum) first touches the penalty’s constraint region. L1’s region is a diamond \(|w_1|+|w_2|\le t\), whose corners sit on the axes — and an ellipse expanding outward often meets a corner first, as in the figure, and at a corner one coordinate is exactly \(0\) (e.g. \(w_1=0\)). How often depends on the shape of the contours and on where the unpenalized optimum lies; when the contour instead first touches a face (a flat side) of the diamond, both weights survive.

L2’s region is a smooth circle \(w_1^2+w_2^2\le t\) with no corners, so the contour touches it at a generic off-axis point where both coordinates are nonzero. So the sharp corners of the absolute-value penalty are what manufacture exact zeros (sparsity); the roundness of the squared penalty cannot, giving only shrinkage.

With \(\lambda=0.6\) the one-weight formulas give: L1 subtracts \(\lambda/2=0.3\) from each pull and stops at zero, so \(0.2\mapsto\boxed{0}\), \(-0.5\mapsto\boxed{-0.2}\), \(2\mapsto\boxed{1.7}\); L2 divides by \(1+\lambda=1.6\), so \(0.2\mapsto\boxed{0.125}\), \(-0.5\mapsto\boxed{-0.3125}\), \(2\mapsto\boxed{1.25}\). L1 makes one exact zero (the weak pull below \(0.3\)), L2 none: it shrinks the small weight to \(62.5\%\) of its pull, exactly as it shrinks the large one.

E9. L1 vs L2 (§5.1), with \(\eta=0.1\) and \(\lambda=0.1\). (a) \(\frac{d}{dw}\lambda|w| = \lambda\,\text{sign}(w)\) — a constant pull: each step subtracts \(\eta\lambda=0.1\times0.1=\boxed{0.01}\) from both the weight of \(10\) and the weight of \(0.01\); \(\frac{d}{dw}\lambda w^2 = 2\lambda w\) — proportional to the weight: each step subtracts \(\eta\cdot2\lambda w\), which is \(0.1\times0.2\times10=\boxed{0.2}\) from the large weight and \(0.1\times0.2\times0.01=\boxed{0.0002}\) from the small one. So L1 pulls a weight of \(10\) and a weight of \(0.01\) by exactly the same amount, which is why, at the optimum, small weights sit exactly at \(0\) (§5.1’s one-weight formula), while L2 shrinks everything by a constant fraction and never quite arrives.

(A plain gradient step with that constant pull would overshoot and jitter around \(0\) — from \(w=0.015\) with the pull of \(0.01\) per step: \(0.015\to0.005\to-0.005\to0.005\to\cdots\) — so practical L1 solvers clip a weight to \(0\) when a step would carry it past zero, soft-thresholding: \(0.015\to0.005\to0\), and there it stays while the penalty acts alone (a data gradient can move it again).)

  1. L1 — the “selection” in lasso — zeroes the irrelevant features and keeps a sparse handful. For the pull \(a=0.04\) with \(\lambda=0.1\): L1 has \(\lambda/2=0.05>0.04\), so \(w^\star=\boxed{0}\) exactly; L2 gives \(a/(1+\lambda)=0.04/1.1=\boxed{0.0364}\), small but not zero.

  2. L1 with soft-thresholding subtracts \(0.01\) a step and stops at zero: \(0.03\to0.02\to0.01\to\boxed{0}\), exactly zero after three steps. L2 multiplies by \(1-2\eta\lambda=0.98\) a step: \(0.03\to0.0294\to0.02881\to\boxed{0.0282}\), still \(94\%\) of where it started, and it will never reach \(0\) (this per-step shrink is what §5.1 calls weight decay). The constant pull finishes a small weight off; the proportional pull barely touches it.

E10. From §6’s MAP table: Gaussian noise on \(y\), with the same spread \(s_y\) for every label, gives the MSE loss (its \(-\log\) is \((\hat y-y)^2/2s_y^2\) plus a constant), and a Gaussian prior centered at \(0\), of spread \(s_\theta\), on \(\theta\) gives the L2 regularizer (its \(-\log\) is \(\|\theta\|_2^2/2s_\theta^2\) plus a constant). The MAP objective maximizes the posterior \(p(\theta\mid\text{data})\propto p(\text{data}\mid\theta)\,p(\theta)\) — a product of likelihood and prior. Taking \(-\log\) does two things at once: because \(\log\) of a product is the sum of the logs, the product splits into \(-\log p(\text{data}\mid\theta)-\log p(\theta)\); and because \(-\log\) is monotone decreasing, the \(\theta\) that maximizes the posterior is the same \(\theta\) that minimizes this sum. The first piece is the loss (negative log-likelihood) and the second is the regularizer (negative log-prior) — so “loss \(+\) regularizer” is just the \(-\log\) of “likelihood \(\times\) prior.”

With \(s_y=1\), §6’s formula \(-\ln p=\tfrac12(\hat y-y)^2+\ln\sqrt{2\pi}\) gives \(0.5+0.919=\boxed{1.419}\) for the miss of \(1\) and \(4.5+0.919=\boxed{5.419}\) for the miss of \(3\); the difference is \(\boxed{4}\), which is exactly the difference of the halved squared errors (\(4.5-0.5\)). The squared-error part depends on the prediction; the constant \(0.919\) is the same for every prediction, so it can never change which one is best, which is why it is dropped.

E11. Regularized-MSE for matrix factorization (§8), \(\mathbf{p}=[2,1]\), \(\mathbf{q}=[1,2]\), \(r=5\), \(\lambda=0.1\). Score \(\hat r=\mathbf{p}^{\top}\mathbf{q}=2\cdot1+1\cdot2=\boxed{4}\). Squared-error term \((r-\hat r)^2=(5-4)^2=\boxed{1}\). L2 term: \(\|\mathbf{p}\|^2=2^2+1^2=5\) and \(\|\mathbf{q}\|^2=1^2+2^2=5\), so \(\lambda(\|\mathbf{p}\|^2+\|\mathbf{q}\|^2)=0.1\cdot(5+5)=\boxed{1.0}\). Total objective \(\mathcal{L}=1+1.0=\boxed{2.0}\). Now the implicit-feedback reading: the skipped item has score \(\mathbf p^\top\mathbf q'=2\cdot1.5+1\cdot0.5=3.5\), so the gap is \(d=4-3.5=\boxed{0.5}\) and the BPR loss is \(-\ln\sigma(0.5)=-\ln0.6225=\boxed{0.474}\) — a correct order but a thin margin, so much is left to learn from this triple (§9).

E12. Numerically stable forms (§2.4 “in practice”). (a) \(\sigma(-800)=1/(1+e^{800})\), and \(e^{800}\) is past the largest storable number, so the denominator is \(\infty\) and \(\sigma(-800)\) comes out as \(0\) — then log(0) \(=-\infty\). The exact value is \(-\ln\sigma(-800)=\ln(1+e^{800})\), which cannot be formed directly either; pull out the big exponent: \(\ln(1+e^{a})=a+\ln(1+e^{-a})\) for \(a>0\), here \(800+\ln(1+e^{-800})\). The last term is about \(e^{-800}\), below the range, so it underflows to \(0\); that is harmless, because it lies far below the third decimal (log1p handles it the same way): \(\boxed{800.000}\) (this is the softplus identity, \(-\ln\sigma(x)=\operatorname{softplus}(-x)\); in PyTorch, -F.logsigmoid(x)).

  1. Subtract the largest logit, \(m=802\): \(\ln(e^{800}+e^{802})=m+\ln(e^{800-m}+e^{802-m})=802+\ln(e^{-2}+1)=802+0.1269=\boxed{802.127}\) — log-sum-exp with the max subtracted, so every exponent is at most \(0\) and nothing overflows (in PyTorch, F.cross_entropy / F.log_softmax). Both identities are exact; the routes avoid every overflow, and the one underflow drops only a term far below the third decimal.

E13. Sampled-softmax log-\(Q\) correction (§10), raw score \(\hat r_{uj}=1\) for both negatives. The corrected contribution to the denominator is \(e^{\hat r_{uj}}/Q=e^{\,\hat r_{uj}-\ln Q}\). Popular item, \(Q=0.4\): \(e^{1}/0.4=\tfrac{2.718}{0.4}=\boxed{6.80}\) (equivalently \(e^{\,1-\ln 0.4}=e^{1.916}=6.80\)). Rare item, \(Q=0.05\): \(e^{1}/0.05=\tfrac{2.718}{0.05}=\boxed{54.4}\) (i.e. \(e^{\,1-\ln 0.05}=e^{3.996}=54.4\)). The rare item is up-weighted by \(\tfrac{54.4}{6.80}=\boxed{8\times}\), exactly the inverse ratio of the two sampling probabilities (\(0.4/0.05=8\)).

The positive’s share. The positive (raw score \(3\), left uncorrected) contributes \(e^{3}=20.09\); the third negative contributes \(e^{1.5}/0.5=8.96\); the two negatives above contribute \(6.80\) and \(54.37\). The corrected denominator is \(20.09+6.80+54.37+8.96\approx90.21\) (the unrounded terms add to \(90.210\); the rounded ones to \(90.22\)), so the positive’s share is \(20.09/90.21=\boxed{0.223}\). The rare negative alone takes more than half of the denominator (\(54.37\) of \(90.21\)), because each sampled copy of it stands in for about \(1/Q=20\) items like it (§10).

Neural Networks & Back-propagation

E1. Single neuron (§1–§2). The pre-activation is the dot product plus bias: \(z=\mathbf{w}\cdot\mathbf{x}+b=2\cdot1+(-1)\cdot4+0.5=2-4+0.5=\boxed{-1.5}\). ReLU then clips it: \(a=\max(0,-1.5)=\boxed{0}\). ReLU clipped the negative score \(-1.5\) down to \(0\) — that flat “off for \(z<0\)” behavior is exactly the bend §2 calls the nonlinearity. (Had we used sigmoid instead, \(\sigma(-1.5)=0.1824\): positive, never exactly \(0\).)

E2. Activations at three points (§2). At \(z=1\): \(\sigma(1)=\dfrac{1}{1+e^{-1}}=\boxed{0.7311}\), \(\tanh(1)=\dfrac{e^{1}-e^{-1}}{e^{1}+e^{-1}}=\dfrac{2.7183-0.3679}{2.7183+0.3679}=\dfrac{2.3504}{3.0862}=\boxed{0.7616}\), \(\mathrm{ReLU}(1)=\boxed{1}\). At \(z=-2\): \(\sigma(-2)=\dfrac{1}{1+e^{2}}=\boxed{0.1192}\), \(\tanh(-2)=\dfrac{e^{-2}-e^{2}}{e^{-2}+e^{2}}=\dfrac{0.1353-7.3891}{0.1353+7.3891}=\dfrac{-7.2538}{7.5244}=\boxed{-0.9640}\), \(\mathrm{ReLU}(-2)=\boxed{0}\). At \(z=2\): \(\sigma(2)=\boxed{0.8808}\), \(\tanh(2)=\boxed{0.9640}\), \(\mathrm{ReLU}(2)=\boxed{2}\). (Check with §2’s identity \(\tanh(z)=2\sigma(2z)-1\): \(2\sigma(2)-1=2(0.8808)-1=0.7616\) and \(2\sigma(-4)-1=2(0.0180)-1=-0.9640\).) Tanh is the one with \(f(-2)=-f(2)\): \(-0.9640=-(0.9640)\). For the sigmoid, \(f(-2)+f(2)=0.1192+0.8808=\boxed{1}\), so it is symmetric about \(0.5\) rather than about \(0\) (\(\sigma(-z)=1-\sigma(z)\)); for ReLU, \(f(-2)+f(2)=0+2=\boxed{2}\), since it ignores the negative side altogether.

E3. Layers against one matrix (§2). (a) Layer by layer: \(W^{(1)}\mathbf{x}=[2\cdot1+0\cdot(-2),\ 1\cdot1+1\cdot(-2)]=[2,-1]\), then \(W^{(2)}[2,-1]=[1\cdot2+(-1)(-1),\ 0\cdot2+2(-1)]=[3,-2]\). The single matrix is \(W^{(2)}W^{(1)}=\begin{bmatrix}1\cdot2+(-1)\cdot1&1\cdot0+(-1)\cdot1\\0\cdot2+2\cdot1&0\cdot0+2\cdot1\end{bmatrix}=\begin{bmatrix}1&-1\\2&2\end{bmatrix}\), and \(\begin{bmatrix}1&-1\\2&2\end{bmatrix}[1,-2]=[1+2,\ 2-4]=[3,-2]\): the two agree, as regrouping a matrix product (Linear-Algebra primer §3) guarantees. (b) With the ReLU between the layers, \(\mathbf h=\mathrm{ReLU}([2,-1])=[2,0]\) and \(W^{(2)}\mathbf h=[1\cdot2+(-1)\cdot0,\ 0\cdot2+2\cdot0]=[2,0]\), which is not \([3,-2]\). The ReLU changed the second entry of the first layer’s output, \(-1\to0\); the clipped entry no longer contributes, so the net is no longer the one matrix \(W^{(2)}W^{(1)}\) — the bend is what separates the two cases.

E4. Tiny \(2\!\to\!2\!\to\!1\) forward pass (§3), input \(\mathbf{x}=[2,1]\). Hidden pre-activations (each row of \(W^{(1)}\) dotted with \(\mathbf{x}\), plus the bias): \[ \mathbf z^{(1)}=W^{(1)}\mathbf{x}+\mathbf{b}^{(1)}=\begin{bmatrix}1\cdot2+1\cdot1\\ -1\cdot2+(-1)\cdot1\end{bmatrix}+\begin{bmatrix}-1\\1\end{bmatrix} =\begin{bmatrix}3\\-3\end{bmatrix}+\begin{bmatrix}-1\\1\end{bmatrix}=\begin{bmatrix}2\\-2\end{bmatrix}. \] Apply ReLU elementwise: \(\mathbf{h}^{(1)}=\mathrm{ReLU}([2,-2])=\boxed{[2,\,0]}\) — the second entry, the negative \(-2\), is the one ReLU clipped to \(0\). Output layer: \(\hat y=\mathbf{w}^{(2)}\cdot\mathbf{h}^{(1)}+b^{(2)}=2\cdot2+1\cdot0+1=\boxed{5}\). Each layer is a matrix–vector product plus an elementwise activation, exactly as §3 states; the clipped \(-2\) is the nonlinearity at work (without it the net would be one linear map, E3).

E5. One neuron, forward and backward (§5), with \(x=2\), \(w=1\), \(b=0\), \(y=0\). (a) Forward: \(z=wx+b=1\cdot2+0=\boxed{2}\), \(a=\sigma(2)=\dfrac{1}{1+e^{-2}}=\dfrac{1}{1+0.1353}=\boxed{0.8808}\), \(\mathcal L=\tfrac12(0.8808-0)^2=\boxed{0.3879}\). (b) Backward — the three local derivatives are \[ \frac{\partial\mathcal L}{\partial a}=a-y=0.8808,\qquad \frac{\partial a}{\partial z}=a(1-a)=0.8808\times0.1192=0.1050,\qquad \frac{\partial z}{\partial w}=x=2, \] so \[ \begin{gathered} \frac{\partial\mathcal L}{\partial z}=0.8808\times0.1050=0.0925,\\[4pt] \frac{\partial\mathcal L}{\partial w}=0.0925\times2=\boxed{0.1850},\qquad \frac{\partial\mathcal L}{\partial b}=0.0925\times1=\boxed{0.0925}. \end{gathered} \] \(\partial\mathcal L/\partial w\) is the larger, by the factor \(2\): \(\partial z/\partial w=x=2\) while \(\partial z/\partial b=1\), so \(0.1850/0.0925=2\). (Notice also \(\sigma'(2)=0.1050\), less than half of §5’s \(\sigma'(0.5)=0.2350\): the sigmoid is flatter at \(z=2\) — it is starting to saturate — so less gradient gets through.) (c) One gradient-descent step with \(\eta=0.5\): \(w\leftarrow w-\eta\dfrac{\partial \mathcal L}{\partial w}=1-0.5(0.1850)=1-0.0925=\boxed{0.9075}\). The weight moved toward \(0\) (down from \(1\)): lowering \(w\) lowers the score \(z\), pushes \(a\) toward the target \(y=0\), and shrinks the loss.

E6. Softmax + cross-entropy (§4), logits \(\mathbf z=[2,0,0]\), true class \(0\). (a) The exponentials are \(e^{2},e^{0},e^{0}=7.389,1,1\) (sum \(9.389\)), so the true-class probability is \(\hat p_0=\dfrac{7.389}{9.389}=\boxed{0.787}\) (the other two get \(\dfrac{1}{9.389}=0.107\) each), and the cross-entropy loss is \(\mathcal L=-\ln\hat p_0=-\ln 0.787=\boxed{0.240}\). It is smaller than §4’s \(0.41\) because the true class now leads both rivals by \(2\) (in §4 one rival was only \(1\) behind), so the model puts more of its mass on the right answer: \(0.787\) instead of \(0.665\). (b) If the model were maximally unsure, \(\hat p_0=\tfrac13\), the loss would be \(-\ln\tfrac13=\boxed{1.0986}\approx1.10\). The uncertain case has the larger loss (\(1.10>0.240\)), which is the right direction: cross-entropy rewards putting probability mass on the correct class (\(\hat p_0\to1\Rightarrow\mathcal L\to0\)) and penalizes spreading it thin — exactly the pressure that trains a classifier (§4).

E7. One Adam step from rest (§6), with \(g_1=-0.3\), \(m_0=v_0=0\), \(\beta_1=0.9\), \(\beta_2=0.999\), \(\eta=0.1\). \[ \begin{gathered} m_1=(1-0.9)\,g_1=0.1\times(-0.3)=\boxed{-0.03},\\[4pt] v_1=(1-0.999)\,g_1^2=0.001\times0.09=\boxed{9\times10^{-5}}. \end{gathered} \] Bias-correct (divide by \(1-\beta^{\,1}\), i.e. by \(0.1\) and \(0.001\)): \(\hat m_1=\dfrac{-0.03}{0.1}=\boxed{-0.3}=g_1\) and \(\hat v_1=\dfrac{9\times10^{-5}}{0.001}=\boxed{0.09}=g_1^2\). The step is \[ \eta\,\frac{\hat m_1}{\sqrt{\hat v_1}}=0.1\times\frac{-0.3}{\sqrt{0.09}}=0.1\times\frac{-0.3}{0.3}=\boxed{-0.1}, \] so the update \(w\leftarrow w-(-0.1)=w+0.1\) moves the weight up by \(0.1\): the gradient is negative, so downhill is the \(+\) direction. For \(g_1=-3\) the same steps give \(\hat m_1=-3\), \(\hat v_1=9\) and the step \(0.1\times(-3)/3=\boxed{-0.1}\) again. Plain SGD’s steps are \(\eta g_1=0.1\times(-0.3)=-0.03\) and \(0.1\times(-3)=-0.3\), ten times apart, while Adam’s two steps are equal: at the first step the ratio is \(g_1/|g_1|=\operatorname{sign}(g_1)=-1\), so the gradient’s magnitude cancels and the size of the move is set by \(\eta\).

E8. All weights equal (§3, §5, §6). (a) \(\mathbf z^{(1)}=W^{(1)}\mathbf x=[0.5\cdot1+0.5\cdot2,\ 0.5\cdot1+0.5\cdot2]=[1.5,1.5]\), so \(\mathbf h^{(1)}=\mathrm{ReLU}(\mathbf z^{(1)})=[1.5,1.5]\) and \(\hat y=0.5\cdot1.5+0.5\cdot1.5=\boxed{1.5}\), with \(\mathcal L=\tfrac12(0.5)^2=0.125\). (b) \(\partial\mathcal L/\partial\hat y=\hat y-y=0.5\); rule 3 gives \(\partial\mathcal L/\partial\mathbf h^{(1)}=0.5\,[0.5,0.5]=[0.25,0.25]\); both ReLU slopes are \(1\) (both scores are positive), so \(\partial\mathcal L/\partial\mathbf z^{(1)}=[0.25,0.25]\), and rule 2 gives \(\partial\mathcal L/\partial W^{(1)}=\begin{bmatrix}0.25\cdot1&0.25\cdot2\\0.25\cdot1&0.25\cdot2\end{bmatrix}=\begin{bmatrix}0.25&0.5\\0.25&0.5\end{bmatrix}\): the two rows are identical. (c) \(W^{(1)}\leftarrow0.5-0.1\,[0.25,\,0.5]=\begin{bmatrix}0.475&0.45\\0.475&0.45\end{bmatrix}\): the rows are still identical. The two hidden units started as copies and received the same gradient, so they stay copies however long training runs; that is the silent failure of a constant start, which random initial weights avoid.

E9. Dropout and early stopping (§7). (a) The survivors are scaled by \(1/(1-p)=1/0.75=\tfrac43\), so \([4,8,12]\) with the second unit zeroed becomes \(\boxed{[\tfrac{16}{3},\,0,\,16]}\approx[5.33,0,16]\). The first output survives with probability \(0.75\) (as \(4/0.75=5.33\)) and is zeroed with probability \(0.25\), so its average over many steps is \(0.75\times5.33+0.25\times0=\boxed{4}\): the scaling keeps the layer’s output unchanged on average. (b) The held-out loss falls to \(0.27\) at epoch \(60\) and then rises to \(0.29\) at epoch \(80\), so early stopping keeps the model from epoch \(\boxed{60}\).

E10. Neuron as the recommender’s dot-product score (§1), \(\mathbf{p}=[2,1,3]\), \(\mathbf{q}=[1,2,1]\). (a) The affinity \(\mathbf{p}\cdot\mathbf{q}\) is a single linear neuron \(\mathbf{w}\cdot\mathbf{x}+b\) with the item vector as the weights (\(\mathbf{w}=\mathbf{q}\)), the user vector as the input (\(\mathbf{x}=\mathbf{p}\)), and no bias (\(b=0\)). Its value: \(\hat r=\mathbf{p}\cdot\mathbf{q}+0=2\cdot1+1\cdot2+3\cdot1=2+2+3=\boxed{7}\). (b) The second item \(\mathbf{q}_2=[3,0,1]\) scores \(\hat r_2=\mathbf{p}\cdot\mathbf{q}_2=2\cdot3+1\cdot0+3\cdot1=6+0+3=\boxed{9}\), and \(9>7\), so it indeed ranks above the first — verified. This bare dot-product head is exactly matrix factorization / LightGCN’s scoring rule, and the embeddings \(\mathbf{p},\mathbf{q}\) are the learned lookup rows of §3’s embedding layer — trained by the same back-prop (§5) as any other weight.

E11. Embedding lookup and the sparse gradient (§3, §7). (a) Id \(2\) returns the last row, \(E_2=[2,1,0]\) — a row read, no arithmetic. (b) Only rows \(\boxed{0\text{ and }2}\) can receive a nonzero gradient: the other row, row \(1\), enters the output multiplied by \(0\), so \(\partial\mathcal L/\partial E_1=\mathbf 0\) under plain SGD with no penalty. (c) Weight decay multiplies every row, present in the batch or not, by \(1-\eta\cdot2\lambda=1-0.1\cdot2\cdot0.05=\boxed{0.99}\), so the absent row becomes \(0.99\,[0,3,1]=[0,\,2.97,\,0.99]\) (momentum and Adam also move absent rows, §6).

E12. The same neuron, flipped target (§5), now with \(x=2\), \(w=0.25\). (a) With \(y=1\): \(\partial \mathcal L/\partial a=a-y=0.6225-1=-0.3775\), so \(\partial \mathcal L/\partial w=-0.3775\times0.2350\times2=-0.1774\). (b) Step: \(w\leftarrow0.25-0.1(-0.1774)=0.2677\) — \(w\) moves up. That makes sense: the target rose from \(0\) to \(1\), so we now want a larger output \(a\), hence a larger score \(z=wx\), hence a larger \(w\). (The error flipped sign, so the gradient — and the step — flipped with it: with \(y=0\), §5’s weight had moved down.)

E13. A content vector for an unseen id (§3). (a) \(\mathbf c\,G=[1,1]\begin{bmatrix}0.3&0.2&0.0\\0.1&0.0&0.6\end{bmatrix}=[0.3+0.1,\ 0.2+0.0,\ 0.0+0.6]=\boxed{[0.4,\,0.2,\,0.6]}\). (b) \(\mathbf p\cdot(\mathbf c\,G)=1\cdot0.4+2\cdot0.2+0\cdot0.6=\boxed{0.8}\). The film had no row in the table, yet it gets a usable vector and a score, because the vector is computed from its content instead of looked up by id.

E14. (a) \(z_1=w_1x=(-0.3)(2)=\boxed{-0.6}\); \(h=\sigma(-0.6)\approx\boxed{0.3543}\); \(\hat y=w_2h=1.5(0.3543)=\boxed{0.5315}\); \(\mathcal L=\tfrac12(0.5315-1)^2=\tfrac12(-0.4685)^2\approx\boxed{0.1097}\). (b) \(\partial\mathcal L/\partial\hat y=\hat y-y=-0.4685\); \(\partial\mathcal L/\partial w_2=-0.4685\cdot h=-0.4685\cdot0.3543\approx\boxed{-0.1660}\); \(\partial\mathcal L/\partial h=-0.4685\cdot w_2=-0.7027\); \(\partial h/\partial z_1=h(1-h)=0.3543\cdot0.6457\approx0.2288\); so \(\partial\mathcal L/\partial z_1=-0.7027\cdot0.2288\approx-0.1608\) and, since \(\partial z_1/\partial w_1=x=2\), \(\partial\mathcal L/\partial w_1=-0.1608\cdot2=\boxed{-0.3216}\). (c) \(w_1\leftarrow-0.3-0.1(-0.3216)=\boxed{-0.2678}\) and \(w_2\leftarrow1.5-0.1(-0.1660)=\boxed{1.5166}\); both gradients are negative, so both weights rise, and the loss falls to about \(0.0968\).

E15. \(u_1=g_1=0.2\) and \(w=1-0.1(0.2)=\boxed{0.98}\); \(u_2=0.9(0.2)+0.2=\boxed{0.38}\) and \(w=0.98-0.1(0.38)=\boxed{0.942}\); \(u_3=0.9(0.38)-0.2=\boxed{0.142}\) and \(w=0.942-0.0142=\boxed{0.9278}\). The gradient flipped, but the velocity is still positive (\(0.142\)), so the weight keeps moving down — momentum carries it through the sign change before it turns.

E16. The steps are \(0.1+0.2+0.3+0.4+0.2+0.1=1.3\), so \(w=1-1.3=\boxed{-0.3}\) (with \(g=1\) each step subtracts its learning rate). A constant \(\eta=0.4\) for six steps subtracts \(6(0.4)=2.4\) and gives \(w=\boxed{-1.4}\): the schedule starts gently, peaks, and then decays, moving the weight less overall.

E17. The columns are \([1,5,9]\) and \([3,7,11]\), with means \(5\) and \(7\) and standard deviations \(\sqrt{32/3}\approx3.266\) each, so (a) BatchNorm gives \(\boxed{\begin{bmatrix}-1.22&-1.22\\0&0\\1.22&1.22\end{bmatrix}}\). (b) Each row \([1,3]\), \([5,7]\), \([9,11]\) has mean \(2,6,10\) and standard deviation \(1\), so LayerNorm gives \(\boxed{\begin{bmatrix}-1&1\\-1&1\\-1&1\end{bmatrix}}\). (c) For the single row \([5,7]\), LayerNorm uses only that row’s own mean \(6\) and standard deviation \(1\) and returns \(\boxed{[-1,1]}\). BatchNorm’s column means are now the row’s own values, \(5\) and \(7\), so each entry minus its mean is \(0\) and with a tiny \(\varepsilon\) the output is \(\boxed{[0,0]}\) whatever the input was (\(0/\sqrt{0+\varepsilon}=0\); with \(\varepsilon\) ignored it would be \(0/0\)): a batch of one gives BatchNorm nothing to standardize (at test time it uses statistics saved from training instead).

E18. (a) He: variance \(2/50=\boxed{0.04}\), standard deviation \(\sqrt{0.04}=\boxed{0.2}\). Xavier: variance \(2/(50+50)=\boxed{0.02}\), standard deviation \(\sqrt{0.02}\approx\boxed{0.141}\) (half of He’s variance, as §6 says for \(\text{fan\_in}=\text{fan\_out}\)). (b) With variance \(3/\text{fan\_in}\) the per-layer factor is \(\text{fan\_in}\times\tfrac{3}{\text{fan\_in}}/2=1.5\), so after \(10\) layers it is \(1.5^{10}\approx\boxed{57.7}\): the signal grows more than fifty-fold, a milder case of §6’s blow-up.

E19. \(t(0.125)=2(0.125)-4\cdot0=\boxed{0.25}\); \(t(0.25)=2(0.25)-4(0)=\boxed{0.5}\), so \(t(t(0.125))=0.5\). Five stacked tents make \(2^5=\boxed{32}\) pieces from \(10\) units; a single hidden ReLU layer of \(m\) units makes at most \(m+1\) pieces, so it needs at least \(\boxed{31}\) units for \(32\) pieces.

Representation Learning & the Transformer

E1. One-hot bags (§1.1). The bag is \(\mathbf p=\mathbf o_1+\mathbf o_3=[1,0,1,0,0]\). (a) \(\mathbf p\cdot\mathbf o_3=\boxed{1}\), since the \(1\) in slot \(3\) meets the \(1\) of \(\mathbf o_3\), and \(\mathbf p\cdot\mathbf o_2=\boxed{0}\), since slot \(2\) of \(\mathbf p\) is \(0\). (b) \(\lVert\mathbf p\rVert=\sqrt2\), so \(\cos(\mathbf p,\mathbf o_3)=\dfrac{1}{\sqrt2\cdot1}=\boxed{0.707}\) and \(\cos(\mathbf p,\mathbf o_2)=\boxed{0}\). (c) Neither number shows it: movie \(2\) gets exactly \(0\), the same as a movie unrelated to everything the user watched. The one-hots of movies \(1\) and \(2\) are orthogonal, so a score built from them can only say “this exact movie was watched”, never “a movie like one that was watched”, which is the flaw §1.2’s dense embedding fixes.

E2. Cosine of two small word vectors (§1.1 / Linear Algebra for Machine Learning §4). Dot product \(\mathbf{v}_{\text{coffee}}\cdot\mathbf{v}_{\text{tea}}=4\cdot3+3\cdot4=12+12=24\); lengths \(\lVert\mathbf{v}_{\text{coffee}}\rVert=\sqrt{16+9}=5\) and \(\lVert\mathbf{v}_{\text{tea}}\rVert=\sqrt{9+16}=5\). So \(\cos=\dfrac{24}{5\cdot5}=\dfrac{24}{25}=\boxed{0.96}\) (an angle of about \(16.3^\circ\)) — very near \(+1\), i.e. the two arrows point almost the same way. The distributional hypothesis (§2.1) reads this geometrically: coffee and tea keep similar company (drink, cup, morning), so a predict-based model pushes their vectors close together, and a near-\(1\) cosine is exactly the “these two words mean similar things” signal.

E3. What negative sampling saves (§2.2). (a) The full softmax sums over every word of the vocabulary, so its cost depends on \(V\) and not on \(k\): the denominator of \(P(c\mid w)\) is the expensive sum over \(V\), and negative sampling replaces it by contrasting the positive against a few negatives (the same move as BPR and InfoNCE). (b) The full softmax needs \(V=\boxed{50{,}000}\) dot products, one per word; negative sampling scores the one true context and the \(k=10\) negatives, \(k+1=\boxed{11}\) dot products, which is \(50000/11\approx\boxed{4{,}545}\) times fewer. (c) The cost of negative sampling stays at \(11\) while the full softmax grows to \(500{,}000\), so the saving grows with \(V\) to \(500000/11\approx\boxed{45{,}455}\) times: the trick pays most exactly where the vocabulary is large.

E4. Skip-gram probabilities (§2.2). (a) Dot products with \(\mathbf v_w=[1,1]\): \(\mathbf u_A\cdot\mathbf v_w=2+1=3\), \(\mathbf u_B\cdot\mathbf v_w=0+1=1\), \(\mathbf u_C\cdot\mathbf v_w=-1+1=0\). (b) Softmax of \([3,1,0]\): \(e^3,e^1,e^0=20.086,\,2.718,\,1.000\), which sum to \(23.804\), so \(P(A)=20.086/23.804=\boxed{0.844}\), \(P(B)=0.114\), \(P(C)=0.042\). (c) Most sure of A (\(0.844\)) — and \(\mathbf u_A=[2,1]\) is the best aligned with \(\mathbf v_w=[1,1]\): largest dot product, and also the largest cosine (\(0.949\), against \(0.707\) and \(0\)), so direction alone already picks A. “Predict your neighbors” rewards alignment.

E5. LSTM cell, one step (§3.3). With \(c_{t-1}=0.50\) and gates \(f_t=0.40\), \(i_t=0.90\), \(\tilde c_t=0.80\), \(o_t=0.50\): (a) the new cell state is \(c_t=f_t c_{t-1}+i_t\tilde c_t=0.40(0.50)+0.90(0.80)=0.20+0.72=\boxed{0.92}\). (b) the readout is \(h_t=o_t\tanh(c_t)=0.50\tanh(0.92)=0.50(0.726)=\boxed{0.363}\).

  1. This step is the LSTM overwriting: a low forget gate (\(0.40\)) with a high input gate (\(0.90\)) discards most of the old memory (\(0.50\)) and lets the new candidate dominate (\(c_t=0.92\)). §3.3’s worked step was the opposite — \(f_t=0.95\), \(i_t=0.10\) held the memory essentially fixed (\(0.80\to0.80\)), the LSTM remembering. Two knobs, set independently: the forget gate decides how much old memory survives, the input gate how much of the candidate is written. Keep \(f_t=0.40\) but lower \(i_t\) to \(0.10\) and \(c_t=0.20+0.08=0.28\): the memory is forgotten, not overwritten.

E6. Scaled dot-product (§4.1). (a) With \(d_k=16\), \(\sqrt{d_k}=4\), so \([6,2,0]\to\boxed{[1.5,\,0.5,\,0]}\). (b) Softmax of the raw scores: \(e^6,e^2,e^0=403.4,\,7.39,\,1.00\), summing to \(411.8\), so \(\boxed{[0.980,\,0.018,\,0.002]}\). Softmax of the scaled scores: the exponentials are \(4.48,\,1.65,\,1.00\) and sum to \(7.13\), so the weights are \(\boxed{[0.629,\,0.231,\,0.140]}\). (c) With the top raw score at \(7\), the raw weights are \([0.992,\,0.007,\,0.001]\): the top weight moves from \(0.9796\) to \(0.9924\), only \(\boxed{0.013}\). Scaled by \(4\), the scores \([1.75,\,0.5,\,0]\) give \([0.685,\,0.196,\,0.119]\): the top weight moves from \(0.629\) to \(0.685\), \(\boxed{0.056}\), about \(4.4\) times more. The raw version has almost nowhere left to move and gives the second and third keys almost no weight, so the gradient through those weights is tiny; the scaled version keeps all three in play and leaves the token more to learn from.

E7. Self-attention, first row of a new history (§4.2’s method). The value rows are \(V=X\) and \(\sqrt{d_k}=\sqrt2\approx1.414\). (a) The query is \(\mathbf{x}_1=[2,0]\); the scaled scores \(\mathbf{x}_1\cdot\mathbf{x}_j/\sqrt2\) are \[ \frac{4}{\sqrt2}=2.828,\qquad \frac{2}{\sqrt2}=1.414,\qquad \frac{0}{\sqrt2}=0.000 . \] (b) Softmax: \(\exp([2.828,1.414,0])=[16.92,\,4.11,\,1.00]\), summing to \(22.03\), so the weights are \(\boxed{[0.768,\,0.187,\,0.045]}\). (c) The output is \[ 0.768[2,0]+0.187[1,1]+0.045[0,1]=[\,1.536+0.187,\ 0.187+0.045\,]=\boxed{[1.723,\,0.232]}. \] (d) The first film gets the most weight (\(0.768\)), because its own score is the largest (\([2,0]\cdot[2,0]=4\)), and the third the least (\(0.045\)), because it is at right angles to the query. The output stays close to the first film’s own \([2,0]\), pulled a little toward the second.

E8. \(Q,K,V\) as learned views (§4.2). (a) Entry \(a\) of a row-times-matrix product is the row dotted with column \(a\). The columns of \(W_Q\) are \([1,0]\) and \([0,0]\), so \(\mathbf{q}=\mathbf{x}W_Q=[\,2\cdot1+1\cdot0,\ 2\cdot0+1\cdot0\,]=[2,0]\); the columns of \(W_K\) are \([0,1]\) and \([0,0]\), so \(\mathbf{k}=\mathbf{x}W_K=[\,2\cdot0+1\cdot1,\ 0\,]=[1,0]\); and \(\mathbf{v}=\mathbf{x}W_V=[2,1]\). (b) Yes — \([2,0]\), \([1,0]\), \([2,1]\) are three different vectors built from the one token \(\mathbf{x}=[2,1]\): the query keeps only its romance, the key only its musical/drama side, the value all of it.

  1. \(\mathbf{q}\cdot\mathbf{k}=2\cdot1+0\cdot0=\boxed{2}\). For \(\mathbf{x}'=[0,2]\) the key is \(\mathbf{k}'=\mathbf{x}'W_K=[2,0]\) (its entry is \(\mathbf x'\) dotted with the column \([0,1]\) of \(W_K\)) and \(\mathbf{q}\cdot\mathbf{k}'=2\cdot2+0\cdot0=\boxed{4}\). So \(\mathbf q\) matches the second token’s key more strongly: the query asks for a high musical/drama side (\(m=2\) against \(1\)). The raw similarity ranks them the other way: \(\mathbf{x}\cdot\mathbf{x}=2\cdot2+1\cdot1=5\) against \(\mathbf{x}\cdot\mathbf{x}'=0\cdot2+1\cdot2=2\), because it only measures how alike two vectors are. The learned pair of matrices pairs one feature of the asker (romance, through \(W_Q\)) with a different feature of the candidate (musical/drama, through \(W_K\)).

E9. Multi-head attention (§4.3). (a) Concatenated, the outputs are \([0.5,\,1.0,\,1.0,\,0.5]\); the \(W_O\) that adds the heads coordinate by coordinate gives \([0.5+1.0,\ 1.0+0.5]=\boxed{[1.5,\,1.5]}\), a vector of the original length \(d=2\). The heads disagreed (one leans to the second coordinate, one to the first) and the mix keeps both.

  1. With the third head the concatenation is \([0.5,\,1.0,\,1.0,\,0.5,\,0.0,\,0.5]\), and the \(W_O\) with six rows that adds the heads coordinate by coordinate gives \([0.5+1.0+0.0,\ 1.0+0.5+0.5]=\boxed{[1.5,\,2.0]}\). It has length \(2=d\) again, however many heads run. Each head contributed its own relation, and the mix keeps all three. (c) The block adds a sub-layer’s input back to its output (add & norm, §4.3), and a sum needs two vectors of the same length, so \(W_O\) must map back to \(d\); the next block also expects length-\(d\) vectors, which is what lets blocks be stacked. A length-\(6\) output would break both.

E10. Positional encoding (§4.3) with \(d=4\), \(\text{pos}=2\). Using \(10000^{0}=1\) and \(10000^{1/2}=100\), with every angle in radians (a calculator in degree mode would give \(\sin 2^\circ=0.035\) instead): \[ \begin{aligned} PE(2,0)&=\sin 2=0.909\ \text{(given)}, & PE(2,1)&=\cos 2=-0.416\ \text{(given)},\\ PE(2,2)&=\sin(2/100)=\sin 0.02=\boxed{0.020}, & PE(2,3)&=\cos(2/100)=\cos 0.02=\boxed{1.000}, \end{aligned} \] so \(PE(\text{pos}=2,\,d=4)\approx[\,0.909,\,-0.416,\,0.020,\,1.000\,]\) — a different fingerprint from \(\text{pos}=1\)’s \([0.841,0.540,0.010,1.000]\), with the slow pair, \(PE(\cdot,2)\) and \(PE(\cdot,3)\), barely moving (\([0.010,1.000]\to[0.020,1.000]\)) while the fast pair, \(PE(\cdot,0)\) and \(PE(\cdot,1)\), changes a lot (\([0.841,0.540]\to[0.909,-0.416]\)). The slow pair barely moves because its angle, \(\text{pos}/100\), grows by only \(0.01\) radian per position, where the fast pair’s grows by a whole radian.

At \(d=6\) the third pair has \(i=2\), so \(2i/d=4/6\) and \(10000^{2/3}=464.16\): at \(\text{pos}=1\) the angle is \(1/464.16=0.00215\) radian, so \(PE(1,4)=\sin 0.00215=\boxed{0.002}\) and \(PE(1,5)=\cos 0.00215=\boxed{1.000}\). The later the pair, the slower its wave and the closer its coordinates stay to \((0,1)\) at small positions.

E11. SASRec / item2vec scoring (§4.4 / §2.4). The intent vector is \(\mathbf{h}=[1,3]\) (leaning toward axis 2), and the score of item \(j\) is the dot product \(\mathbf{h}\cdot\mathbf{e}_j\). (a) Raw dot products: \[ \mathbf{h}\cdot[1,0]=1,\qquad \mathbf{h}\cdot[1,1]=1+3=4,\qquad \mathbf{h}\cdot[0,1]=3, \] so the dot-product ranking is Notebook \((4)\) \(\succ\) La La Land \((3)\) \(\succ\) Titanic \((1)\). (b) Cosine needs the norms: \(\lVert\mathbf{h}\rVert=\sqrt{1+9}=\sqrt{10}\approx3.162\), and the items have norms \(\lVert[1,0]\rVert=1\), \(\lVert[1,1]\rVert=\sqrt2\approx1.414\), \(\lVert[0,1]\rVert=1\). Then \[ \begin{gathered} \cos(\mathbf{h},[1,0])=\frac{1}{\sqrt{10}}\approx\boxed{0.316},\\[4pt] \cos(\mathbf{h},[1,1])=\frac{4}{\sqrt{10}\sqrt2}\approx\boxed{0.894},\\[4pt] \cos(\mathbf{h},[0,1])=\frac{3}{\sqrt{10}}\approx\boxed{0.949}. \end{gathered} \] The cosine ranking is La La Land \((0.949)\) \(\succ\) Notebook \((0.894)\) \(\succ\) Titanic \((0.316)\) — the rankings disagree at the top. The raw dot product puts Notebook first (\(4\) vs \(3\)) partly because Notebook is the larger-norm vector (\(\sqrt2>1\)), not because its direction matches the intent better: under cosine, which divides the length out, La La Land leads. That length sensitivity is exactly the popularity/length bias §4.4’s serving aside (L2-normalize first, turning the dot product into cosine; Linear Algebra for Machine Learning §4) is designed to remove.

E12. BPE on an unseen word (§5.3). (a) Replay the two learned rules in order on s l o w _: \[ \texttt{s l o w \_}\ \xrightarrow{\ \texttt{o+w}\ }\ \texttt{s l ow \_}\ \xrightarrow{\ \texttt{l+ow}\ }\ \texttt{s low \_} \quad\Rightarrow\quad \text{tokens } \boxed{[\,\texttt{s},\ \texttt{low},\ \texttt{\_}\,]}. \] The stem low is the same piece slow shares with low and lowest, so the model reuses what it already learned about that stem — and there is no <UNK>, exactly §5.3’s payoff. (b) In the corpus low ×5, lowest ×2, the pair e,s appears only inside lowest, so its count is \(\boxed{2}\). BPE merges greedily by frequency, and the pair o,w occurs in both word-types (\(5+2=7\)), so o+w (count \(7\)) is merged before e,s (count \(2\)).

E13. KV-cache cost (§5.4). (a) Without the cache each step re-encodes the whole prefix, so the total for \(T=5\) is \(1+2+3+4+5=\boxed{15}\) token-passes (\(=\tfrac{T(T+1)}{2}=\tfrac{5\cdot6}{2}=15\), the \(O(T^2)\) row). (b) With the cache each step pushes only the one new token, so the total is \(1+1+1+1+1=\boxed{5}\) passes (\(=T\), the \(O(T)\) row). (c) The ratio is \(15/5=\boxed{3\times}\) at \(T=5\), larger than the \(10/4=2.5\times\) the chapter shows at \(T=4\). (d) In general the ratio is \(\dfrac{T(T+1)/2}{T}=\boxed{\dfrac{T+1}{2}}\). It reaches \(10\) when \(T+1=20\), that is at \(T=\boxed{19}\) (\(190\) passes against \(19\)). At \(T=1{,}000\) it is \(500.5\times\), the chapter’s “about \(500\times\)”.

E14. What the KV-cache stores (§5.4). (a) The cache holds a key and a value (\(2\) vectors), each \(d=5{,}120\) numbers of \(2\) bytes, in each of \(40\) layers: \(2\times40\times5{,}120\times2=\boxed{819{,}200}\) bytes, \(800\) KiB, per token. (b) For \(2{,}048\) tokens that is \(819{,}200\times2{,}048=1{,}677{,}721{,}600\) bytes, \(1.5625\) GiB (\(\boxed{\approx1.56}\) GiB) per sequence. (c) The weights take \(13\times10^9\times2=26\times10^9\) bytes, \(\approx24.2\) GiB, and \(24.2/1.5625\approx15.5\): about \(\boxed{15}\) sequences fit before the cache outweighs the weights (the sixteenth tips it). At large batches and long contexts, cache memory rather than the weights then limits how many requests a server can hold (a claim from practice this book does not test).

  1. In layer \(1\) a key is \(\mathbf{x}_iW_K\) of the token’s own embedding, which no mask can change: all \(\boxed{6}\) cached keys stay valid. But the layer-1 output of token \(6\) now looks at token \(7\), so it changes, while the outputs of tokens \(1\) to \(5\) do not (token \(5\) sees token \(6\)’s embedding, which is the same). Layer-\(2\) keys are computed from the layer-1 outputs, so token \(6\)’s is stale and tokens \(1\) to \(5\) are valid: \(\boxed{5}\). Each further layer loses one more (token \(5\)’s layer-2 output has seen token \(6\)’s changed layer-1 output, so \(4\) remain in layer \(3\)). With the causal mask nothing is ever stale, and in a fully bidirectional encoder (§5.1) every layer-2 key goes stale, so almost nothing can be reused.

E15. Mean pooling (§5.2). (a) The sum is \([2+0+1+3,\ 0+4+1+3]=[6,8]\), so the mean is \(\boxed{[1.5,\,2.0]}\). (b) \(\lVert[1.5,2]\rVert=2.5\), so \(\cos\) with \([1,0]\) is \(1.5/2.5=\boxed{0.6}\) and with \([0,1]\) is \(2/2.5=\boxed{0.8}\). The review leans toward La La Land, the film on the axis where the pooled vector is larger.

E16. Attention masks (§5.1). (a) Item \(3\) may see items \(1\) to \(3\), so the cell for item \(4\) is blocked (score \(-\infty\), weight \(0\)). Softmax of \([0,1,1]\): \(e^0,e^1,e^1=1,\,2.72,\,2.72\), summing to \(6.44\), so the weights are \(\boxed{[0.155,\,0.422,\,0.422,\,0]}\). (b) Without the mask, \(e^9=8{,}103\) joins the sum (\(8{,}109.5\)), so the weights are \(\boxed{[0.0001,\,0.0003,\,0.0003,\,0.9992]}\). (c) The weights of (b) put \(99.9\%\) (\(0.9992\)) on item \(4\), the answer it is asked to predict, so the model gains by copying it and learns nothing about the history; at serving time item \(4\) does not exist yet, so there is nothing to copy. (d) \(0.15\times200=\boxed{30}\) tokens are picked; of those \(0.8\times30=\boxed{24}\) get [MASK], \(\boxed{3}\) a random token and \(\boxed{3}\) no change (the recipe is Devlin et al.’s, §5.1).

E17. Pre-LN and Post-LN (§4.3). (a) The mean of \([1,2,6]\) is \(3\), the deviations are \(-2,-1,3\), the variance is \((4+1+9)/3=4.667\) and the standard deviation \(2.160\), so \(\mathrm{LN}(\mathbf x)=\boxed{[-0.926,\,-0.463,\,1.389]}\). (b) \(\mathbf x+f(\mathbf x)=3\mathbf x=[3,6,18]\), and normalizing it gives the same \(\boxed{[-0.926,\,-0.463,\,1.389]}\): the factor \(3\) cancels. (c) \(f(\mathrm{LN}(\mathbf x))=[-1.852,\,-0.926,\,2.777]\), so the Pre-LN output is \(\boxed{[-0.852,\,1.074,\,8.777]}\). (d) For \(10\mathbf x=[10,20,60]\), \(\mathrm{LN}\) gives the same \([-0.926,\,-0.463,\,1.389]\) (the standard deviation is now \(21.60\) and the deviations \(10\) times larger), so the Post-LN output, \(\mathrm{LN}\) of \(30\,[1,2,6]\), is unchanged. The Pre-LN output is \(10\mathbf x+2\,\mathrm{LN}(10\mathbf x)=[10,20,60]+[-1.852,-0.926,2.777]=\boxed{[8.148,\,19.074,\,62.777]}\), so the Pre-LN output changed and its last entry is \(62.777\): \(\mathbf x\) itself is added back, whereas Post-LN normalizes every trace of its size away.

E18. Sampling (§5.4). (a) Argmax picks \(\boxed{a}\) every time. (b) Two independent runs both pick \(a\) with probability \(0.5\times0.5=\boxed{0.25}\). (c) The runs agree when both pick \(a\), \(b\), \(c\) or \(d\): \(0.25+0.09+0.0225+0.0025=0.365\), so they differ with probability \(1-0.365=\boxed{0.635}\). (d) About \(1{,}000\times0.15=\boxed{150}\) times. Argmax gives one continuation; sampling gives different ones from run to run.

Traditional Recommender Systems

E1. Content cosine (§2). The numerator is the dot product — it counts shared genres: \(\text{Vendetta}\cdot\text{Matrix}=(1)(1)+(1)(1)+(1)(0)=2\) (they share action and sci-fi; only romance differs). The lengths are \(\lVert\text{Vendetta}\rVert=\sqrt{1+1+1}=\sqrt3\) and \(\lVert\text{Matrix}\rVert=\sqrt{1+1+0}=\sqrt2\), so \[ \cos(\text{Vendetta},\text{Matrix})=\frac{2}{\sqrt3\,\sqrt2}=\frac{2}{\sqrt6}\approx\boxed{0.82}. \] At \(0.82\) it is far closer to §2’s near-aligned \(0.58\) (Titanic–Vendetta) than to its orthogonal \(0\) (Titanic–Matrix) — in fact higher, because Vendetta and Matrix overlap on two of three genres (action and sci-fi) where Titanic and Vendetta shared only one (romance). An angle of about \(35.3^\circ\).

E2. Inverse document frequency (§2), base \(10\). The rarer word appears in \(\mathrm{df}=1\) of \(N_{\text{doc}}=4\) documents, the common word in \(\mathrm{df}=2\): \[ \operatorname{idf}(\textit{spaceship})=\log_{10}\!\tfrac{4}{1}=\log_{10}4\approx\boxed{0.60}, \qquad \operatorname{idf}(\textit{love})=\log_{10}\!\tfrac{4}{2}=\log_{10}2\approx\boxed{0.30}. \] At equal raw counts the rarer word carries \(\dfrac{0.60}{0.30}=\boxed{2\times}\) the weight — TF-IDF up-weights the distinctive term and damps the ubiquitous one, exactly as §2’s alien vs romance (\(2.7\times\)) showed with a different \(N_{\text{doc}}\).

E3. Item-based \(k\)-NN (§4): a similarity-weighted average of Cara’s ratings of Titanic’s neighbors. Cara rated Vendetta \(=5\) and Matrix \(=4\), with weights \(s_{\text{Ti,V}}=0.9\) (§4’s \(s_{\text{V,Ti}}\), since similarity is symmetric) and \(s_{\text{Ti,Ma}}=0.6\): \[ \begin{aligned} \hat r_{\text{Cara,Ti}}&=\frac{s_{\text{Ti,V}}\,r_{\text{Cara,V}}+s_{\text{Ti,Ma}}\,r_{\text{Cara,Ma}}}{s_{\text{Ti,V}}+s_{\text{Ti,Ma}}}\\[4pt] &=\frac{0.9(5)+0.6(4)}{0.9+0.6}=\frac{4.5+2.4}{1.5}=\frac{6.9}{1.5}=\boxed{4.60}. \end{aligned} \] Each rating is weighted by how similar that item is to Titanic, and dividing by the weight-sum \(1.5\) renormalizes the votes onto the \(1\)–\(5\) scale (same mechanics as §4’s \(4.27\)). Here Vendetta is the closer neighbor (\(0.9>0.6\)), so its \(5\) pulls harder, landing the prediction at \(4.6\), between Cara’s two ratings and nearer the \(5\).

  1. Shrinkage (§4) multiplies each similarity by \(n/(n+\kappa)\) with \(\kappa=25\): the \(0.9\) backed by \(n=5\) becomes \(0.9\cdot\frac{5}{30}=0.15\) and the \(0.6\) backed by \(n=100\) becomes \(0.6\cdot\frac{100}{125}=0.48\). Then \(\hat r_{\text{Cara,Ti}}=\frac{0.15(5)+0.48(4)}{0.15+0.48}=\frac{2.67}{0.63}\approx\boxed{4.24}\). Matrix now leads (\(0.48>0.15\)): the closer-looking neighbor, Vendetta, is the one backed by few co-ratings, so shrinkage pulls its weight down and the prediction slides from \(4.60\) toward Matrix’s \(4\).

E4. Matrix-factorization dot product (§5), for Bob’s Titanic: \[ \hat r_{\text{Bob,Titanic}}=\mathbf p_{\text{Bob}}\cdot\mathbf q_{\text{Titanic}} =\underbrace{(1)(2)}_{\text{romance}}+\underbrace{(2)(0)}_{\text{action}}=2+0=\boxed{2}. \] Bob actually rated Titanic \(4\) (§1), so the model is too low by \(2\): these hand-picked factors, which §5 says are not fit to the six ratings, do not describe Bob here. The romance factor carries the whole score: Bob’s small romance taste \(1\) meets Titanic’s romance trait \(2\) (\(1\times2=2\)), while Titanic has no action trait, so Bob’s large action taste adds \(2\times0=0\). Ann–Vendetta’s \(2.5\) was built from both factors, \(2\) from romance and \(0.5\) from action. The score is “taste meets traits”: a factor fires only when both the user wants it and the item has it.

E5. Confidence weighting (§5), \(c_{ui}=1+\alpha\,n_{ui}\) with \(\alpha=10\): \[ c(n{=}2)=1+10(2)=\boxed{21},\qquad c(n{=}5)=1+10(5)=\boxed{51}. \] Reaching \(c=101\) needs \(1+10n=101\), so \(n=\boxed{10}\) plays. A never-played cell has \(n=0\) and enters at the floor confidence \(c=1\); because the loss multiplies each squared error by \(c\), the played items are fit far harder than the blanks, which is the §5 cure for implicit data’s positives-only nature.

E6. Factorization machine (§8). The three pairwise interactions depend only on the factor vectors, which are unchanged from §8 — so all three values are identical to §8’s \(4.75\) computation: \[ \begin{gathered} \langle\text{Ann},\text{Vendetta}\rangle=(2)(1)+(0.5)(1)=\boxed{2.5},\\[4pt] \langle\text{Ann},\text{action}\rangle=(2)(-1)+(0.5)(1)=\boxed{-1.5},\\[4pt] \langle\text{Vendetta},\text{action}\rangle=(1)(-1)+(1)(1)=\boxed{0}. \end{gathered} \] The only thing that moved is the action solo weight \(w_{\text{action}}\), from \(-0.25\) to \(-0.75\) — a swing of \(-0.5\) in the linear part. Reassembling: \[ \hat y=\underbrace{4.0}_{w_0}+\underbrace{(-1.0+1.0-0.75)}_{\text{linear}\,=\,-0.75}+\underbrace{(2.5-1.5+0)}_{\text{interactions}\,=\,1.0}=4.0-0.75+1.0=\boxed{4.25}. \] So \(\hat y\) fell from §8’s \(4.75\) to \(4.25\), exactly the \(-0.5\) the solo-weight change injected — the factorized interaction part (\(1.0\)) is untouched because it never depends on the linear weights.

E7. Pairs (§1, §5). A clicked film is paired with each unclicked one; the unclicked films may simply be unseen, which is why BPR compares instead of fitting zeros. With \(2\) clicked and \(8\) unclicked films there are \(2\times8=\boxed{16}\) pairs; after a third click there are \(3\) clicked and \(7\) unclicked, \(3\times7=\boxed{21}\) pairs. Each pair asks only that the clicked film score above the unclicked one, never that the unclicked one score \(0\).

E8. FM score for a brand-new film (§8). The id weight and id factor are \(0\), so every term involving the new film’s id is \(0\): \(\langle\text{Ann},\text{new}\rangle=0\) and \(\langle\text{new},\text{action}\rangle=0\). Only \(\langle\text{Ann},\text{action}\rangle=(2)(-1)+(0.5)(1)=-1.5\) remains. Then \[\hat y=4.0+(-1.0+0-0.25)+(0-1.5+0)=4.0-1.25-1.5=\boxed{1.25}.\] The zero-valued id contributes nothing, and the score is carried by Ann’s solo weight, the action solo weight and the Ann\(\times\)action interaction: the side feature still speaks where the id has nothing to say. The score is low, \(1.25\) against the \(4.0\) global mean, because Ann’s solo weight (\(-1.0\)), the action solo weight (\(-0.25\)) and the Ann\(\times\)action interaction (\(-1.5\)) all pull down, and the new id has no weight to offset them.

E9. User-based \(k\)-NN with mean-centering (§4). First each user’s own mean over what they rated: \(\bar r_{\text{Cara}}=\tfrac{5+4}{2}=4.5\), \(\bar r_{\text{Ann}}=\tfrac{5+1}{2}=3\), \(\bar r_{\text{Bob}}=\tfrac{4+5}{2}=4.5\). Their Titanic deviations are \(r_{\text{Ann,Ti}}-\bar r_{\text{Ann}}=5-3=+2\) and \(r_{\text{Bob,Ti}}-\bar r_{\text{Bob}}=4-4.5=-0.5\). With \(s_{\text{Cara,Ann}}=0.2\), \(s_{\text{Cara,Bob}}=0.8\): \[ \begin{aligned} \hat r_{\text{Cara,Ti}}&=\bar r_{\text{Cara}}+\frac{s_{\text{Cara,Ann}}(+2)+s_{\text{Cara,Bob}}(-0.5)}{s_{\text{Cara,Ann}}+s_{\text{Cara,Bob}}}\\[4pt] &=4.5+\frac{0.2(2)+0.8(-0.5)}{0.2+0.8}=4.5+\frac{0.4-0.4}{1}=4.5+0=\boxed{4.5}. \end{aligned} \] The prediction is \(\boxed{4.5}\), exactly Cara’s own mean, because the weighted deviations cancel: Ann sat \(+2\) above her baseline but is weighted only \(0.2\) (\(0.4\)), while Bob sat \(0.5\) below his and is weighted \(0.8\) (\(-0.4\)) — equal and opposite. The neighbors’ “above-average” and “below-average” votes net to zero, so the model predicts Cara is simply average for her on Titanic. (Centering is what makes Ann’s stingy scale and Bob’s generous one comparable in the first place — §4’s Pearson move.)

E10. Scoring by dot product (§5), with \(\mathbf p=[2,1,3]\). (a) \(\hat r_1=\mathbf p\cdot\mathbf q_1=(2)(4)+(1)(0)+(3)(0)=8\) and \(\hat r_2=\mathbf p\cdot\mathbf q_2=(2)(0)+(1)(0)+(3)(2)=6\). Since \(8>6\), dot product ranks \(\mathbf q_1\) first (\(\boxed{\mathbf q_1\succ\mathbf q_2}\)). (b) Cosine needs the norms: \(\lVert\mathbf p\rVert=\sqrt{4+1+9}=\sqrt{14}\approx3.742\), \(\lVert\mathbf q_1\rVert=\sqrt{16}=4\), \(\lVert\mathbf q_2\rVert=\sqrt{4}=2\). Then \[ \cos(\mathbf p,\mathbf q_1)=\frac{8}{\sqrt{14}\cdot4}=\frac{2}{\sqrt{14}}\approx\boxed{0.535}, \qquad \cos(\mathbf p,\mathbf q_2)=\frac{6}{\sqrt{14}\cdot2}=\frac{3}{\sqrt{14}}\approx\boxed{0.802}. \] Now \(\mathbf q_2\) wins (\(0.802>0.535\)), so the two rankings disagree (\(\boxed{\text{no}}\)). Raw dot product favored \(\mathbf q_1\) largely because of its bigger norm (\(\lVert\mathbf q_1\rVert=4\) vs \(\lVert\mathbf q_2\rVert=2\)): \(\mathbf q_1\) is twice as long, so its raw dot product is inflated by length, not by better direction agreement. Cosine divides that length out and reveals \(\mathbf q_2\) is the better directional match — exactly the length effect of §5’s latent-map caption, which §5’s “In practice” box links to popularity bias, and which dividing every vector by its length before scoring (the Linear-Algebra primer’s serving trick, its §4) removes.

E11. FM with a genre feature (§8), on the blank Ann–Vendetta tagged sci-fi. (a) Pairwise interactions: \(\langle\text{Ann},\text{Vendetta}\rangle=(2)(1)+(0.5)(1)=2.5\) (unchanged from §8); \(\langle\text{Ann},\text{sci-fi}\rangle=(2)(-1)+(0.5)(0.5)=-2+0.25=-1.75\); \(\langle\text{Vendetta},\text{sci-fi}\rangle=(1)(-1)+(1)(0.5)=-0.5\). (b) Global bias \(w_0=4.0\); linear \(-1.0+1.0-0.25=-0.25\); interactions \(2.5-1.75-0.5=0.25\); total \(\hat y=4.0-0.25+0.25=\boxed{4.0}\), inside the \(1\)–\(5\) scale. That is \(0.75\) below §8’s \(4.75\), and all of the drop comes from the interactions (the solo weights did not change): the sci-fi tag points as far from romance as the action tag did but only half as far toward action, so it clashes a little more with Ann (\(-1.75\) against \(-1.5\)) and now pulls Vendetta’s term below zero (\(-0.5\) against \(0\)).

E12. Coverage (§6). (a) The share asked for is coverage, the fraction of the catalog that appears in anyone’s top-\(K\) list. Here the lists of all customers together contain \(8\) different films out of \(50\), so coverage \(=8/50=\boxed{0.16}\). High Recall@\(2\) and low coverage go together when the held-out films are mostly the best sellers: the popularity list scores well while \(84\%\) of the catalog is never shown. (b) Both are hurt. Customers never meet the niche films they would have liked, and the makers of those films get no exposure, so those films stay without ratings and keep losing to the best sellers.

E13. Jaccard for binary data (§4). (a) \(A\cap B=\{3,4\}\) (size \(2\)), \(A\cup B=\{1,2,3,4,5\}\) (size \(5\)), so Jaccard \(=2/5=\boxed{0.4}\). (b) Jaccard reads as a plain fraction — of the \(5\) users who watched either item, \(2\) watched both — and it is the stricter measure. For sets, cosine is \(|A\cap B|/\sqrt{|A|\cdot|B|}=2/\sqrt{4\cdot3}=2/\sqrt{12}\approx0.58\) (over each item’s whole audience set, the binary-data convention of the similarity table in §4), above Jaccard’s \(0.40\); Jaccard is never the larger of the two, and the gap widens when one item’s audience is much bigger than the other’s (100 viewers against 10, all 10 shared: cosine \(10/\sqrt{1000}\approx0.32\), Jaccard \(10/100=0.10\)).

So choose Jaccard when a big gap in audience size should count against a match (the \(0.10\) above), and cosine when a small audience lying wholly inside a big one should still count as close (the \(0.32\)), as with a niche film watched almost only by blockbuster fans; which is right is a design choice, not a tested rule. Neither measure is fooled by shared zeros: adding a thousand users who watched neither item changes neither number. A measure that does count matching zeros — simple matching, the share of users on whom the two items agree — would jump from \(0.40\) (among just these five users, \(2/5\)) to about \(0.997\) (\(1002/1005\)) once the thousand extra users are counted.

E14. \(e=4-(1\cdot2+2\cdot0)=2\). \(\mathbf p\leftarrow[1,2]+0.1(2[2,0]-0.1[1,2])=[1,2]+0.1[3.9,-0.2]=[1.39,\,1.98]\); \(\mathbf q\leftarrow[2,0]+0.1(2[1,2]-0.1[2,0])=[2,0]+0.1[1.8,\,4]=[2.18,\,0.4]\). New score \(1.39\cdot2.18+1.98\cdot0.4=3.0302+0.792=3.82\), up from \(2\) toward \(4\): both vectors moved the way that shrinks the error, and one step closed \(1.82\) of the gap of \(2\), about \(91\%\).

E15. (a) \(21/31=0.677\) (b) \(101/111=0.910\) (c) \(p\to1\) as \(c\to\infty\) (the ratio \(c/(c+10)\) tends to \(1\)); the \(\lambda\) in the denominator keeps it below \(1\). Reading: \(p\) rises quickly at first (\(0.677\) at \(c=21\), \(0.910\) at \(c=101\)) and then barely at all, so each extra unit of confidence buys less and less.

Evaluation Metrics

E1. Rating error (§2). The errors are \([3{-}4,\,5{-}5,\,2{-}4,\,4{-}3]=[-1,0,-2,1]\). \[ \text{MAE}=\frac{|{-}1|+|0|+|{-}2|+|1|}{4}=\frac{4}{4}=\boxed{1.0}, \qquad \text{RMSE}=\sqrt{\frac{1+0+4+1}{4}}=\sqrt{1.5}\approx\boxed{1.225}. \] RMSE (\(1.225\)) is the larger, because squaring punishes big misses harder, and the single error of \(-2\) on the third movie (which becomes \(4\) inside the sum, two-thirds of the squared total) is what opens the gap above MAE — exactly the “RMSE vs MAE” lesson of §2.

E2. Top-\(K\) set metrics (§4), for the second user: relevant \(\{C,F,G\}\), top-5 \([C,A,F,B,D]\). The hits are \(C\) (rank 1) and \(F\) (rank 3), so there are \(2\) hits among the \(K=5\) shown and \(|\mathcal{R}|=3\) relevant items in all (\(G\) is missed): \[ \begin{gathered} \text{Precision@5}=\frac{2}{5}=\boxed{0.4},\\[4pt] \text{Recall@5}=\frac{2}{3}\approx\boxed{0.667},\\[4pt] \text{F1@5}=\frac{2(0.4)(0.667)}{0.4+0.667}=\frac{0.533}{1.067}=\boxed{0.5}. \end{gathered} \] (On the reciprocals: \(1/0.4=2.5\) and \(1/0.667=1.5\) average to \(2\), and \(1/2=0.5\).) Precision = of the 5 slots spent, \(40\%\) were good (“don’t waste the user’s attention”); Recall = \(2\) of the \(3\) relevant items were found (“don’t miss the good stuff” — \(G\) is the one missed); F1 = the harmonic mean balancing the two into a single \(0.5\).

E3. Reciprocal rank and MRR (§5). The second user’s first relevant item, \(C\), sits at rank 1, so \(\text{RR}=\tfrac11=\boxed{1}\). The next user’s first relevant item is at rank 5, so \(\text{RR}=\tfrac15=\boxed{0.2}\), and the last user’s at rank 3 gives \(\text{RR}=\tfrac13\approx\boxed{0.333}\). Averaging the three, \[ \text{MRR}=\frac{1+0.2+0.333}{3}=\frac{1.533}{3}\approx\boxed{0.511}. \] MRR looks only at the first hit: the user who found a good film at once counts \(5\) times as much as the user who had to read to the end of the list.

E4. Average Precision (§6), second user. The hits are at ranks 1 (\(C\)) and 3 (\(F\)), so the precision at each hit position is \[ P@1=\frac{1}{1}=1,\qquad P@3=\frac{2}{3}\approx0.667. \] Summing these and dividing by the \(|\mathcal{R}|=3\) relevant items (the convention that charges you for the missed \(G\)): \[ \text{AP}=\frac{P@1+P@3}{3}=\frac{1+0.667}{3}=\frac{1.667}{3}\approx\boxed{0.556}. \] Dividing by \(3\) (not by the \(2\) hits) is what makes the un-found \(G\) cost you — exactly the convention noted in §6.

E5. NDCG@5 (§7), second user. The gains down the ranks are \([1,0,1,0,0]\) (for \(0/1\) labels the gain \(2^{\text{rel}}-1\) is the label itself), so only ranks 1 and 3 contribute: \[ \text{DCG@5}=\frac{1}{\log_2 2}+\frac{1}{\log_2 4}=1+0.5=1.5. \] The ideal ordering puts all three relevant items in the top three slots (gains \(1,1,1\) at ranks 1–3), giving (with \(\log_2 3\approx1.585\)) \[ \text{IDCG@5}=\frac{1}{\log_2 2}+\frac{1}{\log_2 3}+\frac{1}{\log_2 4}=1+0.631+0.500\approx2.131, \] \[ \text{NDCG@5}=\frac{\text{DCG@5}}{\text{IDCG@5}}=\frac{1.5}{2.131}\approx\boxed{0.704}. \] That is higher than the running list’s \(0.593\): the second user’s first hit sits at rank 1, the one position with no discount, while the running list spends rank 1 on the irrelevant \(A\). The normalization by IDCG is what pins each score into \([0,1]\) and makes the two users comparable although they have different numbers of relevant items.

E6. AUC as a pairwise probability (§8). With relevant scores \(\{0.8,0.5\}\) and irrelevant scores \(\{0.6,0.3\}\) there are \(2\times2=4\) (relevant, irrelevant) pairs: \[ 0.8\!>\!0.6\ ✓,\qquad 0.8\!>\!0.3\ ✓,\qquad 0.5\!<\!0.6\ ✗,\qquad 0.5\!>\!0.3\ ✓. \] The model orders 3 of the 4 pairs correctly, so \[ \text{AUC}=\frac{\#\text{correctly-ordered pairs}}{\#\text{pairs}}=\frac{3}{4}=\boxed{0.75}. \] Since \(0.75>0.5\) it beats random guessing — the one mistake (\(0.5\) scored below the irrelevant \(0.6\)) is the only pair pulling it below a perfect \(1.0\).

E7. IPS-weighted recall (§12.4). (a) The weights are \(1/p\): \[ w_C=\frac{1}{0.5}=\boxed{2},\qquad w_F=\frac{1}{0.25}=\boxed{4},\qquad w_G=\frac{1}{0.1}=\boxed{10}. \] (b) The hits are \(C\) and \(F\), so the weight of the hits is \(2+4=6\), and the weight of all three relevant films is \(2+4+10=16\): \[ \text{Recall@5}_{\text{IPS}}=\frac{6}{16}=\boxed{0.375},\qquad\text{raw Recall@5}=\frac{2}{3}\approx\boxed{0.667}. \] (c) The missed film \(G\). It was rarely shown (\(p_G=0.1\)), so its weight \(10\) is \(10/16=62.5\%\) of the total, though in the raw count it is only \(1\) of \(3\) films. Missing a film that the old system hardly ever showed is the expensive mistake once exposure bias is undone, so the exposure-corrected recall is much lower than the raw one.

E8. Beyond-accuracy (§10). (a) Gini for counts \([6,3,1,0,0]\) (\(n=5\), total \(10\), mean \(\mu=2\)). Counting each pair once: \(|6-3|=3\), \(|6-1|=5\), \(|6-0|=6\) twice, \(|3-1|=2\), \(|3-0|=3\) twice, \(|1-0|=1\) twice, \(|0-0|=0\) — a total of \(30\). The double sum visits every ordered pair, so it counts each gap twice: \(\sum_i\sum_j|x_i-x_j|=2\times30=60\), and \[ G=\frac{\sum_i\sum_j|x_i-x_j|}{2n^2\mu}=\frac{60}{2\cdot25\cdot2}=\frac{60}{100}=\boxed{0.6}. \] At \(0.6\) this catalog is less concentrated than the chapter’s \([8,2,0,0,0]\) (which gives \(0.72\)): its recommendations are spread a little more evenly, with the head item taking \(6\) of \(10\) rather than \(8\). (b) Of the second user’s \(2\) relevant hits, only \(1\) (\(F\)) is a non-obvious find, so \[ \text{serendipity@5}=\frac{1}{5}=\boxed{0.2}. \] It sits below the Precision@5 of \(0.4\) (E2) because precision credited both hits, whereas serendipity discards the obvious blockbuster \(C\) that a popularity baseline would have surfaced anyway — it rewards only the surprising good picks.

E9. Two lists, four metrics (§4, §5, §7). (a) Each list has \(2\) hits among \(5\) shown and \(4\) relevant films in all, so for both lists \(\text{Precision@5}=\tfrac25=\boxed{0.4}\) and \(\text{Recall@5}=\tfrac24=\boxed{0.5}\). (b) For list P the first hit is at rank 1, so \(\text{RR}=\boxed{1}\); for list Q it is at rank 3, so \(\text{RR}=\tfrac13\approx\boxed{0.333}\). For NDCG@5: \[ \begin{aligned} \text{DCG}_P&=\frac{1}{\log_2 2}+\frac{1}{\log_2 3}=1+0.631=1.631,\\ \text{DCG}_Q&=\frac{1}{\log_2 4}+\frac{1}{\log_2 6}=0.5+0.387=0.887, \end{aligned} \] \[ \text{NDCG@5}_P=\frac{1.631}{2.562}\approx\boxed{0.637},\qquad \text{NDCG@5}_Q=\frac{0.887}{2.562}\approx\boxed{0.346}. \] (c) If only the first suggestion is ever used, trust RR (MRR over users): it measures exactly how early the first good film appears, and list P’s \(1\) is \(1\div\tfrac13=3\) times list Q’s \(0.333\). Precision and recall give the two lists the same values, as part (a) shows, because they count hits and ignore where in the top \(5\) the hits sit; RR and NDCG separate them.

E10. Full against sampled (§11b). (a) No. Under full ranking the held-out items sit at ranks \(5\) and \(6\), both outside the top \(2\), so both models miss HR@2. (b) For P, the held-out item makes the top \(2\) of its four candidates if at most one of the three sampled negatives is among the \(4\) items that outrank it. With none: all three come from the \(3\) lower items, \(1\) way. With one: \(4\times3=12\) ways (one of the \(4\) higher items, two of the \(3\) lower ones). That is \(13\) of the \(35\) samples: \[ P(\text{sampled hit, P})=\frac{13}{35}\approx\boxed{0.371}. \] For Q there are only \(2\) lower items, so no sample can avoid outrankers entirely; one outranker and the \(2\) lower items gives \(5\times1=5\) ways: \[ P(\text{sampled hit, Q})=\frac{5}{35}\approx\boxed{0.143}. \] (c) Full ranking says both models score \(0\); the sampled metric gives them \(0.371\) and \(0.143\). Sampling only removes competitors, so a sampled number is optimistic and is not the full-ranking number; it also changes the gap between the models. Never compare a sampled number with a full-ranking one, or two sampled numbers from different protocols.

E11. Looks better, but ship? (§11d). (a) \(\bar d=(0.012-0.006+0.011-0.004+0.002)/5=0.015/5=+0.003\) — positive, matching the headline. (b) With \(s_d=0.0083\), \(\text{SE}=s_d/\sqrt5=0.0037\) and the multiplier \(2.776\), the \(95\%\) CI is \(0.003\pm2.776(0.0037)\approx[-0.007,+0.013]\), which includes \(0\) (equivalently the paired \(t=0.81\), \(p=0.46\)). (c) Do not ship. A positive mean is not enough: because the per-seed gains swing in sign, the gain is statistically indistinguishable from \(0\) — the \(+0.003\) is within run-to-run noise, so on this evidence B is not better than A.

E12. Calibration (§11e). (a) Observed click rates: bin 1 \(\tfrac{7}{10}=0.70\), bin 2 \(\tfrac{9}{20}=0.45\), bin 3 \(\tfrac{2}{10}=0.20\). The gaps (predicted minus observed) are \(0.9-0.7=0.20\), \(0.6-0.45=0.15\) and \(0.2-0.2=0\). (b) Weight each gap by its bin’s share of the \(40\) predictions (\(\tfrac{10}{40}\), \(\tfrac{20}{40}\), \(\tfrac{10}{40}\)): \[ \text{ECE}=\frac{10(0.20)+20(0.15)+10(0)}{40}=\frac{5}{40}=\boxed{0.125}. \] (c) Over-confident: the gaps (predicted minus observed) are positive, \(+0.20\) and \(+0.15\), in the two bins with the larger predictions, so those scores promise more clicks than they deliver (the low bin, with gap \(0\), is on the diagonal). A post-hoc rescaling fitted on the validation set, Platt scaling or isotonic regression, would pull those scores down without changing the order of the films.

E13. Fairness (§12.5). (a) The studios’ exposure rate is \(\tfrac{45}{60}=\boxed{0.75}\) and the creators’ \(\tfrac{15}{60}=\boxed{0.25}\), so the demographic-parity gap is \(0.75-0.25=\boxed{0.50}\); parity would need \(30\) slots each. (b) The true-positive rates are \(\tfrac{38}{50}=\boxed{0.76}\) and \(\tfrac{21}{60}=\boxed{0.35}\), an equal-opportunity gap of \(\boxed{0.41}\): a user who would like the film is more than twice as likely to be shown it in group 1. (c) The \(45/15\) split earns \(45(0.10)+15(0.06)=4.5+0.9=\boxed{5.4}\) clicks; the equal \(30/30\) split earns \(30(0.10)+30(0.06)=3.0+1.8=\boxed{4.8}\), which is \(0.6\) fewer (about \(11\%\)). That is the price of parity in this toy, and whether it is worth paying is a choice about which good to optimize, not a calculation.

Sequential & Session-Based Recommendation

E1. (a) The adjacent pairs are \(\text{M}\!\to\!\text{V}\) three times (sessions 1, 2, 3), \(\text{V}\!\to\!\text{T}\) twice (sessions 1 and 3) and \(\text{M}\!\to\!\text{T}\) once (session 4). Out of M there are \(3+1=4\) transitions, so \(P(\text{V}\mid\text{M})=\tfrac34=\boxed{0.75}\) and \(P(\text{T}\mid\text{M})=\tfrac14=0.25\). Out of V there are \(2\) transitions, both to T, so \(P(\text{T}\mid\text{V})=\tfrac22=1.0\). (b) Titanic is the last film of every session it appears in, so it has no outgoing transitions: its row is empty, the probability would be \(\tfrac00\), and the chain has no prediction after Titanic. In plain words: a counted table can only say what followed a film if somebody once watched something after it. With the extra session \(\text{T}\!\to\!\text{M}\) the T row has one transition, to Matrix, so the chain now predicts Matrix with probability \(\tfrac11=\boxed{1.0}\), from a single observation.

E2. After a session ending in Vendetta the chain looks only at the V row, and it recommends Titanic with probability \(\boxed{1.0}\) — for Ann and for Cara alike, although Ann watched Matrix before and Cara watched Titanic before. The identical answers show that a first-order chain sees only the last film: the film before it makes no difference to the output.

A chain whose state is the last two films can tell them apart, because (Matrix, Vendetta) and (Titanic, Vendetta) are different states; its table needs a row for every ordered pair of the three films, \(3\times3=\boxed{9}\) rows (against \(3\)), and grows by a factor of \(3\) with each extra film of memory. A model with a memory of the whole session, GRU4Rec’s hidden state or SASRec’s attention, avoids that growth. (Recommending Titanic to Cara repeats a film she has just watched; a real system drops items already in the session — a claim from practice, not tested here.)

E3. Reversed session \((\text{Vendetta},\text{Titanic})\), so the query is Titanic \(=[1,0]\) (§4). Raw scores against the two keys, scaled by \(\sqrt2\): against Vendetta \([0,1]\), \(\tfrac{[1,0]\cdot[0,1]}{1.414}=0\); against Titanic \([1,0]\), \(\tfrac{[1,0]\cdot[1,0]}{1.414}=0.707\). Softmax of \([0,0.707]\) gives weights \(\alpha_{\text{V}}=0.330,\ \alpha_{\text{T}}=0.670\). Context \(=0.330[0,1]+0.670[1,0]=[0.670,0.330]\). Candidate scores: \[ \text{Titanic}=0.670,\qquad \text{Vendetta}=0.330,\qquad \text{Matrix}=[0.670,0.330]\cdot[1,1]=\boxed{1.000}. \] Matrix is still recommended (\(1.000\) — it shares a feature with both), but the ranking of the other two has flipped: now Titanic (\(0.670\)) beats Vendetta (\(0.330\)), the reverse of §4.

Why — and it is not recency: the query is always the last item, and with identity projections each toy film matches itself at least as well as any other (\([1,0]\cdot[1,0]=1\) against \([1,0]\cdot[0,1]=0\)), so whichever film ends the session takes the larger weight. The bare step has no notion of the order of the earlier films: keep the query and merely relist the keys, and each film keeps its weight. What reversing the session changed is which item is the query. (Nor does the last item always win: for a session \((\text{Matrix},\text{Vendetta})\) both scaled scores are \(0.707\) and the weights tie at \(0.5\) each.) A trained SASRec can prefer recent items on purpose — but only through its positional embeddings (§4).

E4. The three exponentials are \(e^{0}=1\), \(e^{0.5}\approx1.649\) and \(e^{1.2}\approx3.320\), and their sum is \(1+1.649+3.320=5.969\). The weights are \[ \frac{1}{5.969}\approx0.168,\qquad \frac{1.649}{5.969}\approx0.276,\qquad \frac{3.320}{5.969}\approx\boxed{0.556}, \] and \(0.168+0.276+0.556=1.000\). The biggest weight is \(0.556/0.168\approx3.3\) times the smallest, which is exactly \(e^{1.2-0}=e^{1.2}\approx3.32\): a softmax weight depends on the gap between scores, not on the scores themselves. In plain words: a position whose score is \(1.2\) higher than another’s gets about \(3.3\) times the attention.

E5. Score Inception \([2,0]\) against \(\mathbf c=[0.330,0.670]\): \(\mathbf c\cdot[2,0]=0.330(2)+0.670(0)=\boxed{0.660}\). Ranking: Matrix \(1.000 >\) Vendetta \(0.670 >\) Inception \(0.660 >\) Titanic \(0.330\) — Inception slips in just below Vendetta. Its score is large mostly because of its norm (the length of a vector, Linear-Algebra primer §4; \(\lVert[2,0]\rVert=\sqrt{2^2+0^2}=2\), twice Titanic’s and Vendetta’s): the raw dot product scales with length, so a long vector outscores a short one pointing the same way. Inception points in exactly Titanic’s direction — both make the same angle with \(\mathbf c\) (\(\cos\theta=0.442\)) — and scores exactly twice as much, \(0.660\) against \(0.330\).

Direction has not stopped mattering: a dot product is \(\lVert\mathbf a\rVert\,\lVert\mathbf b\rVert\cos\theta\) (\(\theta\) the angle between the two), so length only amplifies whatever alignment is already there. That is the same length / popularity bias flagged in Traditional Recommender Systems §5’s “In practice” box (and exercise 10 there) — the reason serving often scores by cosine (the dot product divided by both lengths, Linear-Algebra primer §4) rather than the bare dot product. The two cosines are both \(0.442\), so scoring by cosine makes the two vectors tie.

E6. (a) \(h_1=\tanh(0.5\times0+2)=\tanh 2=\boxed{0.964}\); \(h_2=\tanh(0.5\times0.964+0)=\tanh0.482=0.448\); \(h_3=\tanh(0.5\times0.448)=\tanh0.224=0.220\). (b) Carrying on, \(h_4=0.110\), \(h_5=0.055\), \(h_6=0.027\), \(h_7=0.014\) and \(h_8=0.007\), so the entry first falls below \(0.01\) at \(h_8\), that is seven steps after \(h_1\): once the number is small, \(\tanh x\approx x\) and each step simply halves it.

  1. Each step from \(h_8=0.007\) roughly halves the entry, so \(h_{50}\approx0.007\times0.5^{42}\approx10^{-15}\) (iterating the cell gives \(1.6\times10^{-15}\)): nothing is left. The opening films of a fifty-film session are gone from the summary, because the recurrent state must squeeze the whole past through one vector, updated strictly step by step.

E7. (a) Position 3’s query is Matrix \([1,1]\); its scaled scores against the keys Titanic, Vendetta and Matrix are \(1/1.414=0.707\), \(0.707\) and \(2/1.414=1.414\). The exponentials are \(2.028\), \(2.028\) and \(4.113\) (sum \(8.169\)), so the weights are \(0.248\), \(0.248\) and \(0.503\). The context is \(0.248[1,0]+0.248[0,1]+0.503[1,1]=[0.752,\ 0.752]\), and Matrix scores \([0.752,0.752]\cdot[1,1]=\boxed{1.503}\).

  1. With position 3 blank, every score is \(0\), so the three weights are \(\tfrac13\) each; the context is \(\tfrac13\big([1,0]+[0,1]+[0,0]\big)=[0.333,\ 0.333]\) and Matrix scores \([0.333,0.333]\cdot[1,1]=\boxed{0.667}\). In the blank run the score depends only on Titanic and Vendetta, so it carries information about the neighbours. In the unmasked run (a) Matrix’s own vector had weight \(0.503\) and lifted its score to \(1.503\): the answer was sitting in the input. Prefer the masked run for training, since only it forces the model to learn from the surroundings. In plain words: you cannot test a student on a sentence already written on the exam sheet.

E8. (a) A: \(\tfrac13\big([1,0]+[0,1]+[1,1]\big)=[0.667,\ 0.667]\); B: \(\tfrac13\big([0,1]+[1,0]+[1,1]\big)=\boxed{[0.667,\ 0.667]}\), the same vector, because adding is commutative.

  1. In both sessions the query is Matrix \([1,1]\). Scaled scores: Titanic \(1/1.414=0.707\), Vendetta \(0.707\), Matrix \(2/1.414=1.414\); exponentials \(2.028\), \(2.028\), \(4.113\) (sum \(8.169\)), so the weights are \(0.248\), \(0.248\), \(0.503\). The context is \(0.248[1,0]+0.248[0,1]+0.503[1,1]=[0.752,\ 0.752]\), the same for A and for B, because each film keeps its weight wherever it sits in the list.

  2. None of the three can. The bag average and the bare step give identical vectors; the first-order chain looks only at the last film, Matrix in both. The sessions differ only in the order of the earlier films, and nothing in these three models reads that order. In plain words: “bag” means “who was in the evening”, not “in what order they came”; a model has to be given each film’s position to see the difference.

  3. The query is still Matrix \([1,1]\). Session A has keys and values \([1.5,0]\), \([0,1]\), \([1,1]\) and session B has \([0.5,1]\), \([1,0]\), \([1,1]\). In both the scaled scores are \(1.061\), \(0.707\), \(1.414\), so the weights are the same, \(0.320\), \(0.225\), \(0.456\). The contexts are \(A:\ [0.935,\ 0.680]\) and \(B:\ [0.840,\ 0.775]\): they now differ, because the added vector changes which film’s value sits at position 1.

E9. (a) Past only: train on Titanic, so the profile is \([1,0]\). Matrix scores \([1,0]\cdot[1,1]=1.0\) and the decoy \([1,0]\cdot[1.5,0]=1.5\): the decoy wins and \(\text{HR}@1=0\). (b) With the later Vendetta in training, the profile is \(\tfrac12\big([1,0]+[0,1]\big)=[0.5,0.5]\). Matrix scores \(0.5+0.5=1.0\) and the decoy \(\boxed{0.75}\): Matrix wins and \(\text{HR}@1=1\).

  1. The case in (b) is the leak: Vendetta happened after the hidden Matrix, and Matrix \(=[1,1]\) shares Vendetta’s feature, so the model saw a hint of the answer from the future; that is why a random split tends to give an inflated HR / NDCG (a claim from practice, not tested here; the §6 toy shows \(0\) versus \(0.5\)). The leave-one-out split avoids it by hiding each user’s last film. Zed’s June click is not kept out by leave-one-out unless it is his own last or second-last film: it holds the future back for each user separately, so otherwise Zed’s later click stays in training and can inform a test on Cara’s earlier film. The global temporal split (one cut-off date for everybody, here in April) keeps it out: everything after the date is test data.

E10. FPMC score \(=\underbrace{\mathbf p_u\cdot\mathbf q_i}_{\text{MF / user taste}}+\underbrace{\mathbf q'_l\cdot\mathbf q''_i}_{\text{Markov / last item}}\). (a) A brand-new, anonymous session has a cold \(\mathbf p_u\) (an average vector that says nothing about this visitor), so the MF term adds almost nothing, which leaves the Markov term to carry the prediction (the model leans on the strong “what follows the last item” signal). (b) For a well-known user whose last click was an accidental mis-tap, the MF term dominates (their learned taste \(\mathbf p_u\) is reliable) while the Markov term is misled by the bad last item — so we want MF to outweigh it.

The model of this chapter that uses only a last-item signal and no user term is the plain first-order Markov chain of §2 — but its version of that signal is a counted transition table, not the learned dot product \(\mathbf q'_l\cdot\mathbf q''_i\); replacing the counts with that dot product is exactly what FPMC’s second term does, which is why FPMC can score pairs the log never showed.

E11. (a) Titanic: MF term \([0.3,0.6]\cdot[1,0]=0.3\); Markov term \([0.5,1]\cdot[0.4,0.2]=0.2+0.2=0.4\); score \(=\boxed{0.7}\). Vendetta: MF term \([0.3,0.6]\cdot[0,1]=0.6\); Markov term \([0.5,1]\cdot[0.1,0.3]=0.05+0.3=0.35\); score \(=\boxed{0.95}\). (b) Vendetta is recommended (\(0.95>0.7\)). The Markov term slightly favours Titanic (\(0.4\) against \(0.35\)), but the MF term favours Vendetta by \(0.3\), which is more, so Cara’s taste outweighs the last-film signal. In plain words: two small votes, and the bigger gap wins.

E12. (a) Training films: Inception, Titanic, Vendetta. Validation example: (Inception, Titanic, Vendetta) \(\to\) Matrix, the second-last film as target. Test example: (Inception, Titanic, Vendetta, Matrix) \(\to\) Joker, the last film as target. (b) The three training films make two pairs, (Inception) \(\to\) Titanic and (Inception, Titanic) \(\to\) Vendetta, so \(\boxed{2}\) training examples. (c) With nothing held out, \(n=5\) films give \(n-1=\boxed{4}\) examples, so leave-one-out costs two of them. (d) With \(n\) films the training part holds \(n-2\) films, which make \(n-3\) pairs; \(n-3=3\) gives \(n=\boxed{6}\). In plain words: two films are set aside, and one more is needed to start the first pair.

E13. (a) With \(\tilde h=0\) each step multiplies the entry by \(1-z\). For \(z=0.3\): \(0.5\to0.35\to\boxed{0.245}\). For \(z=0.8\): \(0.5\to0.1\to\boxed{0.02}\). (b) After six films the entry is multiplied by \((1-z)^6\): for \(z=0.3\), \(0.7^6=0.118\), so about \(12\%\) of the first film’s entry is still there; for \(z=0.8\), \(0.2^6=0.000064\), so it is gone. The small gate is the one a model needs when an early film must still matter. The large gate suits a session where only the latest films count (a user who has just changed interests), because it nearly overwrites the entry at every step. (c) The entry after \(k\) films is \(0.5(1-z)^k\). For \(z=0.3\), \(0.5\times0.7^{k}<0.01\) first holds at \(k=\boxed{11}\) (\(0.7^{11}=0.0198\), entry \(0.0099\)); for \(z=0.8\) at \(k=\boxed{3}\) (\(0.5\times0.2^3=0.004\)). In plain words: a gate near \(1\) writes the new, a gate near \(0\) keeps the old.

From Graphs to LightGCN

E1. The edges are \(X\!-\!F_1\), \(X\!-\!F_2\), \(Y\!-\!F_2\), \(Y\!-\!F_3\), so \[ R=\begin{bmatrix}1&1&0\\0&1&1\end{bmatrix}, \qquad A=\begin{bmatrix} 0&0&1&1&0\\ 0&0&0&1&1\\ 1&0&0&0&0\\ 1&1&0&0&0\\ 0&1&0&0&0 \end{bmatrix} \] (node order \(X,Y,F_1,F_2,F_3\); the top-left and bottom-right blocks are \(0\), as in §7). \(A\) contains \(\boxed{8}\) ones, twice the \(4\) edges: every edge is written once in \(R\) and once in the mirror block \(R^{\top}\). The degrees are the row sums: \(\deg(X)=1+1=\boxed{2}\) and \(\deg(F_2)=1+1=\boxed{2}\) (watched by \(X\) and \(Y\)). In words: a \(1\) is one end of an edge, so the ones are always twice the edges (\(7\) edges give \(14\) ones), and a row sum counts a node’s neighbors.

E2. The adjacency has \(1\)s only between \(U\) and each film, so \(\deg U=3\) and each film has degree \(1\): \[ A=\begin{bmatrix}0&1&1&1\\1&0&0&0\\1&0&0&0\\1&0&0&0\end{bmatrix}, \qquad D=\mathrm{diag}(3,1,1,1). \] Symmetric operator: each edge \(U\!-\!F\) weighs \(\tfrac{1}{\sqrt3\sqrt1}=\boxed{0.577}\) in both directions. Its row sums are \(3\times0.577=\boxed{1.732}\) for \(U\) and \(0.577\) for each film, so no row sums to \(1\). Plain average \(D^{-1}A\): \(U\) hears each film at \(\tfrac13=0.333\), and each film hears \(U\) at \(\tfrac11=1\); every row sums to \(1\), \(0.333\times3=1\) for \(U\) and \(1\) for each film. In words: the plain average is an average of the neighbors, so its rows sum to \(1\); the symmetric rule weighs an edge by both ends, so the same edge counts \(0.577\) each way, and the star’s centre, with three neighbors, receives more in total than a single film does.

E3. Vendetta’s neighbors are Bob and Cara, both of degree \(2\) (weight \(\tfrac{1}{\sqrt2\sqrt2}=0.5\) each): \[ \mathbf e_V^{(1)}=0.5\,[0,3]+0.5\,[1,1.5]=\boxed{[0.5,\,2.25]}. \] Cara’s neighbors are Vendetta (degree \(2\), weight \(0.5\)) and Matrix (degree \(1\), weight \(0.707\)), with the changed Matrix start \([1,1]\): \[ \mathbf e_C^{(1)}=0.5\,[1,1]+0.707\,[1,1]=\boxed{[1.207,\,1.207]}. \] Her angle before is \(\arctan(1.5/1)=56.3^\circ\) and after it is \(\arctan(1.207/1.207)=\boxed{45^\circ}\). In words: Cara’s new arrow is built only from her two films, both pointing at \(45^\circ\) here, so she lands exactly on \(45^\circ\) whatever her own start was.

E4. (a) The vector is \[ \begin{aligned} \mathbf e_A^{(5)}&=0.442\,[2,0]+0.221\,[1,1]+0.0625\,[0,2]\\ &=[0.884+0.221,\;0.221+0.125]=\boxed{[1.105,\,0.346]} \end{aligned} \] (b) The share \(0.0625\) is the one walk from Matrix to Ann, whose edge weights are \(0.707\) (Ann, Titanic), \(0.5\) (Titanic, Bob), \(0.5\) (Bob, Vendetta), \(0.5\) (Vendetta, Cara) and \(0.707\) (Cara, Matrix); the product is \(\boxed{0.0625}\). (c) Layer \(7\) is an odd layer, so Ann’s vector is built from the films’ starts only (§5, §11): it contains the starts of Titanic, Vendetta and Matrix (each is reachable by a walk of \(7\) steps, since Matrix is \(5\) hops away and the walk can turn back once), and none of the three users’ starts. In words: a start enters at its hop distance and then at every second layer after it, and the parity of the layer decides whether users or films appear; Matrix is five hops from Ann, so it reaches her late and faintly, with a share of about six percent at layer \(5\).

E5. The cosine of two arrows is their dot product over the product of their lengths (§8).

Bob’s layer 2 and Cara’s layer 3: \([1.457,0.668]\cdot[0.75,1.561]=1.093+1.043=2.136\); the lengths are \(\sqrt{1.457^2+0.668^2}=1.603\) and \(\sqrt{0.75^2+1.561^2}=1.732\); so \(\cos=\tfrac{2.136}{1.603\times1.732}=\boxed{0.769}\), an angle of \(\boxed{39.7^\circ}\).

Bob’s layer 3 and Cara’s layer 2: \([1.375,0.729]\cdot[1,1.625]=1.375+1.185=2.560\); the lengths are \(\sqrt{1.375^2+0.729^2}=1.556\) and \(\sqrt{1+1.625^2}=1.908\); so \(\cos=\tfrac{2.560}{1.556\times1.908}=\boxed{0.862}\), an angle of \(\boxed{30.5^\circ}\).

The second pair points more nearly the same way. In words: even and odd layers are mixtures of different starts (§5), so a cosine across layers compares two different mixtures, and the answer depends on which layers are paired. LightGCN avoids the problem by combining the layers first (§12).

E6. With Cara’s start \([-1,1.5]\), Titanic’s layer-\(1\) arrow is unchanged, \([1.914,-0.414]\), and Matrix’s is \(0.707\,[-1,1.5]=[-0.707,\,1.061]\).

  1. The dot product is \(1.914\times(-0.707)+(-0.414)\times1.061=-1.353-0.439=-1.792\), the lengths are \(1.959\) and \(1.275\), so \(\cos=\tfrac{-1.792}{1.959\times1.275}=\boxed{-0.718}\), an angle of \(135.9^\circ\).

  2. Titanic’s layer-\(1\) arrow is \(0.707\,\mathbf e_A^{(0)}+0.5\,\mathbf e_B^{(0)}\), a mixture of Ann’s and Bob’s starts; Matrix’s is \(0.707\,\mathbf e_C^{(0)}\), Cara’s start alone. The two mixtures share no starting arrow.

  3. No. Sharing nothing does not make the arrows point at a right angle: the cosine is \(-0.718\) here, and with the starts of §8 it would be \(0.366\) (\([0.707,1.061]\) for Matrix). Which way the arrows point depends on the starting arrows, and those are our guess. In words: the graph links the two films by no path shorter than four hops, so any cosine between them, negative or positive, comes from the starts.

E7. From Titanic, a step to Ann weighs \(0.707\) and a step to Bob \(0.5\); from Bob to Vendetta it weighs \(0.5\) and from Vendetta to Cara \(0.5\) (§7).

  1. Cara is \(3\) hops from Titanic (Titanic, Bob, Vendetta, Cara), so her start cannot be inside Titanic’s vector at layer \(1\) or \(2\). It first enters at layer \(\boxed{3}\), along the only walk of three steps between them, with share \(0.5\times0.5\times0.5=\boxed{0.125}\), one factor per edge.

  2. At layer \(2\) Titanic’s vector is built from the films’ starts. Walks of two steps: Titanic, Ann, Titanic with weight \(0.707\times0.707=0.5\) and Titanic, Bob, Titanic with \(0.5\times0.5=0.25\), which together give Titanic’s own start a share of \(\boxed{0.75}\); and Titanic, Bob, Vendetta with \(0.5\times0.5=\boxed{0.25}\) for Vendetta’s start. No other start enters. In words: a start enters at its hop distance, and a film’s own start returns at layer \(2\) by turning back along either of its two edges.

E8. Step 1 (parameters) and step 2 (layers) are given. Step 3, combine: each node is \(\tfrac12\big(\mathbf e^{(0)}+\mathbf e^{(1)}\big)\), \[ \begin{aligned} \mathbf e_B&=\tfrac12\big([1,2]+[1.5,0.5]\big)=[1.25,\,1.25],\\ \mathbf e_T&=\tfrac12\big([2,0]+[1.914,-0.414]\big)=[1.957,\,-0.207],\\ \mathbf e_V&=\tfrac12\big([1,1]+[1,1.75]\big)=[1,\,1.375],\\ \mathbf e_M&=\tfrac12\big([0,2]+[0.707,1.061]\big)=[0.354,\,1.530]. \end{aligned} \] Step 4, score: \(\hat y_{B,T}=1.25\times1.957+1.25\times(-0.207)=\boxed{2.188}\); \(\hat y_{B,V}=1.25\times1+1.25\times1.375=\boxed{2.969}\); \(\hat y_{B,M}=1.25\times0.354+1.25\times1.530=\boxed{2.355}\). The order is Vendetta, Matrix, Titanic. Bob has watched Titanic and Vendetta, so the film to recommend is \(\boxed{\text{Matrix}}\). In words: Bob’s score for a film he already liked is high (Vendetta first), and among the films he has not watched the only candidate is Matrix.

E9. Under the symmetric rule the weight of an edge is \(1/(\sqrt{\deg U}\sqrt{\deg v})\), and \(U\)’s own factor is the same for both films, so the ratio of the two weights is \(\sqrt{\deg P}/\sqrt{\deg Q}=\sqrt{\deg P}\). For \(Q\) to pull \(50\) times harder, \(\sqrt{\deg P}=50\), so \(\deg P=\boxed{2500}\) watchers. The weights are \[ w_{U\leftarrow P}=\frac{1}{\sqrt2\,\sqrt{2500}}=\boxed{0.0141},\qquad w_{U\leftarrow Q}=\frac{1}{\sqrt2\,\sqrt{1}}=\boxed{0.707}, \] and \(0.707/0.0141\approx50\) (exactly \(\sqrt{2500}\)). In words: the pull falls with the square root of the number of watchers, so a film must have \(2500\) watchers, not \(50\), to be discounted \(50\) times (§13, Effect 1).

E10. In the vanilla GCN every node gets a self-loop (§14), so Bob has degree \(2+1=3\), and so do Titanic and Vendetta. Bob’s self-weight and the weights on Titanic and Vendetta are all \(\tfrac{1}{\sqrt3\sqrt3}=\boxed{\tfrac13}\): \[ \tfrac13\big([1,2]+[2,0]+[1,1]\big)=[1.333,\,1]. \] Times \(W\): \([1.333,\,1]\left[\begin{smallmatrix}2&-1\\0&1\end{smallmatrix}\right]=[2.667,\;-1.333+1]=[2.667,\,-0.333]\). After the ReLU: \(\boxed{[2.667,\,0]}\).

  1. On a film graph I would not make the swap. Take the three starts times \(W\): Bob \([1,2]W=[2,1]\), Titanic \([2,0]W=[4,-2]\), Vendetta \([1,1]W=[2,0]\). A layer with no \(W\) and these rows gives \(\tfrac13([2,1]+[4,-2]+[2,0])=[2.667,-0.333]\), exactly the pre-ReLU result above. The graph has no given features (§6): the starts are free parameters, so training could have stored these rows, and \(W\) adds numbers to tune, not capacity (§14: \(W\) folds into the starts). The ReLU also just wiped out the \(-0.333\), and on a film graph a negative number is a dislike. The ablation of He et al. points the same way: removing \(W\) raised Recall@20 for NGCF from \(0.1547\) to \(0.1686\) (§14, reported there, not tested here).

  2. With genre flags and a release year per film I would keep \(W\) for those features. They are given, not trained, so \(W\) is the only way to reshape them into useful numbers, and a bend after it can help to read such measured features (§6, §14); the ID part of each start still needs no \(W\).

In words: more parameters are not more power when they only rename a vector the model already learns, but they are needed when the input is a given feature.

E11. The four layers add to \([1+1.5+1.457+1.375,\;2+0.5+0.668+0.729]=[5.332,\,3.897]\), so \[ \mathbf e_B=\tfrac14\,[5.332,\,3.897]=\boxed{[1.333,\,0.974]}. \] Against his start \([1,2]\) (length \(2.236\)): the dot product is \(1.333+1.948=3.281\) and the combined vector has length \(1.651\), so \(\cos=\tfrac{3.281}{1.651\times2.236}=\boxed{0.889}\), an angle of \(27.3^\circ\). His pure layer-\(3\) arrow \([1.375,0.729]\) gives \(\tfrac{1.375+1.458}{1.556\times2.236}=\boxed{0.814}\), an angle of \(35.5^\circ\). Without the \(k=0\) term the other three layers average to \([1.444,\,0.632]\), whose cosine with his start is \(\boxed{0.768}\) (an angle of \(39.8^\circ\)): worse than the last layer alone. In words: the \(k=0\) term, his untouched start \([1,2]\), is what keeps the combination closer to his own taste (\(0.889\)) than the last layer alone (\(0.814\)).

E12. (a) \(\hat y_{C,M}=0.833\times0.354+1.680\times1.471=\boxed{2.766}\) and \(\hat y_{C,T}=0.833\times1.888+1.680\times(-0.055)=\boxed{1.480}\) (from unrounded vectors, \(1.481\)). The gap is \(2.766-1.480=1.286\approx\boxed{1.29}\) (from unrounded vectors, \(1.285\)).

  1. \(\sigma(1.285)=\tfrac{1}{1+e^{-1.285}}=\boxed{0.783}\), and the loss is \(-\ln0.783=\boxed{0.244}\).

  2. With the scores the other way round the gap is \(-1.285\), \(\sigma=0.217\) and the loss is \(-\ln0.217=\boxed{1.529}\).

  3. The squared lengths of the three users’ starts are \(8\) (Ann), \(5\) (Bob) and \(3.25\) (Cara), which add to \(16.25\); the penalty is \(0.05\times16.25=\boxed{0.8125}\).

In words: the right order costs \(0.244\) and the wrong one \(1.529\), six times more, and the gap of only \(1.29\) leaves a small loss that training would still push down.

E13. (a) There are \(50{,}000+10{,}000=60{,}000\) nodes, so the dense table has \(60{,}000^2=\boxed{3.6\times10^{9}}\) entries (about \(14.4\) GB at \(4\) bytes each). (b) The edges number \(50{,}000\times30=1.5\times10^{6}\), so the table holds \(2\times1.5\times10^{6}=\boxed{3\times10^{6}}\) ones, and a list of them is \(\tfrac{3.6\times10^{9}}{3\times10^{6}}=\boxed{1{,}200}\) times shorter. (c) \(2\times10^{6}\times64\times4\) bytes \(=512\times10^{6}\) bytes \(=\boxed{512\text{ MB}}\). With \(32\) numbers per vector it would be \(2\times10^{6}\times32\times4=256\times10^{6}\) bytes, \(\boxed{256\text{ MB}}\): the size grows with \(d\) and with the number of nodes, not with the number of edges (§15). In words: the sparse list is what makes a large graph fit in memory, and serving costs the same as matrix factorization.

E14. (a) Each liked pair gives two \(1\)s (E1), so \(A\) holds \(2\times2\times10^{6}=\boxed{4\times10^{6}}\) ones. (b) One layer costs \(4\times10^{6}\times64=\boxed{2.56\times10^{8}}\) multiplications, and \(K=3\) layers cost \(3\times2.56\times10^{8}=\boxed{7.68\times10^{8}}\). (c) \(K=6\) costs \(1.536\times10^{9}\), twice as much, because every layer repeats the same sparse multiplication, so the cost grows in step with \(K\) (§15, the table’s cost column).

In words: the cost grows with the number of layers and the number of liked pairs, not with the number of users or films.

Self-Supervised & Contrastive Learning

Throughout these solutions \(\log\) is the natural logarithm, \(\ln\) (the calculator’s “ln” key, not its base-10 “log” key). Check: \(-\ln 0.731=0.31\), whereas \(-\log_{10}0.731=0.136\).

E1. InfoNCE at a milder temperature (§3.2). With \(\tau=0.4\) the three similarities \(0.8,\,0.6,\,-0.6\) give \(\operatorname{sim}/\tau=2,\,1.5,\,-1.5\), so \(\exp(\operatorname{sim}/\tau)=e^{2}=7.389,\ e^{1.5}=4.482,\ e^{-1.5}=0.223\). The denominator is \(7.389+4.482+0.223\approx\boxed{12.09}\), the positive’s softmax share is \(7.389/12.09=0.611\), and \[\mathcal{L}_{\text{cl},i}=-\log(0.611)=\boxed{0.49}.\] The near negative holds \(4.482/(4.482+0.223)=\boxed{0.953}\) of the two negatives’ total (at \(\tau=0.2\) it held \(0.9975\)). The loss is larger than the chapter’s \(0.31\) at \(\tau=0.2\) and the near negative’s hold on the negatives is weaker. A bigger \(\tau\) flattens the softmax: it divides every similarity by a larger number, so the gap between the positive (\(0.8\)) and the close negative (\(0.6\)) shrinks from \(4.0-3.0=1.0\) to \(2.0-1.5=0.5\) before exponentiating. The positive keeps a smaller share (\(0.611\) against \(0.731\)), so the loss rises.

In words: a milder \(\tau\) makes the model less sure which vector is the positive, and the loss measures that doubt. (A smaller \(\tau\) sharpens the contrast and lowers the loss when the positive is already the closest; E3 shows the danger when it is not.)

E2. Healthy against collapsed (§3.3). In the healthy batch the positive’s similarity is \(1\) and the three negatives’ similarities are \(0\), so at \(\tau=0.2\) the exponentials are \(e^{1/0.2}=e^{5}=148.41\) for the positive and \(e^{0}=1\) for each negative. The denominator is \(148.41+3=\boxed{151.41}\), the positive’s share is \(148.41/151.41=0.980\), and \[\mathcal{L}_{\text{cl},i}=-\log(0.980)=\boxed{0.020}.\] The collapse value for \(N=4\) is \(\log 4=\boxed{1.386}\): collapse makes every similarity \(1\), each of the \(N\) shares \(1/N\) and the loss \(-\log(1/N)=\log N\), whatever \(\tau\) is. The healthy loss is \(1.386/0.020\approx\boxed{69}\) times smaller.

For the second site, \(\log 64=\boxed{4.16}\). The collapse value exceeds \(7\) when \(\log N>7\), that is \(N>e^{7}=1096.6\), so the smallest batch is \(N=\boxed{1097}\) nodes: the collapse value grows only with the logarithm of the batch, so it takes a very large batch to push it past \(7\).

In words: pulling pairs together is not enough, because everything on one point is a perfect pull, and InfoNCE sitting near \(\log N\) is the sign that the spread has been lost.

E3. A too-small \(\tau\) is brittle (§3.3). With the positive at \(0.8\) and one rival at \(0.9\), the node’s loss is \(-\log\!\dfrac{e^{0.8/\tau}}{e^{0.8/\tau}+e^{0.9/\tau}}\). At \(\tau=0.12\): \(e^{6.667}=785.77\) and \(e^{7.5}=1808.04\), so the loss is \(-\log\!\dfrac{785.77}{785.77+1808.04}=-\log(0.303)=\boxed{1.19}\).

The rival holds the share \(1808.04/2593.81=\boxed{0.697}\), so the positive holds only \(0.303\). For the second part, a two-term softmax with gap \(0.1\) has loss \(\ln(1+e^{0.1/\tau})\). Setting it to \(1.5\) gives \(e^{0.1/\tau}=e^{1.5}-1=3.482\), so \(0.1/\tau=\ln3.482=1.2475\) and \(\tau=\boxed{0.080}\) (check: \(\ln(1+e^{1.25})=1.50\)). Shrinking \(\tau\) from \(0.12\) to \(0.08\) lifts the loss from \(1.19\) to \(1.5\): the softmax sharpens onto the single closest pair, here the rival, so one wrong pair comes to dominate.

In words: the same dial that helps when the positive is closest hurts when a rival is closer; smaller is not always better.

E4. Counting a batch (§3.1, §3.2). (a) Dan keeps \(4-2=\boxed{2}\) edges in view 1 and \(4-1=\boxed{3}\) edges in view 2. (b) Dan’s denominator runs over the second view of every user in the batch, so it has \(\boxed{64}\) terms: one positive and \(\boxed{63}\) negatives. Titanic’s runs over the 32 films, so it has \(\boxed{32}\) terms: one positive and \(\boxed{31}\) negatives. (c) Each of the 64 users has 63 negatives, \(64\cdot63=4032\) pairs, and each of the 32 films has 31, \(32\cdot31=992\) pairs, so the step has \(4032+992=\boxed{5024}\) pairs.

In words: the user sum and the film sum are counted separately, and each node’s denominator holds one copy of itself and the strangers of its own kind.

E5. Alignment numerically (§3.3). Starting from §3.2 (positive \(0.8\), negatives \(0.6\) and \(-0.6\), \(\tau=0.2\), loss \(0.31\)), push the positive to \(1.0\) with the negatives unchanged. The exponentials become \(e^{1.0/0.2}=e^{5}=148.41\) for the positive and, unchanged, \(e^{3}=20.09\) and \(e^{-3}=0.05\) for the negatives, so the denominator is \(148.41+20.09+0.05=168.55\). The positive’s share rises to \(148.41/168.55=0.881\) (from \(0.731\)), and \[\mathcal{L}_{\text{cl},i}=-\log(0.881)=\boxed{0.13},\] down from \(0.31\). Better alignment shrinks the loss, and a positive at similarity \(1\) with a share near \(1\) would take it toward \(0\).

In words: the loss falling is how a better-aligned pair shows up as a number. The other half of the objective works the other way round: pushing the close negative from \(0.6\) to \(0.0\) with the positive fixed at \(0.8\) would raise the share to \(0.981\) and cut the loss to about \(0.02\). Either half lowers the loss.

E6. Where \(\mathcal{L}_{\text{cl}}\) runs (§3.4). (a) In ID space \([1,2]\cdot[1,1.5]=1+3=4\), the lengths are \(2.236\) and \(1.803\), so the cosine is \(4/4.031=\boxed{0.992}\). In the layer-combined space \([1.319,1.056]\cdot[0.833,1.68]=1.099+1.774=2.873\), the lengths are \(1.690\) and \(1.875\), so the cosine is \(2.873/3.169=\boxed{0.907}\).

  1. The ID space: Cara looks almost identical to Bob there (\(0.992\)), a very hard negative, so the beginner’s loss reacts strongly to a pair whose raw lookup vectors, the vectors before any neighbor-averaging, are almost parallel. In §3.4’s space the same pair is less alike (\(0.907\)) because the vectors have already averaged in different neighbors. (c) The angles are \(\arccos0.992=\boxed{7.1^\circ}\) in the ID space and \(\arccos0.907=\boxed{24.9^\circ}\) in the layer-combined space, so the combined space puts the pair \(24.9-7.1=\boxed{17.8^\circ}\) farther apart. The beginner’s loss reacts to the smaller, ID-space angle, so it constrains the raw lookup vectors themselves, which is not the geometry BPR ranks with: BPR scores the layer-combined vectors, so the two losses pull on different quantities, even though both gradients end in the one ID table.

In words: contrast the vectors you rank with, because averaging over neighbors changes who looks like whom.

E7. LightGCL’s factored cost (§5). The dense layer costs \(\text{users}\cdot\text{items}\cdot d=1000\cdot1000\cdot10=\boxed{1.0\times10^{7}}\) multiply-adds; the factored layer \(U_q(\Sigma_q(V_q^\top E_{\text{items}}))\) costs \((\text{users}+\text{items})\,q\,d=(1000+1000)\cdot5\cdot10=\boxed{1.0\times10^{5}}\). The ratio is \(\dfrac{10^{7}}{10^{5}}=\boxed{100\times}\).

With \(\text{users}=\text{items}=n\) the ratio is \(\dfrac{n^{2}d}{2nqd}=\dfrac{n}{2q}\), independent of \(d\); here \(\dfrac{1000}{2\cdot5}=100\). For §5’s example, \(n=10^{5}\) and \(q=5\), it gives \(10^{5}/10=10{,}000\times\), the saving §5 reports. In words: the dense layer grows with the square of the site and the factored one only with its size, so the bigger the site, the bigger the saving.

E8. Reading two training logs (§3.3, §6.2, §6.4). (a) The collapse value for \(N=128\) is \(\log 128=\boxed{4.85}\). The loss sits at \(4.86\) and then \(4.84\), on top of it, and Recall@20 does not move, so collapse is the first suspect (a collapsed encoder gives exactly this loss, though the converse does not follow: a very large \(\tau\), an encoder that is barely trained, or a bug can pin the loss near \(\log 128\) too; measuring uniformity, which scores a collapsed space worst, would tell them apart). If it is collapse, every node sits on nearly one vector and each softmax share is about \(1/128\). A very large \(\tau\) gives the same loss without any collapse.

  1. A loss of \(0.50\) means the share is \(e^{-0.50}=\boxed{0.607}\), about \(61\%\).

  2. At \(\gamma=2\) the losses are \((1-0.9)^2=\boxed{0.01}\) and \((1-0.5)^2=\boxed{0.25}\), so the second is \(\boxed{25}\) times the first; at \(\gamma=1\) they are \(0.10\) and \(0.50\), only \(\boxed{5}\) times. A larger \(\gamma\) makes the loss tiny for nodes already rebuilt well and so leaves the work to the badly rebuilt ones.

In words: a contrastive loss that sits at \(\log N\) is a warning light, and a scaled cosine error with a larger \(\gamma\) spends its effort on the nodes it rebuilds worst.

E9. RLMRec (§7). (a) Con with similarities \(0.4,\ 0.6,\ -0.6\) at \(\tau=0.2\): \(e^{2}=7.389\), \(e^{3}=20.086\), \(e^{-3}=0.050\); total \(27.524\); the positive’s share is \(7.389/27.524=0.268\) and the loss is (\(1.315\) to three places). It lies between §7’s \(0.31\) (profile similarity \(0.8\)) and \(3.05\) (\(0.0\)). A profile that half agrees with the clicks costs a middling loss, and here another user’s profile (\(0.6\)) sits closer than her own (\(0.4\)), the rival-closer-than-positive case of §3.3.

  1. Gen: the squared errors are \((1-0.8)^2=0.04\) and \((0-0.6)^2=0.36\), so the sum is \(0.40\) and \(\text{MSE}=\boxed{0.20}\). Both vectors have length 1, so \(\cos=0.8\) and \(1-\cos=\boxed{0.20}\). No other user enters this plain reconstruction error. It is the chapter’s toy, not RLMRec-Gen’s loss: Con (the paper’s Eq. 16) and Gen (Eq. 17) both put a cosine inside the InfoNCE-form ratio of its Eq. 18 (§7), where Gen hides the node’s input, maps the result up through its MLP, and compares by cosine.

  2. Con’s new denominator adds \(e^{0.6/0.2}=e^{3}=20.086\) to (a)’s \(27.524\), so it is \(47.610\), the positive’s share falls from \(0.268\) to \(7.389/47.610=0.155\) and the loss is \(-\log0.155=\boxed{1.86}\), up from \(1.32\). Prediction: it rises, because every added negative takes share from the positive and this one is as close as the nearest rival. The plain error of (b) does not change: it compares the rebuilt vector only with the user’s own profile, so nobody else enters. The paper’s own Gen is different: its cosine sits inside Eq. 18’s ratio, so a new user’s profile would enter its denominator too (the paper does not say how the sum is formed, so this chapter does not compute it). In words: more rivals can only raise a ratio loss; a plain reconstruction error has no rivals.

E10. The weight \(\lambda\) (§3.4). (a) The total is \(0.20+\lambda\cdot0.50\). At \(\lambda=0.1\) it is \(\boxed{0.25}\) and the SSL term, \(0.05\), is \(\boxed{20\%}\) of it. At \(\lambda=1\) the total is \(\boxed{0.70}\) and the SSL term, \(0.50\), is \(\boxed{71\%}\). At \(\lambda=10\) the total is \(\boxed{5.20}\) and the SSL term, \(5.00\), is \(\boxed{96\%}\) of it.

  1. The total is measured in units that change with \(\lambda\): at \(\lambda=10\) it is mostly the contrastive term, so its size says nothing about ranking quality, and totals at two different \(\lambda\) cannot be compared. Compare the BPR loss and the contrastive loss on their own, and Recall@20 on held-out data.

  2. On the second batch the total is \(0.15+0.60\lambda\), which reaches \(0.45\) when \(\lambda=0.30/0.60=\boxed{0.5}\); the SSL term is then \(0.30\), a share of \(0.30/0.45=\boxed{67\%}\). For a share \(s\) of the total the condition is \(\lambda\mathcal{L}_{\text{cl}}=\dfrac{s}{1-s}\mathcal{L}_{\text{BPR}}\), so at \(s=0.9\) it is \(\lambda=9\cdot0.15/0.60=\boxed{2.25}\). At \(\lambda=0.1\) the SSL term is \(0.06\) of a total \(0.21\), a share of \(\boxed{29\%}\), below half. In words: each share \(s\) needs \(\lambda\) equal to the ratio of the two losses times \(s/(1-s)\), so where the line falls depends on the two losses of your own batch.

E11. DirectAU’s alignment (§3.3). The unit vectors are Ann \((0.707,-0.707)\), Bob \((0.447,0.894)\), Cara \((0.555,0.832)\), Titanic \((1,0)\), Vendetta \((0.707,0.707)\) and Matrix \((0,1)\). The squared distances are Ann–Titanic \(\boxed{0.586}\), Bob–Titanic \(\boxed{1.106}\), Bob–Vendetta \(\boxed{0.103}\), Cara–Vendetta \(\boxed{0.039}\) and Cara–Matrix \(\boxed{0.336}\); each equals \(2-2\cos\) for unit vectors. Their sum is \(2.169\) and their mean is \(2.169/5=\boxed{0.434}\).

The worst pair is Bob–Titanic: Bob’s taste leans toward action while Titanic points along romance only, so the pull between them is the largest. Ann and Matrix never interacted; their squared distance is \(\boxed{3.414}\), but the alignment term averages only pairs that interacted, so it does not enter. In words: the squared distance of two unit vectors runs from \(0\) to \(4\), so \(0.434\) means the interacting pairs are fairly close, and Bob and Titanic are the pair the loss would pull hardest.

E12. Should you add SSL? (§4, §1). (a) The filled share is \(250{,}000/(50{,}000\cdot10{,}000)=250{,}000/(5\times10^{8})=\boxed{0.05\%}\). ML-20M is \(0.53\%\) filled, so this site is \(0.53/0.05\approx\boxed{10.6}\) times emptier. The average user has clicked \(250{,}000/50{,}000=\boxed{5}\) films.

  1. Plain LightGCN uses 1 pass per step, so an epoch takes \(400\cdot1\cdot0.5=\boxed{200}\) s. §4’s box gives SimGCL 3 passes: \(400\cdot3\cdot0.5=\boxed{600}\) s; XSimGCL 1 pass: \(\boxed{200}\) s; LightGCL 2 passes: \(\boxed{400}\) s. (SGL, with 3 passes, equals SimGCL.)

  2. Plain LightGCN (\(200\) s) and XSimGCL (\(200\) s) fit the \(300\) s limit, but plain LightGCN has no SSL term, so of the three SSL methods only XSimGCL fits. LightGCL at \(400\) s and SimGCL at \(600\) s do not. Try XSimGCL first: it is the box’s own fallback “if three passes per step are what hurt”, and here they do. This disagrees with the box’s default, SimGCL, which the budget rules out. The choice saves \(600-200=\boxed{400}\) s per epoch against SimGCL, two thirds of SimGCL’s \(600\) s (XSimGCL takes the other third). In words: the default is where you start when passes are free, and here they are not.

E13. A severed edge (§4). (a) With Cara–Matrix dropped, Matrix has degree \(0\), so its layers 1 to 3 are exactly zero and the combined view is \[\tfrac14\,[0,2]=\boxed{[0,\,0.5]}.\] The cosine with the main vector \([0.442,1.391]\) is \(1.391\big/\sqrt{0.442^2+1.391^2}=1.391/1.459=\boxed{0.953}\), an angle of \(17.6^\circ\).

  1. At \(K=2\) the view is \(\tfrac13[0,2]=[0,0.667]\) and the cosine with \([0.354,1.471]\) is \(1.471/1.513=\boxed{0.972}\) (\(13.5^\circ\)).

  2. The two views agree better at \(K=2\). What explains it is how far the main vector is pulled away from the ID direction \([0,1]\) by layers 1 to \(K\): \(\arctan(0.354/1.471)=13.5^\circ\) at \(K=2\) (layers 1 and 2 carry \(\tfrac23\) of the weight) and \(\arctan(0.442/1.391)=17.6^\circ\) at \(K=3\) (they carry \(\tfrac34\)). In words: in these two cases, the more layers a combination averages, the farther the main vector drifts from a severed node’s ID direction, though here the cosine stays high at both settings. The dropped view is only the ID vector scaled, \([0,\,c]\), and cosine ignores scale, so the weight on the ID vector itself does not enter the cosine.

E14. Recall@K (§7). (a) Hits among the top 10: ranks 1 and 4, so \(2/6=\boxed{0.333}\). Hits among the top 20: ranks 1, 4 and 12, so \(3/6=\boxed{0.500}\). (b) Recall@20 on LightGCN and Amazon-book is \(0.1411\) for the base and \(0.1446\) with RLMRec-Gen. The difference is \(0.0035\), so about \(\boxed{3.5}\) more hidden films per 1,000 hidden films.

  1. With 4 hidden films: the top 10 holds ranks 3 and 9, so Recall@10 is \(2/4=\boxed{0.5}\); the top 20 adds rank 14, so Recall@20 is \(3/4=\boxed{0.75}\); the top 30 adds rank 25, so Recall@30 is \(4/4=\boxed{1.0}\). Recall first reaches \(0.75\) at \(K=\boxed{14}\), the rank of her third hit. In words: Recall@\(K\) is the share of the films she wanted that made the list, so it steps up at each hit’s rank and is flat between hits.

E15. Identical profiles (§7, §3.3). (a) All 5 similarities are \(1\), so every exponential is \(e^{1/0.2}=e^5=148.41\), the denominator is \(5\times148.41=742.07\), the positive’s share is \(1/5=0.2\) and the loss is \(-\ln0.2=\boxed{1.61}\) (\(\ln5\)), the collapse value for 5 users. (b) The positive gives \(148.41\) and each of the four others \(e^{-5}=0.0067\), so the denominator is \(148.41+0.027=148.44\), the share is \(0.99982\) and the loss is \(\boxed{0.0002}\). (c) The classmate is wrong: identical profiles give \(\ln5\), no better than a model that cannot tell the users apart, and profiles pushed away from \(\mathbf e\) give almost zero. The loss asks the other users’ profiles to be unlike her embedding, with only her own profile close to it; that is why a loss that rewards agreement still needs the rivals to disagree.

The Spectral / Graph-Filter View

E1. The second site has the table \(R\) (rows X, Y, Z; columns F1, F2, F3) and its Gram matrix: \[ R=\begin{bmatrix}1&1&0\\1&1&0\\0&1&1\end{bmatrix},\qquad G=R^{\top}R=\begin{bmatrix}2&2&0\\2&3&1\\0&1&1\end{bmatrix}. \] Entry \(G_{12}=2\): X and Y both liked F1 and F2. \(G_{23}=1\): only Z liked F2 and F3. \(G_{13}=0\). The diagonal (\(2,3,1\)) is each film’s popularity. Zero the diagonal and read the degrees from the row sums: \[ A=\begin{bmatrix}0&2&0\\2&0&1\\0&1&0\end{bmatrix},\qquad (d_1,d_2,d_3)=(2,3,1). \] Each edge is divided by \(\sqrt{d_id_j}\): \(\tilde A_{12}=2/\sqrt{2\cdot3}=\boxed{0.816}\) and \(\tilde A_{23}=1/\sqrt{3\cdot1}=\boxed{0.577}\), so \[ \tilde A=\begin{bmatrix}0&0.816&0\\0.816&0&0.577\\0&0.577&0\end{bmatrix}. \] The hub is F2, with the largest degree \(3\). In words: a pair that many users share gets a heavier edge, and the shape is again a path.

E2. Apply \(\tilde A\) to \(\mathbf v_{-1}=[0.577,-0.707,0.408]\) row by row: \[ \tilde A\mathbf v_{-1}=\begin{bmatrix}0.816\,(-0.707)\\0.816\,(0.577)+0.577\,(0.408)\\0.577\,(-0.707)\end{bmatrix} =\begin{bmatrix}-0.577\\0.707\\-0.408\end{bmatrix}=(-1)\,\mathbf v_{-1}. \] Middle row: \(0.471+0.2355=0.707\). So \(\mathbf v_{-1}\) is an eigenvector with \(\boxed{\lambda=-1}\). Its signs run \(+,-,+\) along F1 – F2 – F3: the sign flips across every edge, the most jagged pattern the path allows, so it is the highest frequency. In words: averaging neighbors cancels a pattern that alternates, which is why the stretch factor is negative and of full size.

E3. (a) One hop: \(\tilde A\mathbf r=[0,\;0.577,\;0]\). Two hops: \(\tilde A^2\mathbf r=\tilde A[0,0.577,0]=[0.816\times0.577,\;0,\;0.577\times0.577]=[0.471,\;0,\;0.333]\).

  1. \(H\mathbf r=[0.471,\;0.577,\;0.333]\). F3 is W’s own click, so the unseen films are F2 with \(\boxed{0.577}\) (first) and F1 with \(\boxed{0.471}\) (second).

  2. The one-hop term \([0,0.577,0]\) gives F1 the score \(\boxed{0}\) (only F2 is lit). The two-hop term \([0.471,0,0.333]\) reaches F1, with \(0.816\times0.577=\boxed{0.471}\) (and leaves F2 at \(0\)). In words: F1 and F3 never co-occur, yet the two-hop chain F3 – F2 – F1 carries the click to F1, so the filter reaches past the data along the graph.

E4. (a) The coordinates are dot products with the three patterns of the exercise intro: \[ c_{+1}=\mathbf v_{+1}\cdot\mathbf r=0.408,\qquad c_{0}=\mathbf v_{0}\cdot\mathbf r=-0.816,\qquad c_{-1}=\mathbf v_{-1}\cdot\mathbf r=0.408. \] (b) Rebuild \(\sum_kc_k\mathbf v_k\): \[ \begin{aligned} &0.408\,[0.577,0.707,0.408]-0.816\,[0.577,0,-0.816]+0.408\,[0.577,-0.707,0.408]\\ &\quad=[0.236,0.289,0.167]+[-0.471,0,0.666]+[0.236,-0.289,0.167]=\boxed{[0,0,1]}=\mathbf r \end{aligned} \] to rounding. The largest in size is the mid coordinate, \(|-0.816|\). In words: a click on the last film is mostly the “ends opposite” pattern, not the smooth one.

E5. (a) \(h(+1)=\big(\tfrac22\big)^2=\boxed{1}\), \(h(0)=\big(\tfrac12\big)^2=\boxed{0.25}\), \(h(-1)=0\).

  1. Scale the coordinates: \((1\cdot0.408,\ 0.25\cdot(-0.816),\ 0\cdot0.408)=(0.408,\ -0.204,\ 0)\), and rebuild: \[ \begin{aligned}\hat{\mathbf r}&=0.408\,\mathbf v_{+1}-0.204\,\mathbf v_{0}\\ &=[0.236,0.289,0.167]+[-0.118,0,0.167]=\boxed{[0.118,\,0.289,\,0.333]}.\end{aligned} \] As a cross-check, \(\big(\tfrac{I+\tilde A}{2}\big)^2\mathbf r\) gives the same vector.

  2. Of the unseen films, F2 (\(0.289\)) ranks first and F1 (\(0.118\)) second, the same order as Exercise 3’s. In words: only the ordering is shared; the sizes differ, because this filter keeps a quarter of the mid pattern, whose negative part at F1 cancels half of the smooth part there (\(0.236\to0.118\)).

E6. (a) \(0.9^{1}=0.9\), \(0.9^{2}=0.81\), \(0.9^{4}=0.656\), \(0.9^{8}=0.430\), \(0.9^{16}=0.185\), \(0.9^{32}=0.034\).

  1. \(0.9^{21}=0.109\) is still above \(0.1\) and \(0.9^{22}=0.098\) is below it, so the answer is \(\boxed{j=22}\) hops.

  2. The weight is \((-0.95)^{j}\). At \(j=10\) it is \(+0.599\) (even power, sign kept) and at \(j=11\) it is \(-0.569\) (odd power, sign flipped). The size \(0.95^{j}\) is \(0.513\) at \(j=13\), still above \(0.5\), and \(0.488\) at \(j=14\), so the answer is \(\boxed{j=14}\) hops. In words: a mode just inside \(-1\) shrinks slowly and flips sign at every hop.

E7. (a) \(0.577^2+0.707^2+0.408^2=0.333+0.500+0.167=1.000\), so \(\mathbf v\) has length \(1\).

  1. \(P\mathbf x=(\mathbf v\cdot\mathbf x)\,\mathbf v\) with \(\mathbf v\cdot\mathbf x=0.577+0.707=1.284\), so \(P\mathbf x=1.284\,[0.577,0.707,0.408]=\boxed{[0.741,\,0.908,\,0.524]}\).

  2. \(\mathbf v\cdot\mathbf v_{-1}=0.333-0.500+0.167=0\): the patterns are perpendicular, so \(P\mathbf v_{-1}=0\cdot\mathbf v=\boxed{\mathbf 0}\).

  3. \(\operatorname{trace}P=\sum_iv_i^2=\lVert\mathbf v\rVert^2=\boxed{1}\). Also \(P^{2}=\mathbf v(\mathbf v^{\top}\mathbf v)\mathbf v^{\top}=\boxed{P}\), because \(\mathbf v^{\top}\mathbf v=1\): projecting twice changes nothing more than projecting once.

E8. (a) For \(n=10{,}000\): \(n^3=\boxed{10^{12}}\) flops, which at \(10^{10}\) per second is \(\boxed{100}\) s. The dense matrix has \(n^2\times8=10^{8}\times8=8\times10^{8}\) bytes \(=\boxed{0.8}\) GB.

  1. For \(n=200{,}000\): \(n^3=8\times10^{15}\) flops, which is \(8\times10^{5}\) s \(=\boxed{9.3}\) days, and \(n^2\times8=3.2\times10^{11}\) bytes \(=\boxed{320}\) GB. Doubling \(n\) costs eight times the time, so twenty times the films costs \(8000\) times.

  2. Dense: \(n^3=1.5^3\times10^{15}=3.375\times10^{15}\) flops, which is \(3.4\times10^{5}\) s, about \(\boxed{3.9}\) days, and \(n^2\times8=180\) GB of memory. Polynomial, \(K=2\): \(2\cdot3\times10^{6}=6\times10^{6}\) flops, \(\boxed{6\times10^{-4}}\) s. Run the polynomial filter (about \(5.6\times10^{8}\) times fewer operations). In words: “spectral is fast” only with a low-order polynomial in \(\tilde A\), or a truncated top-\(q\) decomposition of a sparse \(\tilde A\), never a full dense eigendecomposition.

E9. Entry \((i,j)\) of \(\mathbf v_k\mathbf v_k^{\top}\) is \((\mathbf v_k)_i(\mathbf v_k)_j\). Using the three patterns of the exercise intro: \[ \begin{aligned} (1,2)&:\ (+1)(0.577)(0.707)+0\cdot(0.577)(0)+(-1)(0.577)(-0.707)\\ &\qquad=0.408+0+0.408=\boxed{0.816},\\ (1,3)&:\ (+1)(0.577)(0.408)+0\cdot(0.577)(-0.816)+(-1)(0.577)(0.408)\\ &\qquad=0.236+0-0.236=\boxed{0},\\ (3,3)&:\ (+1)(0.408)^2+0\cdot(-0.816)^2+(-1)(0.408)^2\\ &\qquad=0.167+0-0.167=\boxed{0}. \end{aligned} \] These match \(\tilde A_{12}=0.816\), \(\tilde A_{13}=0\) and \(\tilde A_{33}=0\) of Exercise 1. The \(\lambda=0\) term has weight \(0\), so it can be dropped without changing the sum: \((+1)\mathbf v_{+1}\mathbf v_{+1}^{\top}+(-1)\mathbf v_{-1}\mathbf v_{-1}^{\top}\) already gives all of \(\tilde A\). With weight \(0.3\) instead, entry \((1,3)\) would change by \(0.3\,(0.577)(-0.816)=\boxed{-0.141}\), so the term could no longer be dropped. In words: the matrix is its frequencies, and a frequency with weight zero adds nothing.

E10. (a) With \(\tilde A\mathbf r=[0,0.577,0]\) and \(\tilde A^2\mathbf r=[0.471,0,0.333]\): \[ \hat{\mathbf r}=0.5\,[0,0,1]+0.3\,[0,0.577,0]+0.2\,[0.471,0,0.333]=\boxed{[0.094,\,0.173,\,0.567]}. \] (b) The response is \(h(+1)=0.5+0.3+0.2=\boxed{1}\), \(h(0)=\boxed{0.5}\) and \(h(-1)=0.5-0.3+0.2=\boxed{0.4}\). With the coordinates \((0.408,-0.816,0.408)\) of Exercise 4: \[ \sum_kh(\lambda_k)c_k\mathbf v_k=0.408\,\mathbf v_{+1}-0.408\,\mathbf v_{0}+0.163\,\mathbf v_{-1}=[0.094,\,0.173,\,0.567], \] the same vector as (a). In words: choosing the layer weights is choosing \(h\); here the smooth mode passes fully, the mid mode at half and the jagged mode at \(0.4\).

E11. (a) \(h(+1)=1-1=\boxed{0}\), \(h(0)=\boxed{1}\), \(h(-1)=1-1=\boxed{0}\).

  1. No. It deletes both the smooth end and the jagged end and keeps only the middle mode \(\lambda=0\), so it is the opposite of a low-pass: it passes a band in the middle.

  2. Scale the coordinates \((0.408,-0.816,0.408)\) by \((0,1,0)\) to get \((0,-0.816,0)\), so \[ \hat{\mathbf r}=-0.816\,\mathbf v_{0}=-0.816\,[0.577,0,-0.816]=\boxed{[-0.471,\,0,\,0.666]}. \] Check: \(\mathbf r-\tilde A^{2}\mathbf r=[0,0,1]-[0.471,0,0.333]=[-0.471,0,0.667]\), the same to rounding. In words: this filter keeps exactly the middle part that a low-pass discards, and discards the smooth part that a low-pass keeps.

E12. (a) The absolute gain is \(0.132-0.120=\boxed{0.012}\) of Recall@20, and the relative gain is \(0.012/0.120=\boxed{10\%}\).

  1. Fifty-two refits at \(30\) extra hours each is \(\boxed{1{,}560}\) hours. The gain of \(0.012\) is \(12\) units of \(0.001\), so the price is \(1{,}560/12=\boxed{130}\) hours per \(0.001\) of Recall@20.

  2. The gain is unchanged: \(\boxed{0.012}\) absolute, \(\boxed{10\%}\) relative. The price changes: \(52\times300=15{,}600\) hours a year, so \(15{,}600/12=\boxed{1{,}300}\) hours per \(0.001\), ten times more. The closed-form number is what makes that price visible; whether \(10\%\) is worth it is a decision for the team, not something the Recall figure settles.

Large Language Models and Recommendation: the Landscape

E1. Classify by what job the LLM does (§2), with the tell in each:

  1. Recommender (§2.1): the LLM is the ranker. It reads the history as a prompt and directly returns the pick from a candidate list. Tell: the model outputs the recommendation itself.

  2. Enhancer, feature-engineering cell (§2.3a): the LLM runs offline to write tags, and a classical keyword matcher does the serving. Tell: the LLM’s output is a stored feature, and the live request never calls an LLM. Nothing here is a vector or a collaborative model; what makes it an enhancer is that a classical component keeps the serving path.

  3. Generator (§2.2): recommendation as generation. The model emits the item’s Semantic ID token by token, and there is no candidate list. Tell: an item identifier is generated, with no retrieval set.

  4. Recommender, in its reranker form (§2.1): the LLM scores a shortlist that something else retrieved, so it is the recommender role, applied to only some requests. Tell: a model call scores candidates, even if only on 3 requests in 20. Average cost per 1,000 requests: the retriever’s $0.006 plus the calls it triggers, \(0.15\times\$10=\$1.50\), so \(\$0.006+\$1.50=\$1.506\approx\boxed{\$1.5}\). The cheap path is still paid on every request, and the LLM’s price is paid on the fraction that reaches it.

  5. Recommender (§2.1; §5’s instruction-tuned sub-role, BIGRec’s pattern): a tuned general LLM writes the next title as free text, and a lookup grounds it to the catalog. It is not the generator because what is written is a title, not a Semantic ID from a codebook, and the writing model is an LLM, not a compact model trained on codewords. Tell: free text out, then nearest-embedding grounding. Of the 1,000 recommendations, \(1{,}000-870=\boxed{130}\) (13%) are nearest-neighbor guesses; the other 870 are exact matches.

The test for the enhancer, as (b) shows, is not “vectors” but who keeps the serving path: a classical scorer (§2.3 names matrix factorization, LightGCN and a simple tag-matching rule), with the LLM’s work done ahead of time.

E2. (a) Roles: (i) enhancer 2.3a (an offline profile feeds a stored table); (ii) generator 2.2; (iii) recommender 2.1, as a reranker; (iv) agent 2.4; (v) enhancer 2.3b (a two-tower retriever over offline vectors). Ordered from the least model work on the serving path to the most: \[\textbf{(i)}\;<\;\textbf{(v)}\;<\;\textbf{(ii)}\;\lesssim\;\textbf{(iii)}\;<\;\textbf{(iv)}.\] (i) runs no model on the request (a table lookup and a dot product), while (v) runs a small user network once per request, which is §2.3b’s one small embedding call and sits at the single-digit-to-tens-of-ms end of §4.1’s ladder; so (i) < (v) on model work, and they tie only on price, since (v)’s small network is already inside its $0.008. (v) is below (ii) because a small user network is less work than a sequence model that writes a three-code ID with beam search (an argument about what each runs, not a number: no source prices either). (ii) cannot be ordered against (iii) from numbers, because no source prices it: the \(\lesssim\) rests on §2.2’s argument that its model is compact, not on a price. (iv) is above (iii) because it runs four model steps, each as dear as (iii)’s call.

  1. Cost per 1,000 requests: (i) and (v) $0.008; (iii) $25; (iv) \(4\times\$25=\$100\); (ii) unpriced, so it enters no pair. The pairs beyond a factor of 100 are (i) with (iii), (v) with (iii), (i) with (iv) and (v) with (iv). Their factors are \(25/0.008=3{,}125\) for the first two and \(100/0.008=\boxed{12{,}500}\) for the last two, the widest. Pairs (iii) with (iv) differ by \(100/25=4\), and (i) with (v) by \(1\) in price (they differ in model work, part (a)). What the wide pairs share: one member runs no general-LLM call per request and the other runs one or more. Once a model runs per request, quadrupling its steps changes the cost by a factor of 4; whether it runs at all changes it by thousands.

E3. New anchors, same method as §4.1.

  1. Tokens per request: \(36\times450=16{,}200\), so \(16{,}200\times1{,}000/10^{6}=16.2\) million tokens per 1k queries, and at $0.50 per million tokens the reranker costs \(16.2\times0.50=\boxed{\$8.10}\) per 1k queries. Against the CF serve: \(8.10/0.004=2{,}025\approx\boxed{2{,}000\times}\) to two significant figures, \(\log_{10}2{,}025\approx3.3\): between three and four orders of magnitude, the same band as §4.1’s.

  2. At \(2{,}500{,}000\) queries/day \(=2{,}500\) thousand-query units:

\[\begin{aligned}\text{CF serve} &= 2{,}500\times\$0.004 = \boxed{\$10/\text{day}},\\ \text{LLM reranker} &= 2{,}500\times\$8.10 = \boxed{\$20{,}250/\text{day}}.\end{aligned}\]

  1. Specialist cross-encoder against the LLM reranker: \(8.10/1.6=5.06\approx\boxed{5\times\text{ cheaper}}\) to one significant figure. Against the CF serve: \(1.6/0.004=\boxed{400\times\text{ more expensive}}\), about \(2.6\) orders. The two steps multiply to the whole gap, \(400\times5.06=2{,}025\). The first per-query model is the expensive jump (400 of the 2,025); swapping a cheap reranker for a general LLM is a further factor of about 5.

E4. Residual quantization, §2.2’s recipe (snap, subtract, snap the residual):

  1. Distances of \((0.7,0.4)\) to the level-1 codebook:

\[d_{\textsf{A}}=\sqrt{(-0.3)^2+0.4^2}=\sqrt{0.25}=0.5,\quad d_{\textsf{B}}=\sqrt{0.7^2+0.6^2}\approx0.922,\] \(d_{\textsf{C}}\approx1.746\) and \(d_{\textsf{D}}\approx1.565\). The nearest is \(\boxed{\textsf{A}=(1,0)}\), and the residual is \((0.7,0.4)-(1,0)=\boxed{(-0.3,\,0.4)}\).

  1. Distances of the residual \((-0.3,0.4)\) to the level-2 codebook: \(d_{\textsf{p}}=0\) (exact hit), \(d_{\textsf{q}}=0.6\), \(d_{\textsf{r}}=0.8\), \(d_{\textsf{s}}=1.0\). So the second code is \(\textsf{p}\) and the Semantic ID is \(\boxed{(\textsf{A},\,\textsf{p})}\). Reconstruction: \(\textsf{A}+\textsf{p}=(0.7,0.4)\), zero residual here.

  2. The second item \((0.6,0.5)\): \(d_{\textsf{A}}=\sqrt{0.41}\approx0.640\) against \(d_{\textsf{B}}=\sqrt{0.61}\approx0.781\) (and \(\textsf{C},\textsf{D}\) farther), so it also snaps to \(\textsf{A}\), the same level-1 prefix. Its residual is \((0.6,0.5)-(1,0){=}(-0.4,0.5)\), whose distances to the level-2 codebook are \(d_{\textsf{p}}=\sqrt{0.02}\approx0.141\), \(d_{\textsf{q}}\approx0.707\), \(d_{\textsf{r}}\approx0.906\), \(d_{\textsf{s}}\approx1.140\). The nearest is \(\textsf{p}\) again, so its full tuple is also \(\boxed{(\textsf{A},\textsf{p})}\).

The two items collide: with identical IDs the sequence model cannot tell them apart at all. That is the collision case of §2.2, and TIGER’s cure is to append one extra disambiguating token, e.g. \((\textsf{A},\textsf{p},0)\) against \((\textsf{A},\textsf{p},1)\). Prefix sharing is what lets what is learned about one item carry onto another; whole-tuple sharing is the bug.

E5. The cosines are the dot products \(0.6\) (own view), \(0.8\) (near negative) and \(-0.8\) (far negative).

  1. At \(\tau=0.5\) the weights are \(e^{1.2}=3.320\), \(e^{1.6}=4.953\) and \(e^{-1.6}=0.202\), which add to \(8.475\). The own share is \(3.320/8.475=\boxed{0.39}\) and the loss is \(-\ln0.392=\boxed{0.94}\).

  2. At \(\tau=0.1\) the weights are \(e^{6}=403.4\), \(e^{8}=2981.0\) and \(e^{-8}\approx0.0003\), which add to \(3384.4\). The own share is \(0.119\) and the loss is \(\boxed{2.13}\). The near negative sits \(0.2\) above the own view in cosine; at \(\tau=0.5\) that is \(0.4\) in the exponent, so its weight exceeds the own view’s by a factor \(e^{0.4}=1.5\), and at \(\tau=0.1\) it is \(2\) in the exponent, a factor \(e^{2}=7.4\) (\(2981/403\)).

  3. A turn of \(20^\circ\) takes \(\mathbf{e}_i\) to \((0.940,\,0.342)\), with cosines \(\boxed{0.84}\) (own), \(0.96\) (near negative) and \(-0.55\) (far negative). The weights are \(e^{1.675}=5.34\), \(e^{1.914}=6.78\) and \(e^{-1.093}=0.34\), the own share is \(0.43\), and the loss is \(\boxed{0.85}\): it fell, by \(0.09\). In §3’s toy case the loss rose, because the near negative’s cosine gained more than the own view’s. Here the own view gains \(0.24\) (\(0.60\to0.84\)) and the near negative only \(0.16\) (\(0.80\to0.96\)), so the own view closes the gap on it and its share grows.

Which case you are in depends on whether the own view’s gain beats the average gain of the negatives, each weighted by its share of the negatives’ total weight, as the two toys of §3 show. Here the near negative carries about 96% of the negatives’ weight (95% after the turn) and gains \(0.16\), the far negative gains \(0.25\), so the weighted average is about \(0.16\), below the own view’s \(0.24\): the loss falls.

E6. (a) A plain epoch takes \(20\) s, so a night is \(50\times20=1{,}000\) s; with the alignment term an epoch is \(20\times1.9=38\) s and a night is \(1{,}900\) s. The extra is \(900\) s, \(\boxed{15\text{ min}}\) per night, and over a year \(365\times15=5{,}475\) min \(=\boxed{91.25\text{ h}}\) (about 91 hours), not counting profile writing.

  1. One profile call per new film: \(\boxed{1{,}000}\) per day and \(365\times1{,}000=\boxed{365{,}000}\) per year. Per request: \(\boxed{0}\), so all 2 million daily requests are served by the dot product. The three scales of cost are different: once per film (the profile), once per retrain (the alignment adds 15 minutes a night), and per request (nothing).

  2. The films added since the 02:00 retrain were not in it. From 02:00 to 18:00 is \(16\) h, so \(16\times1{,}000/24=\boxed{667}\) films (about 667) have no trained vector, and the next retrain’s vectors go live at 02:00, \(\boxed{8\text{ h}}\) later. A trained model with one vector per item has none for a new item (§2.3), so a 2.3a enhancer built on one, as RLMRec on LightGCN is, cannot place them meanwhile; the feature-encoding cell, 2.3b (two-tower retrieval), could, because it embeds a film from its text when it is added (by construction; not tested here).

E7. (a) The whole-catalog design reads \(5{,}000\times500=2.5\) million tokens per request, so \(2.5\times10^{6}\times1{,}000/10^{6}=2{,}500\) million tokens per 1k requests, and at $1 per million tokens it costs \(\boxed{\$2{,}500}\) per 1k requests. The 50-film reranker reads \(50\times500=25{,}000\) tokens and costs $25 per 1k, so the whole-catalog design is \(2{,}500/25=\boxed{100\times}\) dearer. Setting aside that 2.5 million tokens may not fit in the model’s input window, the 2 s budget is not what rules the design out: cost does. The cheaper design that wins is a cheap retriever or CF model that fetches a shortlist of about 50 films, with the LLM at most reranking those (§2.1’s production sub-pattern), or the LLM kept offline as an enhancer.

  1. No. The number is an average over every test user and item, and it says nothing about which items the gain came from; the claim is about a subset (thin-data items). To support it you would ask for Recall@20 per popularity bucket, say the least-clicked third of items against the most-clicked third, with a paired significance test in each bucket. §3 adds that RLMRec’s own paper reports no such breakdown, so the claim cannot be borrowed from it either. (The figure in the stem is the paper’s LightGCN and Amazon-book Recall@20 for RLMRec-Con, a \(+5.1\%\) rise.)

E8. (a) \(2{,}000{,}000\times768\times4=6.144\times10^{9}\) bytes, \(\boxed{6.1\text{ GB}}\); at 2 bytes per number, \(3.1\) GB.

  1. Each new film’s text is embedded once by the encoder (offline) and its vector appended to the index. That is one encoder call per new film, \(10{,}000/2{,}000{,}000=\boxed{0.5\%}\) of the catalog, and \(10{,}000\times768\times4\approx\boxed{30.7\text{ MB}}\) of new vectors. Re-embedding all 2 million would be \(200\times\) more work and is unnecessary, since a film’s vector depends on its text alone. No retrain of any ranking model is involved.

  2. From 02:00 to 14:05 is \(12\text{ h }5\text{ min}=12.08\) h, and \(12.08/24=\boxed{50\%}\) of a day (0.503). The vector reflects the logs as they stood at 02:00, so the 14:05 click, and everything since 02:00, is missing from it. The oldest a batch vector can be is just under \(\boxed{24\text{ h}}\) (a click at 01:59). To reflect a click made minutes ago, the user side must run at request time: one small network (or one embedding call) over the user’s recent history, then an ANN lookup against the stored film vectors. That is one small call per request and no LLM ranking call, so it stays enhancer-class on cost (the 2.3b cell, §4).

  3. The day’s \(10{,}000/200=\boxed{50\text{ s}}\) on one GPU; a full re-embedding is \(2{,}000{,}000/200=10{,}000\) s, about \(\boxed{2.8\text{ h}}\), which is \(200\times\) the daily job, the ratio found in part b. A new film needs only its text: embed it with the same encoder and insert the vector, with no clicks and no retraining, so it is findable by semantic proximity (by construction; no paper read here tests it).

E9. (a) A time-based split uses as targets only interactions after the model’s knowledge cutoff, 31 March 2025: C (20 April), D, E and F. A (12 January) and B (3 March) precede it and may have been in training. That is \(\boxed{4}\) of the 6, \(67\%\) of the log.

  1. The second model’s cutoff, 30 September 2025, is after every date in the log, so \(\boxed{0}\) of the 6 qualify. The evaluator should test on a log that post-dates the cutoff, or use held-out or obfuscated item IDs so that a memorized title cannot be recognized, and state which was done (§5).

  2. It would measure, at least in part, whether the model remembers films and interactions it was trained on, not how well it predicts behavior it has not seen. That is a measurement problem and not a modeling one: the model need not be any better at the task, but the number is contaminated. A random hold-out does nothing about this, since every held-out interaction lies inside the model’s training period.

E10. (a) Against the \(1.5\) s budget, using §4.1’s latency column:

  • Enhancer (2.3): single-digit to tens of ms, so it is open.
  • Recommender as a reranker (2.1): the vendor’s p50 is about \(0.7\) to \(2\) s; the low end fits and the high end does not, and a p50 is a median, so half the requests are slower. It is marginal, and open only with a shortlist and a margin. The specialist cross-encoder, at \(0.1\) to \(0.2\) s, fits.
  • Agent (2.4): four steps at \(0.7\) s each is \(4\times0.7=2.8\) s, already over \(1.5\) s (three steps would be \(2.1\) s), and at \(2\) s per step it is \(8\) s. It is ruled out.
  • Generator (2.2): the column gives no figure, so the budget cannot be decided from it.
  1. Per day, \(200{,}000\) requests are \(200\) thousand-query units. The pure-CF serve costs \(200\times\$0.004=\boxed{\$0.80}\) and the 50-film reranker \(200\times\$25=\boxed{\$5{,}000}\), a ratio of \(5{,}000/0.80=6{,}250\). (At the cross-encoder anchor of $2 the bill is $400.)

  2. Split: offline, the LLM writes a profile for each new title when it is added and embeds it for the ANN index; LightGCN is retrained with the alignment term on a regular schedule, so the per-epoch overhead (about 1.9 times on a light backbone like LightGCN) recurs at every retrain. Until a new title’s first retrain it has no trained \(\mathbf{e}_i\), so, of the parts of this split, only the two-tower retriever can surface it (a pure content-reading part such as the decision table’s Recommender 2.1 could, but it is not in this split). Serve: LightGCN dot product, or ANN over the offline vectors, with no LLM call.

What to still measure per bucket instead of trusting top-line NDCG: Recall or NDCG restricted to titles added since the last retrain, and separately to items with few clicks, since a top-line metric is an average over all test interactions and can rise while the cold-start problem you care about does not (§5).

Training and Serving a Recommender

E1. Ann \(=[2,0.5]\), positive The Notebook \(\mathbf q^{+}=[2,0.2]\), negative La La Land \(\mathbf q^{-}=[1.5,0.5]\). \(\hat r^{+}=(2)(2)+(0.5)(0.2)=4.1\); \(\hat r^{-}=(2)(1.5)+(0.5)(0.5)=3.25\); gap \(\Delta=4.1-3.25=0.85\). Since \(\Delta=0.85>0\), the order is already correct, but only narrowly: both films are romance-leaning.

E2. With \(\Delta=0.85\) and \(\sigma(0.85)=0.7006\): loss \(=-\ln(0.7006)=0.356\), and the gradient factor is \(1-\sigma(0.85)=0.2994\). Both are larger than §4’s triple (loss \(0.2014\), factor \(0.1824\) at \(\Delta=1.5\)). That is correct and desirable: this triple is ranked correctly by a narrower margin (\(0.85\) against \(1.5\)), so the model is less sure of it and the update pushes harder.

E3. The gradient is

\[\begin{aligned} \frac{\partial\mathcal L}{\partial\mathbf p}&=-(1-\sigma(\Delta))(\mathbf q^{+}-\mathbf q^{-})\\ &=-0.2994\,([2,0.2]-[1.5,0.5])=-0.2994\,[0.5,-0.3]=[-0.1497,\,0.0898]. \end{aligned}\]

Update with \(\eta=0.1\): \(\mathbf p\leftarrow[2,0.5]-0.1\,[-0.1497,0.0898]=[2.0150,\,0.4910]\). Ann’s romance factor moved up (\(2\to2.0150\)) and her action factor down (\(0.5\to0.4910\)), because the positive is more romantic than the negative in the first entry (\(2>1.5\)) and less action-leaning in the second (\(0.2<0.5\)).

E4. \(1-\sigma(5)=1-0.9933=0.0067\) for \(X\) and \(1-\sigma(-2)=\sigma(2)=0.8808\) for \(Y\). The ratio is \(0.8808/0.0067\approx131\) on the printed four-decimal values (\(131.6\) at full precision), so \(Y\)’s update is about 130 times as large as \(X\)’s. With \(\mathbf q^{+}-\mathbf q^{-}=[2,-2]\) and \(\eta=0.1\), the first entry of its user’s vector moves by \(0.1\times0.0067\times2=0.0013\) for \(X\) and by \(0.1\times0.8808\times2=0.1762\) for \(Y\): \(X\)’s step is almost nothing, \(Y\)’s is visible.

E5. (a) Plain-popularity shares: \(80/100=0.80\), \(15/100=0.15\), \(5/100=0.05\); uniform: each \(\tfrac13=0.333\). (b) The \(\text{pop}^{0.75}\) weights \(26.75, 7.62, 3.34\) sum to \(37.71\), so the probabilities are (La La Land), \(7.62/37.71=0.202\) (The Notebook), \(3.34/37.71=0.089\) (Die Hard). (c) La La Land’s probability fell (\(0.80\to0.709\)); The Notebook’s (\(0.15\to0.202\)) and Die Hard’s (\(0.05\to0.089\)) rose. The power \(0.75\) pulls the skew back toward uniform: it softens the skew without removing it.

E6. (a) Each user gets the other \(128-1=127\) positives in the batch as free negatives. (b) \(X\) turns up four times as often as \(Y\), so with no correction it is used as a negative, and pushed down, about four times as often, although it is no worse a film: it is over-penalized for being frequent. (c) The correction takes off \(\ln(0.04/0.01)=\ln 4=1.386\) from \(X\)’s score relative to \(Y\)’s, the handicap that cancels being drawn four times as often.

E7. (a) Exact scan \(\approx Md=4\times10^{6}\times128=5.12\times10^{8}\) multiply-adds, per user, per request. (b) \(\log_2(4\times10^{6})\approx21.9\), so about \(22\) steps. Each step scores about \(24\) linked items: \(22\times24=528\). At the bottom layer the working list of \(60\) candidates scores \(60\times24=1{,}440\) more. Altogether about \(528+1{,}440=1{,}968\), roughly \(2{,}000\) item scores, against \(4\times10^{6}\) for the full scan: a speed-up of about \(4\times10^{6}/1{,}968\approx2{,}000\).

E8. (a) \(X\): dot product \((1)(0.8)+(2)(1.6)=4.0\), distance \(\sqrt{0.2^2+0.4^2}=0.447\). \(Y\): dot product \((1)(3)+(2)(3)=9.0\), distance \(\sqrt{2^2+1^2}=2.236\). The dot product prefers \(Y\); the distance prefers \(X\). (b) The lengths are \(\sqrt5=2.236\), \(0.8\sqrt5=1.789\) and \(3\sqrt2=4.243\), so the unit vectors are \([0.447,0.894]\) for the query and for \(X\) (they point the same way), and \([0.707,0.707]\) for \(Y\). The dot products are now \(1.000\) for \(X\) and \(0.949\) for \(Y\): it prefers \(X\), in agreement with the distance. For unit vectors the dot product is the cosine similarity.

E9. (a) Retrieval scores with \([1,2]\): La La Land \((1)(1.5)+(2)(0.5)=2.5\); Die Hard \((1)(0.5)+(2)(1.8)=4.1\); Matrix \((1)(0)+(2)(2)=4.0\). Interactions with \([1.5,-1]\): La La Land \((1.5)(1.5)+(0.5)(-1)=+1.75\); Die Hard \((0.5)(1.5)+(1.8)(-1)=-1.05\); Matrix \((0)(1.5)+(2)(-1)=-2.0\). The ranker scores are La La Land \(4.25\), Die Hard \(3.05\), Matrix \(2.0\). (b) Final ranking: La La Land \(>\) Die Hard \(>\) Matrix, against retrieval’s order Die Hard, Matrix, La La Land. La La Land rose from rank 3 to 1, Die Hard fell from 1 to 2 and Matrix from 2 to 3. (c) With only Die Hard and Matrix passed on, the final list is Die Hard \((3.05)>\) Matrix \((2.0)\): it loses La La Land, the film the ranker would put first, which never reaches it. Once retrieval drops a film the ranker cannot recover it.

E10. (a) \(10\ \text{ms}\times(2\times10^{6}/300)\approx66{,}667\) ms, about \(67\) seconds, which is \(66{,}667/30\approx2{,}200\) times the budget. (b) At \(20\) ms per \(300\) candidates, a \(10\) ms slice fits \(150\) candidates. (c) A shorter shortlist has fewer slots for good films, so retrieval has to pack the films the user would like into fewer places: it must keep its recall high at a smaller list size, because the ranker can only reorder what retrieval kept.

Click-Through Rate Prediction & Feature Interactions

E1. A clicked impression keeps the term \(-\ln\hat p\). At \(\hat p=0.25\): \(-\ln 0.25=1.3863\). At \(\hat p=0.75\): \(-\ln 0.75=\mathbf{0.2877}\). A non-clicked impression keeps \(-\ln(1-\hat p)\), so at \(\hat p=0.75\) it costs \(-\ln 0.25=\mathbf{1.3863}\): exactly the cost of a click called \(0.25\). In words: calling a non-click “\(0.75\) likely” is as wrong as calling a click “\(0.25\) likely”, and the clicked impression at \(0.75\) is the one the model got more right, with the smaller cost.

E2. Replace Bob/Matrix’s term \(\ln 0.4\) with \(\ln 0.7\). The four label-selected logs are now \(\ln 0.8\), \(\ln 0.5\), \(\ln 0.7\), \(\ln 0.7\), i.e. \(-0.22314\), \(-0.69315\), \(-0.35667\), \(-0.35667\), summing to \(-1.62964\). LogLoss \(=-\tfrac14(-1.62964)\) \(=\mathbf{0.4074}\) — down from \(0.5473\). In words: the model got more confident on a true click, so the score improved.

E3. New terms: \(\ln 0.8\), \(\ln 0.5\), \(\ln 0.45\), \(\ln 0.7\), i.e. \(-0.22314\), \(-0.69315\), \(-0.79851\), \(-0.35667\), summing to \(-2.07147\), so LogLoss \(=\mathbf{0.5179}\), down from \(0.5473\). For AUC the clicks score \(\{0.8,\,0.45\}\) and the non-clicks \(\{0.5,\,0.3\}\): \(0.8>0.5\) ✓, \(0.8>0.3\) ✓, \(0.45>0.5\) ✗, \(0.45>0.3\) ✓, so AUC stays \(\tfrac34=\mathbf{0.75}\). In words: LogLoss noticed the better probability; AUC did not, because \(0.45\) is still below Ann’s un-clicked \(0.5\) and the order of the pairs has not changed.

E4. The clicks are Ann–Titanic (\(0.6\)) and Bob–Matrix (\(0.3\)); the non-clicks are Ann–Matrix (\(0.6\)) and Cara–Titanic (\(0.2\)). The four pairs: \(0.6\) vs \(0.6\) is a tie, worth \(0.5\); \(0.6\) vs \(0.2\) is correct, \(1\); \(0.3\) vs \(0.6\) is wrong, \(0\); \(0.3\) vs \(0.2\) is correct, \(1\). Total \(2.5\) of \(4\), so AUC \(=\mathbf{0.625}\). In words: of every \(8\) random (click, non-click) pairs this model orders \(5\) correctly, counting each tie as half right.

E5. Layer \(0\): \(\mathbf x_0^{\top}\mathbf w_0=(2)(0.25)+(1)(0.5)=1.0\), so \(\mathbf x_1=1.0\,[2,1]+[2,1]=\mathbf{[4,\,2]}\). Layer \(1\): \(\mathbf x_1^{\top}\mathbf w_1=(4)(1)+(2)(0.5)=5\), so \(\mathbf x_2=5\,[2,1]+[4,2]=\mathbf{[14,\,7]}\), which is \(7\,\mathbf x_0\): a multiple of \(\mathbf x_0\). With \(\mathbf b_1=[1,\,0]\): \(\mathbf x_2=[10,5]+[1,0]+[4,2]=\mathbf{[15,\,7]}\). The entries are \(15/2=7.5\) and \(7/1=7\) times those of \(\mathbf x_0\): two different factors, so it is no longer a multiple of \(\mathbf x_0\). In words: with zero biases the layers only stretch the direction of \(\mathbf x_0\) (xDeepFM’s Theorem 2.1 proves this for every cross layer without biases); a bias is what lets the output turn away from it.

E6. On the log-odds scale the four rates are \(z=\ln\frac{p}{1-p}\): \(0.40\to-0.4055\), \(0.10\to-2.1972\), \(0.08\to-2.4423\), \(0.04\to-3.1781\). The last row gives \(b=\mathbf{-3.1781}\); the second row gives \(w_{\text{rom}}=-2.1972-(-3.1781)=\mathbf{0.9808}\) and the third \(w_{\text{tab}}=-2.4423-(-3.1781)=\mathbf{0.7357}\). The romance-on-tablet row is then predicted \(z=-3.1781+0.9808+0.7357=-1.4615\) (summing the unrounded logits), so \(\sigma(-1.4615)=\mathbf{0.1882}\), less than half of the true \(0.40\). To fit it the cross weight must be \(-0.4055-(-1.4615)\approx\mathbf{1.0561}\) (the rounded figures give \(1.0560\)). In words: the argument of §2 survives the change of scale and the change of table: the additive model still has no room for the interaction, and one cross weight, now on the log-odds scale, makes room.

E7. The pair strength is \(\langle\mathbf e_{\text{romance}},\mathbf e_{\text{desktop}}\rangle=(-0.5)(0.6)+(1)(0.2)=\mathbf{-0.1}\). First order: \(-2.0+0.2+0.1=-1.7\), so \(y_{\text{FM}}=-1.7+(-0.1)=-1.8\) and \(\sigma(-1.8)=\mathbf{0.1419}\). The wide arm has cross weight \(0\) for this never-trained pair: \(-1.7\), and \(\sigma(-1.7)=\mathbf{0.1545}\). In words: the FM arm shades the score a little downward, using vectors learned from other impressions of romance and of desktop; the wide arm cannot say anything about the pair, so it stays at the sum of the single-feature weights.

E8. With the residual, \(\mathbf x_0^{\top}\mathbf w=0.5(3)+0.5(1)=2\): \(\mathbf x_1=2\,\mathbf x_0+\mathbf x_0=[9,3]\); then \(\mathbf x_1^{\top}\mathbf w=6\) and \(\mathbf x_2=6\,\mathbf x_0+\mathbf x_1=[27,9]\). Without it: \(\mathbf x_1=2\,\mathbf x_0=[6,2]\); \(\mathbf x_1^{\top}\mathbf w=4\) and \(\mathbf x_2=4\,\mathbf x_0=[12,4]\). Tripling the input to \([9,3]\) (so \(\mathbf x_0^{\top}\mathbf w=6\)): with the residual \(\mathbf x_1=6[9,3]+[9,3]=[63,21]\), then \(\mathbf x_1^{\top}\mathbf w=42\) and \(\mathbf x_2=42[9,3]+[63,21]=[441,147]\); growth factors \(63/9=\mathbf{7}\) and \(441/27\approx\mathbf{16.33}\). Without it \(\mathbf x_1=6[9,3]=[54,18]\), then \(\mathbf x_1^{\top}\mathbf w=36\) and \(\mathbf x_2=36[9,3]=[324,108]\); factors \(54/6=\mathbf{9=3^2}\) and \(324/12=\mathbf{27=3^3}\). In words: only the version without the residual grows by a clean power of \(3\), \(3^{L+1}\), because its output is made of one degree, \(L+1\). With the residual the factors are not powers of \(3\) because degrees mix: the first entry of \(\mathbf x_1=[9,3]\) is a degree-\(1\) part \(3\) plus a degree-\(2\) part \(6\), and tripling the input gives \(3\cdot3+6\cdot9=63\). So the residual keeps the degrees \(1\) to \(L+1\) all present.

E9. The free weight \(w_{\text{romance},\text{tablet}}\) multiplies the feature \(x_{\text{romance}}x_{\text{tablet}}\), which is \(1\) only when both are active, so only the \(\mathbf{7}\) impressions that show both can move it. FM’s \(\mathbf v_{\text{romance}}\) is moved by every impression in which romance is active together with some other active feature, \(\mathbf{500}\) of them, and \(\mathbf v_{\text{tablet}}\) by \(\mathbf{300}\). The vectors therefore stand on \(500/7\approx\mathbf{71}\) and \(300/7\approx\mathbf{43}\) times more impressions than the free weight. In words: the free weight rests on \(7\) examples while each vector rests on hundreds, so for this rare pair the dot product of the two vectors is the steadier estimate (the authors’ argument of §6, with its count made concrete here).

E10. \(\sigma(0.8)=\mathbf{0.6900}\), \(\sigma(-0.3)=\mathbf{0.4256}\), \(\sigma(-1.5)=\mathbf{0.1824}\): the ranking is film 1, film 2, film 3. Cara clicks only film 1, so the pairs are (film 1, film 2) and (film 1, film 3), both ordered correctly: AUC \(=\mathbf{1}\). LogLoss keeps \(-\ln 0.6900=0.3711\), \(-\ln(1-0.4256)=0.5544\) and \(-\ln(1-0.1824)=0.2014\): sum \(1.1269\), LogLoss \(=\mathbf{0.3756}\). After the retraining \(\sigma(0.2)=\mathbf{0.5498}\), \(\sigma(0.5)=\mathbf{0.6225}\) and film 3 is unchanged at \(0.1824\): the ranking is now film 2, film 1, film 3. Film 1 is scored below the non-clicked film 2 but still above film 3, so one pair of two is right and AUC \(=\mathbf{0.5}\). The terms are \(0.5981\), \(0.9741\) and \(0.2014\), sum \(1.7736\), LogLoss \(=\mathbf{0.5912}\): worse.

In words: the change moved film 1 down and film 2 up, which flipped one pair, so both metrics got worse; AUC counts flipped pairs, LogLoss also prices how far the probabilities moved. The fields to feed: user (Cara), item (each candidate), the item’s genre, device=mobile, daypart=evening, each one-hot and embedded; log AUC (the ordering) and LogLoss (the probabilities) against held-out clicks.

E11. \(W_0\mathbf x_0+\mathbf b_0=[(1)(2)+(0.5)(1)+0.5,\ (0)(2)+(1)(1)+0]=[3,\,1]\), so \(\mathbf x_1=[2,1]\odot[3,1]+[2,1]=\mathbf{[8,\,2]}\). The old DCN layer: the scalar is \(\mathbf x_0^{\top}\mathbf w_0=2.5\), so \(\mathbf x_1=2.5\,[2,1]+[0.5,0]+[2,1]=\mathbf{[7.5,\,3.5]}\). The ratios to \(\mathbf x_0=[2,1]\) are \(4\) and \(2\) for DCNv2 and \(3.75\) and \(3.5\) for DCN: neither output is a multiple of \(\mathbf x_0\), and DCNv2 has moved further (its ratios differ by \(2\), the old layer’s by \(0.25\)). In words: in the old layer only the bias turns the output away from \(\mathbf x_0\), and slightly; in DCNv2 the second row gives that entry its own mix, so the direction is free, as in §7.

Bandits & Online Recommendation

E1. Each pull of the sub-optimal arm adds its gap to the regret, so \(8 \times 0.25 = \mathbf{2.0}\) expected clicks given up. In words: eight small mistakes of a quarter click each cost two clicks.

E2. \(K=4\), \(\varepsilon=0.2\). The best arm is pulled on exploit (prob \(0.8\)) or when explore lands on it (prob \(0.2/4=0.05\)): \(P(\text{best}) = 1-\varepsilon+\varepsilon/K = 0.8+0.05 = \mathbf{0.85}\). Each specific other arm is pulled only via explore: \(\varepsilon/K = \mathbf{0.05}\). (Check: \(0.85 + 3\times0.05 = 1.00\).) In words: the best arm gets \(85\%\) of rounds and each of the other three gets \(5\%\).

E3. With \(t\) as the pulls so far, \(t=3+2=5\) and \(\ln 5 = 1.609\): \(\text{UCB}_M = 0.667 + \sqrt{2(1.609)/3} = 0.667 + 1.036 = \mathbf{1.703}\); \(\hat\mu_T=1/2=0.5\) and \(\text{UCB}_T = 0.5 + \sqrt{2(1.609)/2} = 0.5 + 1.268 = \mathbf{1.768}\). UCB pulls T again, by \(0.066\) (computed before rounding the scores). In words: T has been tried only twice, so its bonus (\(1.268\)) still outweighs M’s higher mean (by \(0.167\)).

E4. Means: \(\hat\mu_M=22/40=0.55\) and \(\hat\mu_T=4/10=0.40\). With \(\ln 50=3.912\): \(\text{UCB}_M = 0.55 + \sqrt{2(3.912)/40} = 0.55 + 0.442 = \mathbf{0.992}\); \(\text{UCB}_T = 0.40 + \sqrt{2(3.912)/10} = 0.40 + 0.885 = \mathbf{1.285}\). UCB pulls T, the arm with the lower mean (by \(0.15\)), because its bonus is larger (by \(0.442\)). In words: UCB pulls the highest upper bound, not the highest mean, so a lightly tried arm gets a turn.

E5. Starting from \(\text{Beta}(1,1)\) and adding \(s=4\) clicks, \(f=6\) non-clicks: \(\text{Beta}(1+4,\,1+6) = \mathbf{\text{Beta}(5,7)}\), posterior mean \(\dfrac{5}{5+7} = \dfrac{5}{12} = \mathbf{0.417}\). In words: the best guess for this arm’s click chance is about \(42\%\).

E6. (a) The explore slice is spread over all \(20\) films, so the \(19\) poor films get \(\varepsilon\cdot\frac{K-1}{K}=0.05\times\frac{19}{20}=\mathbf{0.0475}\) of all impressions, \(4.75\%\), forever; one specific poor film gets \(\varepsilon/K=\mathbf{0.0025}\). (b) The bonus is \(\sqrt{2\ln(10^{6})/10^{4}}=\sqrt{0.002763}=0.0526\), so its score is \(0.1+0.0526=\mathbf{0.153}\), far below the best film’s \(0.5\) or more: it is not pulled. (c) A bonus of \(0.4\) needs \(2\ln t/10^{4}=0.16\), so \(\ln t=800\), a \(t\) with more than \(300\) digits: in practice never. In words: ε-greedy keeps paying \(0.25\%\) of the traffic to a film it already knows is poor, while UCB’s bonus for a film with \(10{,}000\) pulls is too small to matter.

E7. (a) \(12.4\%\), \(3.2\%\) and \(0.5\%\) (\(124/1{,}000\), \(323/10{,}000\), \(503/100{,}000\)). (b) The counts rise by \(323-124=199\) and then by \(503-323=180\), so a tenfold step adds about \(190\) and the count at \(T=1{,}000{,}000\) is about \(503+190\approx\mathbf{690}\). (A \(100\)-run simulation, seed 16, to \(T=1{,}000{,}000\) gives \(662\pm12\), within about \(5\%\) of the estimate \(690\); the \(20\)-run counts are noisier.) (c) The gap is halved, so \(1/\Delta^{2}\) quadruples: expect about \(\mathbf{4}\) times as many worse-arm pulls at a given \(T\). (The \(0.6\) and \(0.5\) game, simulated the same way as the problem’s \(20\) runs, seed 16, gives \(253\), \(854\) and \(1{,}673\) at the three horizons, \(2.0\), \(2.6\) and \(3.3\) times the counts above; the factor \(4\) is the large-\(T\) limit and the early rounds fall short of it.) In words: a small gap costs many more pulls to rule out.

E8. (a) \(\text{Beta}(2,2)\): \(P(X>0.55)=1-(3\cdot0.55^{2}-2\cdot0.55^{3})=1-(0.9075-0.3328)=\mathbf{0.425}\). \(\text{Beta}(40,40)\): mean \(0.5\), standard deviation \(\sqrt{0.25/81}=0.0556\), so \(0.55\) is \(z=0.9\) above the mean and the normal tail is \(\mathbf{0.184}\) (the exact Beta value is \(0.186\)). (b) The \(\text{Beta}(2,2)\) film is drawn above \(0.55\) about \(0.425/0.184\approx\mathbf{2.3}\) times as often (\(0.425/0.186\) with the exact value gives the same \(2.3\)). (c) The posterior is \(\text{Beta}(2+160,\,2+240)=\text{Beta}(162,242)\) with mean \(0.40\); the chance of a draw above \(0.55\) is about \(10^{-9}\), so it is no longer drawn above the known film. In words: the same arm that was explored at about \(43\%\) falls to almost never once data has narrowed it.

E9. ε-greedy: with the plain rule of section 3 the established film is the best, and the new film’s average, \(0\) after its one miss, ranks below the established \(0.6\), so it can only be reached by the explore slice, and then only by the coin landing on it: \(\varepsilon/K=0.1/2=\mathbf{0.05}\).

UCB: with \(t=101\), \(\ln 101=4.615\). The established film (mean \(0.6\), \(n=100\)) scores \(0.6+\sqrt{2(4.615)/100}=0.6+0.304=\mathbf{0.904}\); the new film (mean \(0\), \(n=1\)) scores \(0+\sqrt{2(4.615)/1}=\mathbf{3.038}\). The new film has the higher score, so it is shown: chance \(\mathbf{1}\).

Thompson: after its one non-click the new film’s posterior is \(\text{Beta}(1,2)\), whose chance of exceeding a value \(x\) is \((1-x)^2\); with \(X\sim\text{Beta}(61,41)\) the established film’s draw, \(1-X\sim\text{Beta}(41,61)\) and the average is \(41\cdot42/(102\cdot103)=\mathbf{0.164}\) (a \(200{,}000\)-round simulation, seed 16, gives \(16.4\%\)). Ranking: UCB (\(1\)) \(>\) Thompson (\(0.164\)) \(>\) ε-greedy (\(0.05\)). In words: UCB gives the barely-tried film a pull while its bonus dwarfs the established film’s, Thompson gives it about one chance in six, and ε-greedy a slice of \(5\%\) that does not depend on how little we know.

E10. The viewer’s context for Matrix is \((0,1)\). Four pulls, \(2\) clicks: \(A=I+4\,(0,1)^{\top}(0,1)=\begin{pmatrix}1&0\\0&5\end{pmatrix}\), \(\mathbf b=2\,(0,1)^{\top}=(0,2)^{\top}\), \(\hat\theta=A^{-1}\mathbf b=(0,\,0.4)^{\top}\), with \(A^{-1}=\mathrm{diag}(1,\,0.2)\).

For Ann, Titanic \(\mathbf x=(1,0)\): \(0+1.2\sqrt{1}=\mathbf{1.200}\). Matrix \(\mathbf x=(0,0.5)\): \(0.2+1.2\sqrt{0.25\times0.2}=0.2+0.268=\mathbf{0.468}\). She is shown Titanic.

For Cara, Titanic \(\mathbf x=(0.5,0)\): \(0+1.2\sqrt{0.25}=\mathbf{0.600}\). Matrix \(\mathbf x=(0,1)\): \(0.4+1.2\sqrt{0.2}=0.4+0.537=\mathbf{0.937}\). She is shown Matrix.

If Ann clicks Titanic (\(r=1\), \(\mathbf x=(1,0)\)): \(A\to\begin{pmatrix}2&0\\0&5\end{pmatrix}\), \(\mathbf b\to(1,\,2)^{\top}\). Ann’s Titanic bonus was \(1.2\sqrt{1}=1.200\); with \(A^{-1}=\mathrm{diag}(0.5,\,0.2)\) it is \(1.2\sqrt{0.5}=\mathbf{0.849}\), smaller by \(0.351\), because her click put data in the romance direction. In words: one shared model gives two viewers two different films, because their contexts differ; Titanic wins for Ann as no one has been shown it, and Matrix wins for Cara since its direction is the one with a positive estimate.