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.
- patch embedding: a convolution with kernel equal to stride, equivalent to one linear map applied to each flattened patch. Read more: 14.3 Vision transformer encoder in AI Engineering
- word error rate (WER): pooled substitutions, deletions and insertions divided by the total reference word count. Read more: 15.5 Multimodal eval metrics: WER, CER, VQA, captions, zero-shot in AI Engineering
- receptive field: the region of the input that a given neuron depends on, which grows with depth. Read more: Chapter 3: Neural Architectures and Their History in AI Engineering
- col2im: the adjoint of im2col, which sums column entries back into the pixels they came from. Read more: 18.1 Convolution as a linear map
- im2col: copying every receptive field into a column, ordered , so a convolution becomes one matmul. Read more: 18.1 Convolution as a linear map
- Interpolation kernel: a function , 1 at 0 and 0 at other integers, that turns samples into a continuous signal via . Read more: 18.3 Resampling and interpolation
- DFT: multiplication by , writing samples as coefficients of complex sinusoids. Read more: 18.4 DFT and FFT
- Conjugate symmetry: for real input, which lets
rfftkeep only bins. Read more: 18.4 DFT and FFT - STFT: the DFT of overlapping windowed frames, giving a spectrum per time step. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
- InfoNCE: a softmax cross-entropy that picks the matching item from a batch of candidates; CLIP applies it in both directions. Read more: 14.5 Contrastive image-text: CLIP and SigLIP in AI Engineering
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives
- Nyquist frequency: , the highest frequency a sample rate can represent. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- Mutual information: , the nats that knowing one variable saves about the other. Read more: 17.3 Mutual information, PMI, and PPMI
- Edit distance: the minimum number of insertions, deletions, and substitutions that turn one string into another, computed by a two-sequence DP over pairs of prefixes in . Read more: Chapter 9: Dynamic programming in Programming and Algorithms
- Cross-correlation: sliding a kernel over a signal and taking a dot product at each position, with no flip; what deep learning calls convolution. Read more: 18.1 Convolution as a linear map
- Upstream gradient: , the gradient arriving at an op’s output. Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs
- Linear map: a function between vector spaces with ; once bases are chosen, every linear map is a matrix. Read more: Chapter 9: Linear Algebra
- Inner product: . Read more: 9.6 Inner products, projections, cosine similarity, and top-k
- CLS token: a learned vector prepended to the sequence whose output summarizes the image. Read more: 14.3 Vision transformer encoder in AI Engineering
- Channels last (NHWC): a memory order in which the channel is the fastest axis; the logical shape can stay NCHW. Read more: 18.2 Image tensors and layouts
- Lanczos3: the windowed sinc on . Read more: 18.3 Resampling and interpolation
- Periodic Hann window: , one period of a cosine of the DFT length. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Chain rule: . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- Log-sum-exp (LSE): , computed stably as . Read more: 15.2 Stable numerics: logsumexp, softmax, and compensated sums
- softplus: , a smooth relu whose derivative is . Read more: 7.3 Activation Functions and Their Derivatives
- Representation learning: learning the features themselves, not just a decision rule over given features. Read more: Chapter 2: Deep Learning in AI Engineering
- Change of basis: rewriting a matrix in new coordinates as ; the transformation is the same, only its description changes. Read more: Chapter 9: Linear Algebra
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 ideas you need
Section titled “The ideas you need”The problems use the following symbols.
| Symbol | Meaning | Type / shape |
|---|---|---|
| input length, kernel size, stride, padding, dilation | ints | |
| Toeplitz matrix of a 1-D correlation | matrix | |
| , | sample rate and Nyquist frequency | Hz |
| , | interpolation kernel and Keys’ parameter | function, float |
| DFT | vector | |
| , | STFT frame length and hop | ints |
| mel value | mel | |
| , | contrastive logits and candidate count | floats, int |
| logistic sigmoid | function | |
| substitutions, deletions, insertions | ints |
Convolution arithmetic and the conv as a matrix. . A stack of layers sees input samples. The correlation has the kernel along each row of , shifted one column per row; 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 sampled at is indistinguishable from for any integer ; it appears at the representative in . Keeping every -th pixel divides the rate by , so frequencies above cycles per input pixel alias. Antialiased resizing stretches the kernel by the scale factor to remove them first (see Resampling and interpolation).
Fourier. . . Real input gives ; real and even input gives a real spectrum. The product of DFTs is the DFT of the circular convolution; padding to length makes it linear. An FFT recursion splits by its factors (see DFT and FFT). Centered STFT frames number , bins are apart, and a periodic Hann at hop overlap-adds its square to (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 . Whisper’s feature is with (see Mel scale and filterbanks).
Contrastive losses. CLIP scores a batch of image-text pairs with a similarity matrix and, for each image, classifies its own caption among the with a softmax: InfoNCE, . Its gradient is softmax minus one-hot, and van den Oord et al. show : more negatives can certify more mutual information. SigLIP replaces the softmax by an independent logistic loss per pair, with on the diagonal and 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.
Five warm-up examples
Section titled “Five warm-up examples”Before the problems, here is one small example of each kind, worked in full.
Conv. , , , : . The window starts sit at in input coordinates, and the next one () would need input sample 11.
Aliasing. 9 kHz sampled at 16 kHz: Hz, below , so it appears at 7 kHz.
DFT. : ; only survives, , and Parseval gives .
InfoNCE. Two candidates with equal logits: ; with the positive ahead by , .
WER. Reference “a b c”, hypothesis “a x c d”: one substitution and one insertion, WER .
The problems
Section titled “The problems”Answers are exact (for example , 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.
Convolution arithmetic
Section titled “Convolution arithmetic”Problem 1. Give the output length for (a) , , , (a ResNet stem); (b) a max pool with , on that output, along one axis; (c) Whisper’s stem on 3000 frames: a conv with , , , then one with , , ; (d) , , , , . [number]
Answer
(a) 112, (b) 56, (c) 1500, (d) 24. Apply each time. (a) : the stem halves the image. (b) On : . (c) The first conv keeps the length, ; the second gives , which is why Whisper’s encoder has 1500 positions. (d) The dilated kernel spans samples, so . Common mistakes: dropping the (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 with dilation span? Answer in and . (b) For odd , stride 1, and dilation 1, which padding keeps the output length equal to the input length? Answer in . [expr]
Answer
(a) , (b) . (a) The taps are apart, so there are gaps of between the first and the last tap, plus the first sample itself. (b) With the formula gives ; setting it equal to gives , an integer for odd (“same” padding: 1 for , 3 for ). A common mistake is in (a), which counts a gap after the last tap, and in (b), which is not even an integer for odd .
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 convs with stride 1; (b) a conv with stride 2 followed by a conv with stride 1. [number]
Answer
(a) 7, (b) 7. Use . (a) Three layers of at stride 1: . (b) The first layer adds ; 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 : . A common mistake in (a) is , 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. and , no padding. Give (a) the cross-correlation and (b) the true convolution (kernel flipped). [vector]
Answer
(a) , (b) . There are outputs. (a) , . (b) The true convolution slides the flipped kernel : and . Neural-network “convs” are cross-correlations; confusing the two swaps the answers.
Problem 5. For the cross-correlation of Problem 4 written as : (a) give . [matrix] (b) Give the input gradient for the upstream gradient . [vector] (c) Is equal to the full (zero-padded) true convolution of with ? [bool]
Answer
(a) , (b) , (c) true. (a) Row holds the kernel starting at column : a Toeplitz matrix. (b) is the sum of the rows weighted by : ; it has the input’s length 4, not the output’s. (c) The full true convolution of with is , 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 and as in Problems 4 and 5, give the weight gradient . [vector] (b) A image goes through im2col with a kernel, stride 1, no padding. How many times is its center pixel copied into the columns? [number]
Answer
(a) , (b) 4. (a) : , , . It is the correlation of the input with the gradient; reversing it to is a common mistake. (b) A kernel on a image has 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 and every column array of the matching shape, , 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 and every column array of the matching shape, .
Proof. Index column entries by . im2col copies one value into each entry: with padding , stride , and dilation , entry reads the input position , and is 0 when that position falls in the padding. Let be the set of entries whose is a real pixel. Then Every reads exactly one pixel, so grouping the terms by the pixel partitions : 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 , and the sum is . Since this holds for all and , 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.
Parameter and FLOP counts
Section titled “Parameter and FLOP counts”Problem 8. A conv from 64 to 128 channels with a kernel, groups 1, and a bias. Give (a) its parameter count; (b) the parameter count of a depthwise conv on 64 channels with a bias; (c) the multiply-accumulates of the first conv for one image with a output. [number]
Answer
(a) 73856, (b) 640, (c) 231211008. (a) Weights , plus 128 biases. (b) Depthwise means one filter per channel: . (c) Each of the outputs takes multiply-accumulates: (the bias is an add, not a MAC). Common mistakes: 73728 forgets the bias; 36928 in (b) is a full conv; 462422016 in (c) counts FLOPs (two per MAC) where MACs were asked.
Problem 9. ViT-B/16 on a 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) patches per side, . (b) The patch embedding is a conv from 3 to 768 channels with a kernel: weights plus 768 biases. (c) 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).
Layouts and strides
Section titled “Layouts and strides”Problem 10. A float32 tensor with logical shape . (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 in the NHWC storage. [number] (d) Give the length of one flattened patch when a image is cut into patches. [number]
Answer
(a) , (b) , (c) 476, (d) 588. float32 is 4 bytes. (a) NCHW is row-major in : element strides , times 4 bytes. (b) NHWC stores row-major: element strides , , , ; listed per logical axis and times 4 bytes, . (c) bytes. (d) A patch over 3 channels flattens to values. Common mistakes: strides in elements ; the NHWC strides in memory order instead of per logical axis; and 196 in (d), which forgets the channels.
Sampling and aliasing
Section titled “Sampling and aliasing”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) . For the others, take the representative of in . (b) . (c) . (d) is still above 8000; the nearest multiple of the rate is , and . 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 cycle per input pixel. Keeping every fourth pixel samples at 1 per 4 input pixels, i.e. the frequency becomes cycles per output pixel, which aliases to : stripes with a period of 3 output pixels (12 input pixels), a moiré. (b) The output grid’s Nyquist frequency is cycle per input pixel, and , so no. (c) That Nyquist limit: . (d) The checkerboard is ; keeping even and even makes always even, so every kept pixel has the same value: a constant image. Common mistakes: in (a) (the period divided by 4, as if the pattern were simply shrunk), or in (c) (the input’s Nyquist, or the scale itself), and “a checkerboard” in (d).
Interpolation kernels
Section titled “Interpolation kernels”Problem 13. Keys’ cubic kernel is for and for . Give (a) and (b) with (Pillow), and (c) with (PyTorch). [number]
Answer
(a) , (b) , (c) . (a) , : . (b) The outer piece at is , so . (c) The same with : . The negative lobe is what makes cubic sharper than bilinear. Common mistakes: in (a), the bilinear weight; in (b), a sign slip; and in (c), Pillow’s 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, . (c) Give the support radius of Lanczos3 when shrinking by 2. [number]
Answer
(a) 4, (b) 9, (c) 6. Shrinking by a factor stretches the kernel by so that it also low-passes below the new Nyquist. (a) , so bilinear’s support 1 becomes 4 input pixels. (b) 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.
The DFT
Section titled “The DFT”Problem 15. With , give the DFT of (a) , (b) , (c) . [vector] (d) A real signal of length 8 has . Give . [number]
Answer
(a) , (b) , (c) , (d) . (a) An impulse at 0 has for every . (b) A constant has all its energy at : , and the other sums of roots of unity are 0. With this unnormalized convention, is the sum, not the mean. (c) An impulse at gives . (d) For real input , and , so ; this symmetry is why rfft keeps only bins. Common mistakes: in (c), the inverse transform’s sign, and in (d).
Problem 16. (a) Give for . (b) Give the circular convolution of length 4 of and . [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 () real? [bool]
Answer
(a) 24, (b) , (c) 399, (d) true. (a) Parseval: . (b) The linear convolution is and has length , so nothing wraps around: . (c) The linear convolution has length , and the circular one equals it when the transform length is at least that. (d) Yes: an even real signal has , because the sine terms of and cancel. Common mistakes: 6 in (a) (Parseval without the factor of this convention), a length-5 vector in (b), and 300 in (c).
The FFT
Section titled “The FFT”Problem 17. (a) Into how many radix stages does an FFT split when it uses radices 4, 4, 5, 5? (b) How many complex multiplies does the direct DFT of length 400 take ()? (c) How many butterflies does a radix-2 FFT of length 1024 take, ? [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) , one stage per radix. (b) . (c) . (d) A radix-2 FFT needs a power of 2; padding 400 to 512 computes a different transform, with bins apart instead of , which is why Whisper’s 400-point frame needs mixed radices. Common mistakes: 6 in (a) (the prime factorization , six stages, is a valid but different plan; the question names radices 4, 4, 5, 5); 400 in (b); 10240 in (c) ( instead of half of it).
Windows and the STFT
Section titled “Windows and the STFT”Problem 18. Whisper: 30 s of audio at 16 kHz, , 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 for a periodic Hann window at hop ; (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) . (a) samples; centered framing pads on each side and gives frames, and Whisper drops the last to get 3000. (b) Hz. (c) . (d) The periodic Hann is , so , whose mean over a period is ; overlap-adding at hop stacks 4 copies, . (e) s ms. (f) : . 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 in (f).
Mel and decibels
Section titled “Mel and decibels”Problem 19. On the Slaney scale, give the mel value of (a) 1000 Hz and (b) 6400 Hz. (c) On the HTK scale, : give . (d) Give a power ratio of 1000 in decibels. (e) Whisper computes , then , then . Give the feature of a band with mel power in a clip whose largest is 1. (f) Give the feature value of pure silence. [number]
Answer
(a) 15, (b) 42, (c) , (d) 30, (e) , (f) . (a) , the break point. (b) , one factor of 6.4 above the break: . (c) . (d) dB. (e) ; the clip raises it to ; . (f) Silence floors everywhere at , the maximum is also , the clip does nothing, and . Common mistakes: 96 in (b) (the linear piece used above 1 kHz), in (c), 60 in (d) (the amplitude rule on a power), in (e) (the floor applied but not the clip), and in (f) (the log without the rescale).
Contrastive losses
Section titled “Contrastive losses”Problem 20. InfoNCE for one anchor scores candidates with logits (the positive is ) and takes , the cross-entropy with the positive as the label. (a) Give for . (b) Give when all logits are equal. Answer in . [expr] (c) The InfoNCE bound says . Give the bound in nats for and . [number] (d) SigLIP’s loss for one pair is with label . Give for a positive pair () with , , . [number]
Answer
(a) , (b) , (c) nats, (d) . (a) . (b) Equal logits give the positive probability , so : the loss of a model that knows nothing. (c) in nats is , minus 2. (d) , and (Problem 22). Common mistakes: in (a) (the positive left out of the denominator), or in (b), in (c) ( mixed into a bound in nats), and in (d) (the temperature dropped from the chain rule).
Problem 21. Prove that the gradient of the InfoNCE loss of Problem 20 with respect to the logits is , and conclude that for every finite and that only when for every . [proof]
Answer
Claim. For with finite logits, ; ; and exactly when for every .
Proof. Write . The first term contributes ; by the chain rule . So : prediction minus one-hot label, the cross-entropy gradient with the positive as the label.
Positivity: , and for finite logits every , so with , and .
When the loss vanishes: dividing numerator and denominator by gives . The sum has positive terms, so iff every iff for every .
A complete proof writes the loss as log-sum-exp, differentiates both terms, shows strict positivity from , and rewrites to read off the limit. Model proof also in q21.md.
Problem 22. Prove that for SigLIP’s loss , , 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 with , , , and a pair’s gradient involves no other pair’s logit.
Proof. Let , so and . Then . By the chain rule, . (Since , this is the loss as SigLIP writes it.)
Independence: SigLIP’s batch loss is , and term depends only on , , , and . Differentiating with respect to leaves exactly that one term. In InfoNCE (Problem 21) the softmax normalizer 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 , 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.
Word error rate
Section titled “Word error rate”Problem 23. The word error rate is , 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 , , and WER . WER can exceed 1 because insertions are counted against the reference length. A common mistake is , 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. , with the word edit distance and the reference length, is not a metric, because it is not symmetric.
Proof. Take $a = $ “the cat” and $b = $ “the”. With as reference and as hypothesis, the cheapest alignment matches “the” and deletes “cat”: , WER . With as reference and as hypothesis, match “the” and insert “cat”: (no alignment costs 0, since the lengths differ), WER . So , and the symmetry axiom 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.
Common mistakes
Section titled “Common mistakes”The wrong answers in this set cluster into a few patterns, each tied to the problem where it shows up.
| Mistake | Symptom | How to avoid it |
|---|---|---|
| Dropping the in the size formula | 1499 positions where Whisper’s encoder has 1500 | apply the full formula with its (Problem 1) |
| Ignoring dilation in the kernel span | a dilated layer sized like a dense one | size the kernel as (Problem 1(d), Problem 2) |
| Flipping the kernel in a network conv | correlation and convolution swapped | keep 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 estimate | count one MAC per weight per output (Problem 8(c)) |
| Forgetting the CLS token | 196 positions where the encoder has 197 | add the CLS token after counting patches (Problem 9(c)) |
| Strides in elements, or in memory order instead of per logical axis | a kernel indexing the wrong neighbour | multiply by the itemsize and list strides per logical axis (Problem 10) |
| Folding once instead of to the nearest multiple of the rate | 9 kHz instead of 7 kHz | fold to the representative in (Problem 11(d)) |
| The bilinear weight where the cubic was asked, or the wrong | interpolation that does not match Pillow or PyTorch | evaluate Keys’ kernel with the library’s (Problem 13) |
| Forgetting the antialias stretch | support 1 where Pillow uses 4 | stretch the support by the scale factor (Problem 14) |
| The inverse DFT’s sign | a mirrored spectrum | use for the forward transform (Problem 15(c)) |
| Treating 400 as stages | six radix-2 and radix-5 stages instead of four | count one stage per named radix (Problem 17(a)) |
| Counting frames without the centered padding’s extra frame | 3000 where Whisper computes 3001 | use for centered frames (Problem 18(a)) |
| instead of for weighted overlap-add | reconstructions scaled by 3/4 | overlap-add the squared window (Problem 18(d)) |
| The symmetric Hann window | divide by , not , in the cosine (Problem 18(f)) | |
| 20 for a power | decibels doubled | use for power (Problem 19(d)) |
| Applying only the floor in Whisper’s scaling | where the 8-unit clip gives | floor, then clip at , then rescale (Problem 19(e)) |
| Mixing into a nats bound | instead of | keep every logarithm natural (Problem 20(c)) |
| Dividing WER by the hypothesis length | where the answer is 2 | divide by the reference length (Problem 23) |
Where this leads
Section titled “Where this leads”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.
Further reading
Section titled “Further reading”- 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).
Summary
Section titled “Summary”- One formula sizes every conv, pool, and patch embedding, and receptive fields grow by 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 (Problem 17).
- InfoNCE is cross-entropy with the positive as the label, bounded below by ; 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).