Skip to content

18.7 Signals and images problem set: convolution, sampling, Fourier, mel, contrastive losses, and WER

Teaching a language model system to see and hear introduces a new class of bugs, and most of them are arithmetic: an encoder that expects 1500 positions and gets 1501, a patch embedding with 196 tokens where the prompt reserved 197 slots, a spectrogram whose bins are 80 Hz apart instead of 40, a thumbnail that aliased before it was hashed. The sections of this chapter built the functions, from Convolution as a linear map through Mel scale and filterbanks. This problem set predicts their numbers by hand, so that a wrong shape in the vision and speech parts of AI Engineering looks wrong before anything runs.

Concepts from other sections (29)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

The set also covers two topics that have no section of their own but that the multimodal models lean on: the contrastive losses CLIP and SigLIP train with (Contrastive image-text: CLIP and SigLIP) and the word error rate that speech evaluations report (Multimodal eval metrics). It has 24 problems with 68 exact answers and 4 proofs (Problems 7, 21, 22, and 24), each followed by a collapsed worked answer with the final result, the derivation, and the common wrong answers and why they are wrong. Model proofs are also in course/solutions/solve/S-M12/. Try each problem before opening its answer. You need nothing beyond the six sections of this chapter and its opening.

Learning objectives

  • Size any convolution, pool, or patch embedding and compute receptive fields, parameter counts, and multiply-accumulates.
  • Write a 1-D correlation as a Toeplitz matrix and derive its backward pass, and prove that col2im is the adjoint of im2col.
  • Predict aliasing in audio and images, and evaluate interpolation kernels and antialiased supports.
  • Use DFT properties (Parseval, conjugate symmetry, the convolution theorem) and count FFT work, STFT frames, and mel and decibel values.
  • Derive the gradients of InfoNCE and SigLIP’s sigmoid loss, compute WER, and prove that WER is not a metric.

The problems use the following symbols.

SymbolMeaningType / shape
n,k,s,p,dn, k, s, p, dinput length, kernel size, stride, padding, dilationints
TTToeplitz matrix of a 1-D correlationmatrix
sr\text{sr}, fN=sr/2f_N = \text{sr}/2sample rate and Nyquist frequencyHz
k(t)k(t), aainterpolation kernel and Keys’ parameterfunction, float
X=WxX = WxDFTvector
nfftn_\text{fft}, hop\text{hop}STFT frame length and hopints
m(f)m(f)mel valuemel
sjs_j, NNcontrastive logits and candidate countfloats, int
σ(u)=1/(1+e−u)\sigma(u) = 1/(1 + e^{-u})logistic sigmoidfunction
S,D,IS, D, Isubstitutions, deletions, insertionsints

Convolution arithmetic and the conv as a matrix. out=⌊(n+2p−d(k−1)−1)/s⌋+1\text{out} = \lfloor (n + 2p - d(k-1) - 1)/s \rfloor + 1. A stack of layers sees 1+∑ℓ(kℓ−1)∏m<ℓsm1 + \sum_\ell (k_\ell - 1) \prod_{m < \ell} s_m input samples. The correlation y=Txy = Tx has the kernel along each row of TT, shifted one column per row; T⊤T^\top maps an output gradient to an input gradient, which is the full true convolution of the gradient with the kernel. im2col copies each pixel into every window that contains it; col2im adds those copies back (see Convolution as a linear map).

Sampling, aliasing, and interpolation. A tone at ff sampled at sr\text{sr} is indistinguishable from ∣f−j sr∣|f - j\,\text{sr}| for any integer jj; it appears at the representative in [0,fN][0, f_N]. Keeping every qq-th pixel divides the rate by qq, so frequencies above 1/(2q)1/(2q) cycles per input pixel alias. Antialiased resizing stretches the kernel by the scale factor to remove them first (see Resampling and interpolation).

Fourier. X[k]=∑jx[j]e−2πijk/nX[k] = \sum_j x[j] e^{-2\pi i jk/n}. ∑∣X∣2=n∑∣x∣2\sum |X|^2 = n \sum |x|^2. Real input gives X[n−k]=X[k]‾X[n - k] = \overline{X[k]}; real and even input gives a real spectrum. The product of DFTs is the DFT of the circular convolution; padding to length ≥na+nb−1\ge n_a + n_b - 1 makes it linear. An FFT recursion splits nn by its factors (see DFT and FFT). Centered STFT frames number 1+⌊n/hop⌋1 + \lfloor n/\text{hop} \rfloor, bins are sr/nfft\text{sr}/n_\text{fft} apart, and a periodic Hann at hop nfft/4n_\text{fft}/4 overlap-adds its square to 3/23/2 (see Sampling, Nyquist, windows, and the STFT).

Mel and decibels. Slaney mel is linear to 15 mel at 1000 Hz, then 27 mel per factor 6.4. Power in dB is 10log⁡1010 \log_{10}. Whisper’s feature is (max⁡(L,max⁡L−8)+4)/4(\max(L, \max L - 8) + 4)/4 with L=log⁡10max⁡(mel,10−10)L = \log_{10} \max(\text{mel}, 10^{-10}) (see Mel scale and filterbanks).

Contrastive losses. CLIP scores a batch of NN image-text pairs with a similarity matrix and, for each image, classifies its own caption among the NN with a softmax: InfoNCE, L=−log⁡es1∑jesjL = -\log \frac{e^{s_1}}{\sum_j e^{s_j}}. Its gradient is softmax minus one-hot, and van den Oord et al. show I(X;Y)≥log⁡N−LI(X; Y) \ge \log N - L: more negatives can certify more mutual information. SigLIP replaces the softmax by an independent logistic loss per pair, −log⁡σ(z(ts+b))-\log \sigma(z(ts + b)) with z=+1z = +1 on the diagonal and −1-1 elsewhere, so no normalizer couples the pairs.

Edit distance and WER. The word edit distance is the minimum number of substitutions, deletions, and insertions turning one word sequence into another, computed by a DP over prefixes. WER divides it by the number of reference words: insertions can push it above 1, and swapping reference and hypothesis changes the denominator.

Before the problems, here is one small example of each kind, worked in full.

Conv. n=10n = 10, k=3k = 3, s=2s = 2, p=1p = 1: ⌊(10+2−3)/2⌋+1=5\lfloor (10 + 2 - 3)/2 \rfloor + 1 = 5. The window starts sit at −1,1,3,5,7-1, 1, 3, 5, 7 in input coordinates, and the next one (99) would need input sample 11.

Aliasing. 9 kHz sampled at 16 kHz: ∣9000−16000∣=7000|9000 - 16000| = 7000 Hz, below fN=8000f_N = 8000, so it appears at 7 kHz.

DFT. x=(1,−1,1,−1)x = (1, -1, 1, -1): X[k]=∑j(−1)j(−i)jkX[k] = \sum_j (-1)^j (-i)^{jk}; only k=2k = 2 survives, X=(0,0,4,0)X = (0, 0, 4, 0), and Parseval gives 16=4⋅416 = 4 \cdot 4.

InfoNCE. Two candidates with equal logits: L=log⁡2L = \log 2; with the positive ahead by ln⁡3\ln 3, L=log⁡(1+1/3)=log⁡(4/3)L = \log(1 + 1/3) = \log(4/3).

WER. Reference “a b c”, hypothesis “a x c d”: one substitution and one insertion, WER =2/3= 2/3.

Answers are exact (for example 9/169/16, not 0.5625). The tag after each problem says what kind of answer is expected: a number, an expression, a vector or matrix, a true-or-false, a choice, or a proof. The problems run in the order of the chapter, then the two extra topics.

Problem 1. Give the output length for (a) n=224n = 224, k=7k = 7, s=2s = 2, p=3p = 3 (a ResNet stem); (b) a 3×33 \times 3 max pool with s=2s = 2, p=1p = 1 on that output, along one axis; (c) Whisper’s stem on 3000 frames: a conv with k=3k = 3, p=1p = 1, s=1s = 1, then one with k=3k = 3, p=1p = 1, s=2s = 2; (d) n=32n = 32, k=3k = 3, d=4d = 4, p=0p = 0, s=1s = 1. [number]

Answer

(a) 112, (b) 56, (c) 1500, (d) 24. Apply out=⌊(n+2p−d(k−1)−1)/s⌋+1\text{out} = \lfloor (n + 2p - d(k-1) - 1)/s \rfloor + 1 each time. (a) ⌊(224+6−6−1)/2⌋+1=⌊223/2⌋+1=112\lfloor (224 + 6 - 6 - 1)/2 \rfloor + 1 = \lfloor 223/2 \rfloor + 1 = 112: the stem halves the image. (b) On n=112n = 112: ⌊(112+2−2−1)/2⌋+1=55+1=56\lfloor (112 + 2 - 2 - 1)/2 \rfloor + 1 = 55 + 1 = 56. (c) The first conv keeps the length, ⌊(3000+2−3)/1⌋+1=3000\lfloor (3000 + 2 - 3)/1 \rfloor + 1 = 3000; the second gives ⌊2999/2⌋+1=1500\lfloor 2999/2 \rfloor + 1 = 1500, which is why Whisper’s encoder has 1500 positions. (d) The dilated kernel spans 4⋅2+1=94 \cdot 2 + 1 = 9 samples, so 32−9+1=2432 - 9 + 1 = 24. Common mistakes: dropping the +1+1 (111, 55, 1499); 1501 from rounding up instead of down; and 30 in (d), which sizes the dilated kernel as if it spanned 3 samples.

Problem 2. (a) How many input samples does a kernel of size kk with dilation dd span? Answer in kk and dd. (b) For odd kk, stride 1, and dilation 1, which padding pp keeps the output length equal to the input length? Answer in kk. [expr]

Answer

(a) d(k−1)+1d(k - 1) + 1, (b) (k−1)/2(k - 1)/2. (a) The kk taps are dd apart, so there are k−1k - 1 gaps of dd between the first and the last tap, plus the first sample itself. (b) With s=d=1s = d = 1 the formula gives n+2p−k+1n + 2p - k + 1; setting it equal to nn gives 2p=k−12p = k - 1, an integer for odd kk (“same” padding: 1 for 3×33 \times 3, 3 for 7×77 \times 7). A common mistake is dkdk in (a), which counts a gap after the last tap, and k/2k/2 in (b), which is not even an integer for odd kk.

Problem 3. The receptive field of an output is the number of input samples (along one axis) it depends on. Give it for (a) three stacked 3×33 \times 3 convs with stride 1; (b) a 3×33 \times 3 conv with stride 2 followed by a 3×33 \times 3 conv with stride 1. [number]

Answer

(a) 7, (b) 7. Use 1+∑ℓ(kℓ−1)∏m<ℓsm1 + \sum_\ell (k_\ell - 1) \prod_{m < \ell} s_m. (a) Three layers of k=3k = 3 at stride 1: 1+2+2+2=71 + 2 + 2 + 2 = 7. (b) The first layer adds k−1=2k - 1 = 2; the second also adds 2, but each of its steps is 2 input samples apart because of the first layer’s stride, so it adds 2⋅2=42 \cdot 2 = 4: 1+2+4=71 + 2 + 4 = 7. A common mistake in (a) is 3⋅3=93 \cdot 3 = 9, as if the kernels did not overlap; in (b), 5 forgets that the second layer’s steps are scaled by the first layer’s stride.

Correlation, convolution, and the conv as a matrix

Section titled “Correlation, convolution, and the conv as a matrix”

Problem 4. x=(1,2,3,4)x = (1, 2, 3, 4) and w=(1,0,−1)w = (1, 0, -1), no padding. Give (a) the cross-correlation y[i]=∑rw[r] x[i+r]y[i] = \sum_r w[r]\, x[i + r] and (b) the true convolution (kernel flipped). [vector]

Answer

(a) (−2,−2)(-2, -2), (b) (2,2)(2, 2). There are 4−3+1=24 - 3 + 1 = 2 outputs. (a) y[0]=1⋅1+0⋅2−1⋅3=−2y[0] = 1 \cdot 1 + 0 \cdot 2 - 1 \cdot 3 = -2, y[1]=2−4=−2y[1] = 2 - 4 = -2. (b) The true convolution slides the flipped kernel (−1,0,1)(-1, 0, 1): −1+3=2-1 + 3 = 2 and −2+4=2-2 + 4 = 2. Neural-network “convs” are cross-correlations; confusing the two swaps the answers.

Problem 5. For the cross-correlation of Problem 4 written as y=Txy = T x: (a) give TT. [matrix] (b) Give the input gradient T⊤gT^\top g for the upstream gradient g=(1,1)g = (1, 1). [vector] (c) Is T⊤gT^\top g equal to the full (zero-padded) true convolution of gg with ww? [bool]

Answer

(a) (10−10010−1)\begin{pmatrix} 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{pmatrix}, (b) (1,1,−1,−1)(1, 1, -1, -1), (c) true. (a) Row ii holds the kernel ww starting at column ii: a Toeplitz matrix. (b) T⊤gT^\top g is the sum of the rows weighted by gg: (1,0,−1,0)+(0,1,0,−1)=(1,1,−1,−1)(1, 0, -1, 0) + (0, 1, 0, -1) = (1, 1, -1, -1); it has the input’s length 4, not the output’s. (c) The full true convolution of g=(1,1)g = (1, 1) with w=(1,0,−1)w = (1, 0, -1) is (1,1+0,0−1,−1)=(1,1,−1,−1)(1, 1 + 0, 0 - 1, -1) = (1, 1, -1, -1), the same vector: the backward of a correlation is a true convolution of the gradient with the kernel. A common mistake in (a) is the flipped kernel in each row, which is the matrix of the true convolution.

Problem 6. (a) With xx and gg as in Problems 4 and 5, give the weight gradient ∂L/∂w[r]=∑ig[i] x[i+r]\partial L / \partial w[r] = \sum_i g[i]\, x[i + r]. [vector] (b) A 1×1×3×31 \times 1 \times 3 \times 3 image goes through im2col with a 2×22 \times 2 kernel, stride 1, no padding. How many times is its center pixel copied into the columns? [number]

Answer

(a) (3,5,7)(3, 5, 7), (b) 4. (a) ∂L/∂w[r]=g[0]x[r]+g[1]x[r+1]\partial L/\partial w[r] = g[0] x[r] + g[1] x[r + 1]: 1+2=31 + 2 = 3, 2+3=52 + 3 = 5, 3+4=73 + 4 = 7. It is the correlation of the input with the gradient; reversing it to (7,5,3)(7, 5, 3) is a common mistake. (b) A 2×22 \times 2 kernel on a 3×33 \times 3 image has 2×2=42 \times 2 = 4 windows, and the center pixel lies in all four, so im2col copies it four times (a corner pixel only once).

Problem 7. Prove that col2im is the adjoint of im2col: for every input xx and every column array YY of the matching shape, ⟨im2col⁡(x),Y⟩=⟨x,col2im⁡(Y)⟩\langle \operatorname{im2col}(x), Y \rangle = \langle x, \operatorname{col2im}(Y) \rangle, where col2im adds every column entry back to the pixel it was copied from and drops entries copied from the zero padding. [proof]

Answer

Claim. For every input xx and every column array YY of the matching shape, ⟨im2col⁡(x),Y⟩=⟨x,col2im⁡(Y)⟩\langle \operatorname{im2col}(x), Y \rangle = \langle x, \operatorname{col2im}(Y) \rangle.

Proof. Index column entries by α=(n,c,r,s,p,q)\alpha = (n, c, r, s, p, q). im2col copies one value into each entry: with padding (ph,pw)(p_h, p_w), stride (sh,sw)(s_h, s_w), and dilation (dh,dw)(d_h, d_w), entry α\alpha reads the input position π(α)=(n,c,psh+rdh−ph,qsw+sdw−pw)\pi(\alpha) = (n, c, p s_h + r d_h - p_h, q s_w + s d_w - p_w), and is 0 when that position falls in the padding. Let AA be the set of entries whose π(α)\pi(\alpha) is a real pixel. Then ⟨im2col⁡(x),Y⟩=∑α∈Axπ(α)Yα.\langle \operatorname{im2col}(x), Y \rangle = \sum_{\alpha \in A} x_{\pi(\alpha)} Y_\alpha . Every α∈A\alpha \in A reads exactly one pixel, so grouping the terms by the pixel u=π(α)u = \pi(\alpha) partitions AA: ∑α∈Axπ(α)Yα=∑uxu(∑α∈A, π(α)=uYα).\sum_{\alpha \in A} x_{\pi(\alpha)} Y_\alpha = \sum_u x_u \Big( \sum_{\alpha \in A,\ \pi(\alpha) = u} Y_\alpha \Big). The inner sum is col2im’s definition: every column entry goes back to the pixel it was copied from, entries landing on the same pixel are added, and entries copied from the padding are dropped. So it equals col2im⁡(Y)u\operatorname{col2im}(Y)_u, and the sum is ⟨x,col2im⁡(Y)⟩\langle x, \operatorname{col2im}(Y) \rangle. Since this holds for all xx and YY, col2im is the adjoint (the transpose) of the linear map im2col.

A complete proof writes im2col as an index map with the padding case, expands the inner product, regroups by input pixel, and identifies the regrouped coefficient as col2im. A col2im that assigns instead of adding keeps one term of each inner sum and breaks the identity wherever windows overlap. Model proof also in q7.md.

Problem 8. A conv from 64 to 128 channels with a 3×33 \times 3 kernel, groups 1, and a bias. Give (a) its parameter count; (b) the parameter count of a depthwise 3×33 \times 3 conv on 64 channels with a bias; (c) the multiply-accumulates of the first conv for one image with a 56×5656 \times 56 output. [number]

Answer

(a) 73856, (b) 640, (c) 231211008. (a) Weights 64⋅128⋅3⋅3=7372864 \cdot 128 \cdot 3 \cdot 3 = 73728, plus 128 biases. (b) Depthwise means one 3×33 \times 3 filter per channel: 64⋅9+64=64064 \cdot 9 + 64 = 640. (c) Each of the 128⋅56⋅56=401408128 \cdot 56 \cdot 56 = 401408 outputs takes 64⋅9=57664 \cdot 9 = 576 multiply-accumulates: 401408⋅576=231211008401408 \cdot 576 = 231211008 (the bias is an add, not a MAC). Common mistakes: 73728 forgets the bias; 36928 in (b) is a full 64→6464 \to 64 conv; 462422016 in (c) counts FLOPs (two per MAC) where MACs were asked.

Problem 9. ViT-B/16 on a 3×224×2243 \times 224 \times 224 image with width 768. Give (a) the number of patches; (b) the parameters of the patch embedding (a conv with kernel and stride 16, with a bias); (c) the sequence length after prepending a CLS token. [number]

Answer

(a) 196, (b) 590592, (c) 197. (a) 224/16=14224/16 = 14 patches per side, 142=19614^2 = 196. (b) The patch embedding is a conv from 3 to 768 channels with a 16×1616 \times 16 kernel: 3⋅16⋅16⋅768=5898243 \cdot 16 \cdot 16 \cdot 768 = 589824 weights plus 768 biases. (c) 196+1=197196 + 1 = 197 with the CLS token. Common mistakes: 197 in (a) (the CLS token is not a patch), 589824 in (b) (no bias), and 196 in (c) (CLS forgotten).

Problem 10. A float32 tensor with logical shape (N,C,H,W)=(2,3,4,5)(N, C, H, W) = (2, 3, 4, 5). (a) Give its byte strides per logical axis when stored NCHW, and (b) when stored NHWC (channels last). [vector] (c) Give the byte offset of element (1,2,3,4)(1, 2, 3, 4) in the NHWC storage. [number] (d) Give the length of one flattened patch when a 3×224×2243 \times 224 \times 224 image is cut into 14×1414 \times 14 patches. [number]

Answer

(a) (240,80,20,4)(240, 80, 20, 4), (b) (240,4,60,12)(240, 4, 60, 12), (c) 476, (d) 588. float32 is 4 bytes. (a) NCHW is row-major in (N,C,H,W)(N, C, H, W): element strides (3⋅4⋅5,4⋅5,5,1)=(60,20,5,1)(3 \cdot 4 \cdot 5, 4 \cdot 5, 5, 1) = (60, 20, 5, 1), times 4 bytes. (b) NHWC stores (N,H,W,C)(N, H, W, C) row-major: element strides N: 60N{:}\ 60, H: 5⋅3=15H{:}\ 5 \cdot 3 = 15, W: 3W{:}\ 3, C: 1C{:}\ 1; listed per logical axis (N,C,H,W)(N, C, H, W) and times 4 bytes, (240,4,60,12)(240, 4, 60, 12). (c) 1⋅240+2⋅4+3⋅60+4⋅12=4761 \cdot 240 + 2 \cdot 4 + 3 \cdot 60 + 4 \cdot 12 = 476 bytes. (d) A 14×1414 \times 14 patch over 3 channels flattens to 3⋅14⋅14=5883 \cdot 14 \cdot 14 = 588 values. Common mistakes: strides in elements (60,20,5,1)(60, 20, 5, 1); the NHWC strides in memory order (240,60,12,4)(240, 60, 12, 4) instead of per logical axis; and 196 in (d), which forgets the channels.

Problem 11. (a) Give the Nyquist frequency in Hz at a 16 kHz sample rate. At what frequency in Hz does a pure tone appear when (b) 7 kHz is sampled at 10 kHz; (c) 12 kHz is sampled at 16 kHz; (d) 25 kHz is sampled at 16 kHz? [number]

Answer

(a) 8000, (b) 3000, (c) 4000, (d) 7000. (a) fN=16000/2f_N = 16000/2. For the others, take the representative of ∣f−j sr∣|f - j\,\text{sr}| in [0,fN][0, f_N]. (b) ∣7000−10000∣=3000≤5000|7000 - 10000| = 3000 \le 5000. (c) ∣12000−16000∣=4000≤8000|12000 - 16000| = 4000 \le 8000. (d) 25000−16000=900025000 - 16000 = 9000 is still above 8000; the nearest multiple of the rate is 3200032000, and ∣25000−32000∣=7000|25000 - 32000| = 7000. A common mistake in (d) is 9 kHz, from folding once instead of to the nearest multiple of the rate, and in (b) and (c) reporting the original frequency, which is above Nyquist.

Problem 12. An image has a stripe pattern with a period of 3 pixels. It is shrunk by 4 on each axis by keeping every fourth pixel, with no filter. (a) Give the period in output pixels of the stripes that appear. [number] (b) Can the output grid represent the original pattern without aliasing? [bool] (c) Give the highest frequency, in cycles per input pixel, that a shrink by 4 can keep without aliasing. [number] (d) A 1-pixel checkerboard is shrunk by 2 by keeping every other pixel on each axis. The result is: (a) a checkerboard, (b) a constant image, (c) stripes. [choice]

Answer

(a) 3, (b) false, (c) 1/8, (d) b. (a) The stripes have frequency 1/31/3 cycle per input pixel. Keeping every fourth pixel samples at 1 per 4 input pixels, i.e. the frequency becomes 4/34/3 cycles per output pixel, which aliases to ∣4/3−1∣=1/3|4/3 - 1| = 1/3: stripes with a period of 3 output pixels (12 input pixels), a moiré. (b) The output grid’s Nyquist frequency is 1/81/8 cycle per input pixel, and 1/3>1/81/3 > 1/8, so no. (c) That Nyquist limit: 12⋅14=18\frac12 \cdot \frac14 = \frac18. (d) The checkerboard is (−1)i+j(-1)^{i + j}; keeping even ii and even jj makes i+ji + j always even, so every kept pixel has the same value: a constant image. Common mistakes: 3/43/4 in (a) (the period divided by 4, as if the pattern were simply shrunk), 1/21/2 or 1/41/4 in (c) (the input’s Nyquist, or the scale itself), and “a checkerboard” in (d).

Problem 13. Keys’ cubic kernel is k(t)=(a+2)∣t∣3−(a+3)∣t∣2+1k(t) = (a+2)|t|^3 - (a+3)|t|^2 + 1 for ∣t∣<1|t| < 1 and a∣t∣3−5a∣t∣2+8a∣t∣−4aa|t|^3 - 5a|t|^2 + 8a|t| - 4a for 1≤∣t∣<21 \le |t| < 2. Give (a) k(1/2)k(1/2) and (b) k(3/2)k(3/2) with a=−1/2a = -1/2 (Pillow), and (c) k(3/2)k(3/2) with a=−3/4a = -3/4 (PyTorch). [number]

Answer

(a) 9/169/16, (b) −1/16-1/16, (c) −3/32-3/32. (a) a=−1/2a = -1/2, ∣t∣=1/2|t| = 1/2: (3/2)(1/8)−(5/2)(1/4)+1=3/16−10/16+16/16=9/16(3/2)(1/8) - (5/2)(1/4) + 1 = 3/16 - 10/16 + 16/16 = 9/16. (b) The outer piece at ∣t∣=3/2|t| = 3/2 is a (27/8−5⋅9/4+8⋅3/2−4)=a (27−90+96−32)/8=a/8a\,(27/8 - 5 \cdot 9/4 + 8 \cdot 3/2 - 4) = a\,(27 - 90 + 96 - 32)/8 = a/8, so −1/16-1/16. (c) The same with a=−3/4a = -3/4: −3/32-3/32. The negative lobe is what makes cubic sharper than bilinear. Common mistakes: 1/21/2 in (a), the bilinear weight; +1/16+1/16 in (b), a sign slip; and −1/16-1/16 in (c), Pillow’s aa where PyTorch’s was asked.

Problem 14. Pillow shrinks with an antialiased kernel stretched by the scale factor. (a) Give the support radius in input pixels of the bilinear kernel (support 1) when shrinking 1000 pixels to 250. (b) Give the number of taps in Pillow’s weight table for that pass, 2⌈support⌉+12 \lceil \text{support} \rceil + 1. (c) Give the support radius of Lanczos3 when shrinking by 2. [number]

Answer

(a) 4, (b) 9, (c) 6. Shrinking by a factor scale\text{scale} stretches the kernel by scale\text{scale} so that it also low-passes below the new Nyquist. (a) 1000/250=41000/250 = 4, so bilinear’s support 1 becomes 4 input pixels. (b) 2⌈4⌉+1=92 \lceil 4 \rceil + 1 = 9 taps. (c) Lanczos3 has support 3; shrinking by 2 gives 6. A common mistake is the unstretched support (1, then 3 taps, and 3), which is resizing without antialiasing.

Problem 15. With X[k]=∑jx[j]e−2πijk/nX[k] = \sum_j x[j] e^{-2\pi i jk/n}, give the DFT of (a) (1,0,0,0)(1, 0, 0, 0), (b) (1,1,1,1)(1, 1, 1, 1), (c) (0,1,0,0)(0, 1, 0, 0). [vector] (d) A real signal of length 8 has X[3]=2−iX[3] = 2 - i. Give X[5]X[5]. [number]

Answer

(a) (1,1,1,1)(1, 1, 1, 1), (b) (4,0,0,0)(4, 0, 0, 0), (c) (1,−i,−1,i)(1, -i, -1, i), (d) 2+i2 + i. (a) An impulse at 0 has X[k]=e0=1X[k] = e^0 = 1 for every kk. (b) A constant has all its energy at k=0k = 0: X[0]=4X[0] = 4, and the other sums of roots of unity are 0. With this unnormalized convention, X[0]X[0] is the sum, not the mean. (c) An impulse at j=1j = 1 gives X[k]=e−2πik/4=(−i)kX[k] = e^{-2\pi i k/4} = (-i)^k. (d) For real input X[n−k]=X[k]‾X[n - k] = \overline{X[k]}, and 8−3=58 - 3 = 5, so X[5]=2−i‾=2+iX[5] = \overline{2 - i} = 2 + i; this symmetry is why rfft keeps only n/2+1n/2 + 1 bins. Common mistakes: (1,i,−1,−i)(1, i, -1, -i) in (c), the inverse transform’s sign, and 2−i2 - i in (d).

Problem 16. (a) Give ∑k∣X[k]∣2\sum_k |X[k]|^2 for x=(1,2,0,−1)x = (1, 2, 0, -1). (b) Give the circular convolution of length 4 of (1,2,0,0)(1, 2, 0, 0) and (1,1,0,0)(1, 1, 0, 0). [vector] (c) Give the smallest transform length for which an FFT product computes the linear convolution of signals of lengths 300 and 100. [number] (d) Is the DFT of a real, even signal (x[j]=x[(n−j) mod n]x[j] = x[(n - j) \bmod n]) real? [bool]

Answer

(a) 24, (b) (1,3,2,0)(1, 3, 2, 0), (c) 399, (d) true. (a) Parseval: ∑∣X∣2=n∑∣x∣2=4(1+4+0+1)=24\sum |X|^2 = n \sum |x|^2 = 4 (1 + 4 + 0 + 1) = 24. (b) The linear convolution is (1,3,2)(1, 3, 2) and has length 3≤43 \le 4, so nothing wraps around: (1,3,2,0)(1, 3, 2, 0). (c) The linear convolution has length 300+100−1=399300 + 100 - 1 = 399, and the circular one equals it when the transform length is at least that. (d) Yes: an even real signal has X[k]=∑jx[j]cos⁡(2πjk/n)X[k] = \sum_j x[j] \cos(2\pi jk/n), because the sine terms of jj and n−jn - j cancel. Common mistakes: 6 in (a) (Parseval without the factor nn of this convention), a length-5 vector in (b), and 300 in (c).

Problem 17. (a) Into how many radix stages does an FFT split n=400n = 400 when it uses radices 4, 4, 5, 5? (b) How many complex multiplies does the direct DFT of length 400 take (n2n^2)? (c) How many butterflies does a radix-2 FFT of length 1024 take, (n/2)log⁡2n(n/2) \log_2 n? [number] (d) Can a radix-2-only FFT compute a 400-point DFT, without changing the bin frequencies? [bool]

Answer

(a) 4, (b) 160000, (c) 5120, (d) false. (a) 400=4⋅4⋅5⋅5400 = 4 \cdot 4 \cdot 5 \cdot 5, one stage per radix. (b) 4002=160000400^2 = 160000. (c) (1024/2)⋅10=5120(1024/2) \cdot 10 = 5120. (d) A radix-2 FFT needs a power of 2; padding 400 to 512 computes a different transform, with bins sr/512\text{sr}/512 apart instead of sr/400\text{sr}/400, which is why Whisper’s 400-point frame needs mixed radices. Common mistakes: 6 in (a) (the prime factorization 24⋅522^4 \cdot 5^2, six stages, is a valid but different plan; the question names radices 4, 4, 5, 5); 400 in (b); 10240 in (c) (nlog⁡2nn \log_2 n instead of half of it).

Problem 18. Whisper: 30 s of audio at 16 kHz, nfft=400n_\text{fft} = 400, hop 160, centered frames. Give (a) the number of STFT frames before Whisper drops the last one; (b) the spacing of the bins in Hz; (c) the number of rfft bins per frame; (d) the constant value of ∑tw[m−t hop]2\sum_t w[m - t\,\text{hop}]^2 for a periodic Hann window at hop nfft/4n_\text{fft}/4; (e) the frame length in milliseconds. [number] (f) Give the periodic Hann window of length 4. [vector]

Answer

(a) 3001, (b) 40, (c) 201, (d) 3/2, (e) 25, (f) (0,1/2,1,1/2)(0, 1/2, 1, 1/2). (a) 30⋅16000=48000030 \cdot 16000 = 480000 samples; centered framing pads nfft/2n_\text{fft}/2 on each side and gives 1+⌊480000/160⌋=30011 + \lfloor 480000/160 \rfloor = 3001 frames, and Whisper drops the last to get 3000. (b) 16000/400=4016000/400 = 40 Hz. (c) 400/2+1=201400/2 + 1 = 201. (d) The periodic Hann is w[m]=sin⁡2(πm/nfft)w[m] = \sin^2(\pi m/n_\text{fft}), so w2=sin⁡4w^2 = \sin^4, whose mean over a period is 3/83/8; overlap-adding at hop nfft/4n_\text{fft}/4 stacks 4 copies, 4⋅3/8=3/24 \cdot 3/8 = 3/2. (e) 400/16000400/16000 s =25= 25 ms. (f) w[m]=12−12cos⁡(2πm/4)w[m] = \frac12 - \frac12 \cos(2\pi m/4): (0,1/2,1,1/2)(0, 1/2, 1, 1/2). Common mistakes: 3000 in (a) (the centered padding’s extra frame forgotten), 80 Hz in (b), 200 bins, 2 in (d) (the sum of the window, which is what plain overlap-add needs, not of its square), 10 ms in (e) (the hop), and the symmetric window (0,3/4,3/4,0)(0, 3/4, 3/4, 0) in (f).

Problem 19. On the Slaney scale, give the mel value of (a) 1000 Hz and (b) 6400 Hz. (c) On the HTK scale, m(f)=2595log⁡10(1+f/700)m(f) = 2595 \log_{10}(1 + f / 700): give m(700)m(700). (d) Give a power ratio of 1000 in decibels. (e) Whisper computes L=log⁡10(max⁡(mel,10−10))L = \log_{10}(\max(\text{mel}, 10^{-10})), then max⁡(L,max⁡L−8)\max(L, \max L - 8), then (L+4)/4(L + 4)/4. Give the feature of a band with mel power 10−1210^{-12} in a clip whose largest LL is 1. (f) Give the feature value of pure silence. [number]

Answer

(a) 15, (b) 42, (c) 2595log⁡1022595 \log_{10} 2, (d) 30, (e) −3/4-3/4, (f) −3/2-3/2. (a) 1000⋅3/200=151000 \cdot 3/200 = 15, the break point. (b) 6400=1000⋅6.46400 = 1000 \cdot 6.4, one factor of 6.4 above the break: 15+27=4215 + 27 = 42. (c) 2595log⁡10(1+1)≈781.172595 \log_{10}(1 + 1) \approx 781.17. (d) 10log⁡101000=3010 \log_{10} 1000 = 30 dB. (e) L=log⁡10max⁡(10−12,10−10)=−10L = \log_{10} \max(10^{-12}, 10^{-10}) = -10; the clip raises it to max⁡(−10,1−8)=−7\max(-10, 1 - 8) = -7; (−7+4)/4=−3/4(-7 + 4)/4 = -3/4. (f) Silence floors everywhere at −10-10, the maximum is also −10-10, the clip does nothing, and (−10+4)/4=−3/2(-10 + 4)/4 = -3/2. Common mistakes: 96 in (b) (the linear piece used above 1 kHz), 2595ln⁡22595 \ln 2 in (c), 60 in (d) (the 20log⁡1020\log_{10} amplitude rule on a power), −3/2-3/2 in (e) (the floor applied but not the clip), and −10-10 in (f) (the log without the rescale).

Problem 20. InfoNCE for one anchor scores NN candidates with logits sjs_j (the positive is j=1j = 1) and takes L=−log⁡es1∑jesjL = -\log \frac{e^{s_1}}{\sum_j e^{s_j}}, the cross-entropy with the positive as the label. (a) Give LL for s=(2,0,0,0)s = (2, 0, 0, 0). (b) Give LL when all NN logits are equal. Answer in NN. [expr] (c) The InfoNCE bound says I(X;Y)≥log⁡N−LI(X; Y) \ge \log N - L. Give the bound in nats for N=1024N = 1024 and L=2L = 2. [number] (d) SigLIP’s loss for one pair is ℓ=−log⁡σ(z(ts+b))\ell = -\log \sigma\big(z (t s + b)\big) with label z=±1z = \pm 1. Give ∂ℓ/∂s\partial \ell / \partial s for a positive pair (z=1z = 1) with t=10t = 10, b=−10b = -10, s=1s = 1. [number]

Answer

(a) log⁡(1+3e−2)\log(1 + 3e^{-2}), (b) log⁡N\log N, (c) ln⁡1024−2≈4.93\ln 1024 - 2 \approx 4.93 nats, (d) −5-5. (a) L=−log⁡e2e2+3=log⁡e2+3e2=log⁡(1+3e−2)L = -\log \frac{e^2}{e^2 + 3} = \log \frac{e^2 + 3}{e^2} = \log(1 + 3e^{-2}). (b) Equal logits give the positive probability 1/N1/N, so L=log⁡NL = \log N: the loss of a model that knows nothing. (c) log⁡N\log N in nats is ln⁡1024=10ln⁡2≈6.93\ln 1024 = 10 \ln 2 \approx 6.93, minus 2. (d) u=z(ts+b)=10−10=0u = z(ts + b) = 10 - 10 = 0, and ∂ℓ/∂s=−tz σ(−u)=−10⋅12=−5\partial \ell/\partial s = -tz\,\sigma(-u) = -10 \cdot \frac12 = -5 (Problem 22). Common mistakes: log⁡(3e−2)\log(3e^{-2}) in (a) (the positive left out of the denominator), 00 or log⁡(N−1)\log(N - 1) in (b), 10−210 - 2 in (c) (log⁡2\log_2 mixed into a bound in nats), and −1/2-1/2 in (d) (the temperature tt dropped from the chain rule).

Problem 21. Prove that the gradient of the InfoNCE loss of Problem 20 with respect to the logits is ∂L/∂sj=softmax⁡(s)j−[j=1]\partial L / \partial s_j = \operatorname{softmax}(s)_j - [j = 1], and conclude that L>0L > 0 for every finite ss and that L→0L \to 0 only when s1−sj→∞s_1 - s_j \to \infty for every j≠1j \ne 1. [proof]

Answer

Claim. For L(s)=−log⁡es1∑keskL(s) = -\log \frac{e^{s_1}}{\sum_k e^{s_k}} with N≥2N \ge 2 finite logits, ∂L/∂sj=softmax⁡(s)j−[j=1]\partial L/\partial s_j = \operatorname{softmax}(s)_j - [j = 1]; L>0L > 0; and L→0L \to 0 exactly when s1−sj→∞s_1 - s_j \to \infty for every j≠1j \ne 1.

Proof. Write L=−s1+log⁡∑keskL = -s_1 + \log \sum_k e^{s_k}. The first term contributes −[j=1]-[j = 1]; by the chain rule ∂∂sjlog⁡∑kesk=esj/∑kesk=softmax⁡(s)j\frac{\partial}{\partial s_j} \log \sum_k e^{s_k} = e^{s_j}/\sum_k e^{s_k} = \operatorname{softmax}(s)_j. So ∂L/∂sj=softmax⁡(s)j−[j=1]\partial L/\partial s_j = \operatorname{softmax}(s)_j - [j = 1]: prediction minus one-hot label, the cross-entropy gradient with the positive as the label.

Positivity: L=−log⁡softmax⁡(s)1L = -\log \operatorname{softmax}(s)_1, and for finite logits every esk>0e^{s_k} > 0, so with N≥2N \ge 2, softmax⁡(s)1<1\operatorname{softmax}(s)_1 < 1 and L>0L > 0.

When the loss vanishes: dividing numerator and denominator by es1e^{s_1} gives L=log⁡(1+∑j≠1esj−s1)L = \log\big(1 + \sum_{j \ne 1} e^{s_j - s_1}\big). The sum has positive terms, so L→0L \to 0 iff every esj−s1→0e^{s_j - s_1} \to 0 iff s1−sj→∞s_1 - s_j \to \infty for every j≠1j \ne 1.

A complete proof writes the loss as log-sum-exp, differentiates both terms, shows strict positivity from softmax⁡(s)1<1\operatorname{softmax}(s)_1 < 1, and rewrites LL to read off the limit. Model proof also in q21.md.

Problem 22. Prove that for SigLIP’s loss ℓ=log⁡(1+e−z(ts+b))\ell = \log\big(1 + e^{-z(t s + b)}\big), ∂ℓ/∂s=−tz σ(−z(ts+b))\partial \ell / \partial s = -t z\, \sigma\big(-z(t s + b)\big), and explain why, unlike Problem 21, the gradient of one pair does not depend on the logits of the other pairs in the batch. [proof]

Answer

Claim. For ℓ(s)=log⁡(1+e−z(ts+b))\ell(s) = \log(1 + e^{-z(ts + b)}) with z∈{1,−1}z \in \{1, -1\}, t>0t > 0, ∂ℓ/∂s=−tz σ(−z(ts+b))\partial \ell/\partial s = -tz\,\sigma(-z(ts + b)), and a pair’s gradient involves no other pair’s logit.

Proof. Let u=z(ts+b)u = z(ts + b), so ℓ=log⁡(1+e−u)\ell = \log(1 + e^{-u}) and du/ds=ztdu/ds = zt. Then dℓdu=−e−u1+e−u=−1eu+1=−σ(−u)\frac{d\ell}{du} = \frac{-e^{-u}}{1 + e^{-u}} = -\frac{1}{e^u + 1} = -\sigma(-u). By the chain rule, ∂ℓ/∂s=−tz σ(−z(ts+b))\partial \ell/\partial s = -tz\,\sigma(-z(ts + b)). (Since −log⁡σ(u)=log⁡(1+e−u)-\log \sigma(u) = \log(1 + e^{-u}), this is the loss as SigLIP writes it.)

Independence: SigLIP’s batch loss is ∑i,jℓ(sij,zij)\sum_{i,j} \ell(s_{ij}, z_{ij}), and term (i,j)(i, j) depends only on sijs_{ij}, zijz_{ij}, tt, and bb. Differentiating with respect to sijs_{ij} leaves exactly that one term. In InfoNCE (Problem 21) the softmax normalizer ∑kesk\sum_k e^{s_k} couples every logit of a row, so each gradient depends on all of them; that is why softmax-based contrastive losses need the whole similarity row on one device, and the sigmoid loss shards cheaply.

A complete proof sets up uu, differentiates the softplus, applies the chain rule, notes the loss is a sum of independent per-pair terms, and contrasts it with the softmax normalizer. Model proof also in q22.md.

Problem 23. The word error rate is WER=(S+D+I)/Nref\text{WER} = (S + D + I) / N_\text{ref}, the minimum number of substitutions, deletions, and insertions turning the reference into the hypothesis, over the number of reference words. Give the WER of the hypothesis “oh hello there” against the reference “hello”. [number]

Answer

2. The reference has one word; the hypothesis matches “hello” and adds two words, so S=D=0S = D = 0, I=2I = 2, and WER =2/1=2= 2/1 = 2. WER can exceed 1 because insertions are counted against the reference length. A common mistake is 2/32/3, which divides by the hypothesis length.

Problem 24. Prove that WER is not a metric on word sequences, by showing it is not symmetric with an example (compute both directions), and state which metric axiom fails and why. [proof]

Answer

Claim. WER(r,h)=d(r,h)/∣r∣\text{WER}(r, h) = d(r, h)/|r|, with dd the word edit distance and ∣r∣|r| the reference length, is not a metric, because it is not symmetric.

Proof. Take $a = $ “the cat” and $b = $ “the”. With aa as reference and bb as hypothesis, the cheapest alignment matches “the” and deletes “cat”: d=1d = 1, WER =1/2= 1/2. With bb as reference and aa as hypothesis, match “the” and insert “cat”: d=1d = 1 (no alignment costs 0, since the lengths differ), WER =1/1=1= 1/1 = 1. So WER(a,b)=1/2≠1=WER(b,a)\text{WER}(a, b) = 1/2 \ne 1 = \text{WER}(b, a), and the symmetry axiom d(x,y)=d(y,x)d(x, y) = d(y, x) fails.

The cause is the normalizer: the edit distance itself is symmetric (an insertion one way is a deletion the other, and substitutions are symmetric), so the numerator is the same both ways, but the denominator is the length of whichever sequence plays the reference. The same normalization is why WER can exceed 1 (Problem 23).

A complete proof computes both directions of one example, names symmetry as the failing axiom, and locates the cause in the denominator. Model proof also in q24.md.

The wrong answers in this set cluster into a few patterns, each tied to the problem where it shows up.

MistakeSymptomHow to avoid it
Dropping the +1+1 in the size formula1499 positions where Whisper’s encoder has 1500apply the full formula with its +1+1 (Problem 1)
Ignoring dilation in the kernel spana dilated layer sized like a dense onesize the kernel as d(k−1)+1d(k - 1) + 1 (Problem 1(d), Problem 2)
Flipping the kernel in a network convcorrelation and convolution swappedkeep the kernel unflipped in each Toeplitz row (Problems 4 and 5)
Counting FLOPs where MACs were asked (or the reverse)a factor 2 in every cost estimatecount one MAC per weight per output (Problem 8(c))
Forgetting the CLS token196 positions where the encoder has 197add the CLS token after counting patches (Problem 9(c))
Strides in elements, or in memory order instead of per logical axisa kernel indexing the wrong neighbourmultiply by the itemsize and list strides per logical axis (Problem 10)
Folding once instead of to the nearest multiple of the rate9 kHz instead of 7 kHzfold to the representative in [0,fN][0, f_N] (Problem 11(d))
The bilinear weight where the cubic was asked, or the wrong aainterpolation that does not match Pillow or PyTorchevaluate Keys’ kernel with the library’s aa (Problem 13)
Forgetting the antialias stretchsupport 1 where Pillow uses 4stretch the support by the scale factor (Problem 14)
The inverse DFT’s signa mirrored spectrumuse e−2πijk/ne^{-2\pi i jk/n} for the forward transform (Problem 15(c))
Treating 400 as 24⋅522^4 \cdot 5^2 stagessix radix-2 and radix-5 stages instead of fourcount one stage per named radix (Problem 17(a))
Counting frames without the centered padding’s extra frame3000 where Whisper computes 3001use 1+⌊n/hop⌋1 + \lfloor n/\text{hop} \rfloor for centered frames (Problem 18(a))
∑w\sum w instead of ∑w2\sum w^2 for weighted overlap-addreconstructions scaled by 3/4overlap-add the squared window (Problem 18(d))
The symmetric Hann window(0,3/4,3/4,0)(0, 3/4, 3/4, 0)divide by nn, not n−1n - 1, in the cosine (Problem 18(f))
20 log⁡10\log_{10} for a powerdecibels doubleduse 10log⁡1010 \log_{10} for power (Problem 19(d))
Applying only the floor in Whisper’s scaling−3/2-3/2 where the 8-unit clip gives −3/4-3/4floor, then clip at max⁡L−8\max L - 8, then rescale (Problem 19(e))
Mixing log⁡2\log_2 into a nats bound10−210 - 2 instead of ln⁡1024−2\ln 1024 - 2keep every logarithm natural (Problem 20(c))
Dividing WER by the hypothesis length2/32/3 where the answer is 2divide by the reference length (Problem 23)

Each group of problems checks one section of this chapter: conv sizes, the Toeplitz form, im2col and col2im (Problems 1 to 9) check Convolution as a linear map; strides and patches (Problem 10) check Image tensors and layouts; aliasing, interpolation kernels, and antialias support (Problems 11 to 14) check Resampling and interpolation; DFT properties and FFT counts (Problems 15 to 17) check DFT and FFT; and frames, windows, mel, and Whisper’s scaling (Problems 18 and 19) check Sampling, Nyquist, windows, and the STFT and Mel scale and filterbanks.

The same arithmetic carries forward into AI Engineering. Conv layers and pooling on autograd and the Vision transformer encoder build conv layers and the ViT patch embedding; Contrastive image-text: CLIP and SigLIP trains with the losses of Problems 20 to 22; and the audio frontend, the Whisper encoder, and Multimodal eval metrics use the frames and features of Problems 18 and 19 and report the WER of Problems 23 and 24.

  • Dumoulin and Visin, “A guide to convolution arithmetic for deep learning” (free).
  • Oppenheim and Schafer, Discrete-Time Signal Processing, ch. 4 and 8.
  • van den Oord, Li, Vinyals, “Representation learning with contrastive predictive coding” (2018).
  • Zhai et al., “Sigmoid loss for language image pre-training” (ICCV 2023).
  • Jurafsky and Martin, Speech and Language Processing, ch. 2.5 (edit distance, free).
  • One formula sizes every conv, pool, and patch embedding, and receptive fields grow by (k−1)(k - 1) times the product of earlier strides (Problems 1 to 3).
  • A conv is a Toeplitz matrix; its transpose (col2im) is the backward pass, and the backward of a correlation is a true convolution (Problems 4 to 7).
  • Sampling keeps only frequencies below half the rate; everything above folds back, in time and in images (Problems 11 and 12).
  • The DFT is a unitary change of basis: Parseval, conjugate symmetry for real signals, and the convolution theorem with zero padding (Problems 15 and 16); an FFT needs radices that divide nn (Problem 17).
  • InfoNCE is cross-entropy with the positive as the label, bounded below by log⁡N−I\log N - I; SigLIP’s sigmoid loss decouples the pairs (Problems 20 to 22). WER is an edit distance over the reference length: it can exceed 1 and is not symmetric (Problems 23 and 24).