Exercise: solving with ℓ1
Before this unit: Sparsity and the ℓ1 penalty (previous unit); Day 7: Wavelets; Day 5: Deconvolution.
See also: Day 8: Image compression; Day 9: Radon transform and tomography; Day 28: Ising model and Markov random fields.
Goal
By the end of this exercise you should be able to:
- run ISTA by hand on a small problem and explain each half-step,
- choose a safe step size and say what FISTA’s momentum buys,
- run orthogonal matching pursuit by hand and explain when it fails,
- write deconvolution and inpainting with a sparsity prior as one problem with different forward operators,
- (math) derive the soft threshold as a proximal operator and ISTA as minimising an upper bound.
Background
| Idea | What it means |
|---|---|
| Problem | \min_{\mathbf x} \tfrac12\|A\mathbf x - \mathbf y\|^2 + λ\|\mathbf x\|_1; A = forward operator × synthesis basis |
| ISTA step | \mathbf z = \mathbf x + τA^\top(\mathbf y - A\mathbf x) (Landweber), then \mathbf x = \mathrm{soft}(\mathbf z, τλ) |
| Step size | τ ≤ 1/L, L = largest eigenvalue of A^\top A |
| FISTA | the ISTA step from an extrapolated point; error ∝ 1/k² instead of 1/k |
| Proximal step | \mathrm{prox}_{τg}(\mathbf v) = \arg\min \tfrac12\|\mathbf x - \mathbf v\|^2 + τg(\mathbf x): soft threshold for ℓ1, clipping for x \ge 0 |
| OMP | pick the column most correlated with the residual, refit all chosen columns by least squares, repeat |
| Inpainting | A = mask that keeps the known pixels; the missing pixels are its null space |
By hand
B1. Two ISTA iterations. A = \begin{pmatrix}1 & 0.5\\ 0.5 & 1\end{pmatrix}, \mathbf y = (2, 1), λ = 0.5, τ = 0.4, \mathbf x_0 = (0, 0). (a) Compute \mathbf z_1 and \mathbf x_1, then \mathbf z_2 and \mathbf x_2. (b) The iterates continue (1.048, 0.506), (1.122, 0.434), (1.187, 0.368), … and converge to (1.6, 0). Check that (1.6, 0) satisfies the lasso conditions: compute A^\top(\mathbf y - A\mathbf x) there. (c) Compute the ridge solution (A^\top A + λI)^{-1}A^\top\mathbf y for λ = 0.5. Does it have a zero? (d) Compare the objectives \tfrac12\|A\mathbf x - \mathbf y\|^2 + λ\|\mathbf x\|_1 of (1.6, 0) and of the truth (2, 0).
B2. OMP on the same problem. (a) Compute the correlations \mathbf a_j^\top\mathbf y and the normalised ones \mathbf a_j^\top\mathbf y/\|\mathbf a_j\|. Which atom is picked? (b) Refit and compute the residual. Does OMP stop? (c) Why does OMP give (2, 0) and ISTA (1.6, 0)?
B3. Step size. (a) The eigenvalues of A^\top A for B1 are 0.25 and 2.25. What is the largest safe step? Was τ = 0.4 safe? (b) A normalised blur (\sum kernel = 1, so \max|H| = 1). Which τ are safe? What does a mask that keeps some pixels give?
B4. Inpainting, counting. An 8-sample signal is a constant plus one cosine: \mathbf x = a\,\mathbf d_0 + b\,\mathbf d_k (\mathbf d_k = DCT basis vectors). Only the samples 0, 2, 3 and 6 are known. (a) If you know that k = 3, how many unknowns and how many equations? Can you find a and b? (b) If k is unknown, how many two-atom supports \{0, k\} are possible? Why can sparsity still pick the right one? (c) The known samples of \mathbf x = 2\mathbf d_0 + \mathbf d_3 are 1.123, 0.217, 0.429, 0.805. OMP on the 8 masked DCT atoms first picks atom 0 (normalised correlation 1.287, just ahead of atom 5 with 1.281), refits a = 1.82, then picks atom 3 and refits to a = 2, b = 1 with residual 0. What does the near tie in the first step tell you?
B5. Predict. A blurred profile with a step, a ramp and a narrow spike is restored with Tikhonov (smoothness), TV and a wavelet ℓ1 prior. Sketch what each does to (i) the step, (ii) the ramp, (iii) the spike.
B6. A colleague runs ISTA on a deblurring problem for 20 iterations and reports that “ℓ1 does not work: the result is still blurred”. What would you check?
B1. (a) A\mathbf x_0 = 0, A^\top\mathbf y = (2 + 0.5, 1 + 1) = (2.5, 2), \mathbf z_1 = 0.4 \cdot (2.5, 2) = (1.0, 0.8); threshold at τλ = 0.2: \mathbf x_1 = (0.8, 0.6). Then A\mathbf x_1 = (1.1, 1.0), \mathbf y - A\mathbf x_1 = (0.9, 0), A^\top(0.9, 0) = (0.9, 0.45), \mathbf z_2 = (0.8, 0.6) + 0.4 \cdot (0.9, 0.45) = (1.16, 0.78), \mathbf x_2 = (0.96, 0.58). (b) A\mathbf x = (1.6, 0.8), \mathbf r = (0.4, 0.2), A^\top\mathbf r = (0.5, 0.4). For the active x_1 > 0: 0.5 = λ ✓. For x_2 = 0: |0.4| \le 0.5 ✓. So (1.6, 0) is the minimum. (c) A^\top A + 0.5I = \begin{pmatrix}1.75 & 1\\ 1 & 1.75\end{pmatrix}, right-hand side (2.5, 2): solution (1.152, 0.485). No zero. (d) At (1.6, 0): ½(0.16 + 0.04) + 0.5 · 1.6 = 0.9. At (2, 0): 0 + 1.0 = 1.0. The lasso prefers the shrunk value: it trades a little misfit for a smaller penalty.
B2. (a) \mathbf a_1^\top\mathbf y = 2.5, \mathbf a_2^\top\mathbf y = 2; both columns have norm \sqrt{1.25} = 1.118: normalised 2.236 and 1.789. Atom 1. (b) x_1 = \mathbf a_1^\top\mathbf y/\|\mathbf a_1\|^2 = 2.5/1.25 = 2; residual \mathbf y - 2\mathbf a_1 = (2, 1) - (2, 1) = 0: stop. (c) Both find the support {1}; OMP refits without penalty (exact for noise-free data), ℓ1 keeps the shrinkage λ·(something) on the kept coefficient.
B3. (a) L = 2.25, τ ≤ 1/2.25 = 0.444; τ = 0.4 is safe. (b) L = 1, τ ≤ 1. A mask also has L = 1 (it keeps or deletes, never amplifies).
B4. (a) Two unknowns (a, b), four equations; the 4 × 2 system has rank 2, so least squares gives a, b exactly. (b) Seven (k = 1 … 7). With only 4 samples a general 8-coefficient signal cannot be found, but among the few-atom explanations usually only one fits all four samples; sparsity picks that one. (c) The first choice was almost a coin toss: atoms 0 and 5 explain the four known samples almost equally well. With fewer known samples or more noise OMP could have committed to atom 5 (the failure mode of B2’s slide); here the refit after picking atom 0 makes atom 3 the clear winner (0.684 against 0.612).
B5. Tikhonov: the step stays rounded with ripples on both sides, the ramp is fine, the spike becomes a low, wide bump. TV: the step becomes sharp, the ramp a staircase, the spike a short plateau of reduced height. Wavelet ℓ1: the step sharper than Tikhonov with small ripples, the ramp smooth, the spike reduced. On the lecture’s profile: RMSE 0.111 (Tikhonov), 0.089 (TV), 0.108 (wavelet ℓ1).
B6. Whether ISTA has converged (20 iterations are far too few with a blur: compare the objective with that after 500 iterations, or use FISTA); the step size (τ = 1/L); whether λ is too large (everything is shrunk away, check against λ_max); and whether the basis is suitable.
Math questions
B7. (math) Show that \mathrm{prox}_{τλ|\cdot|}(v) = \mathrm{sign}(v)\max(|v| - τλ, 0), and that the proximal operator of the indicator of \{x \ge 0\} is \max(v, 0).
B8. (math) Let h(\mathbf x) = \tfrac12\|A\mathbf x - \mathbf y\|^2 and L \ge \|A\|^2. Show h(\mathbf x) \le h(\mathbf x_k) + \nabla h(\mathbf x_k)^\top(\mathbf x - \mathbf x_k) + \tfrac L2\|\mathbf x - \mathbf x_k\|^2, and that minimising the right-hand side plus λ‖x‖₁ gives one ISTA step with τ = 1/L. Conclude that the objective never increases.
B9. (math) Show that a fixed point of ISTA, \mathbf x = \mathrm{soft}(\mathbf x + τA^\top(\mathbf y - A\mathbf x), τλ), satisfies the lasso optimality conditions.
B10. (math) Show that after the least-squares refit in OMP the residual is orthogonal to every chosen column, so OMP never picks the same column twice.
B7. Minimise \tfrac12(x - v)^2 + τλ|x|: see the quiz Q9 or the sparsity unit (three cases x > 0, x < 0, x = 0; the last is optimal iff |v| \le τλ). For the indicator: minimise \tfrac12(x - v)^2 over x \ge 0: the unconstrained minimum v if v \ge 0, otherwise the boundary x = 0.
B8. h(\mathbf x) = h(\mathbf x_k) + \nabla h(\mathbf x_k)^\top\mathbf d + \tfrac12\mathbf d^\top A^\top A\mathbf d exactly, with \mathbf d = \mathbf x - \mathbf x_k (a quadratic). Since \mathbf d^\top A^\top A\mathbf d \le \|A\|^2\|\mathbf d\|^2 \le L\|\mathbf d\|^2, the bound holds, with equality at \mathbf d = 0. Completing the square: bound $= L2|x - (x_k - 1Lh(x_k))|^2 + $ const, so minimising bound + λ‖x‖₁ is \mathrm{prox}_{(λ/L)\|\cdot\|_1} of the gradient step: ISTA with τ = 1/L. Then f(\mathbf x_{k+1}) \le bound(\mathbf x_{k+1}) + λ\|\mathbf x_{k+1}\|_1 \le bound(\mathbf x_k) + λ\|\mathbf x_k\|_1 = f(\mathbf x_k).
B9. Let \mathbf g = A^\top(\mathbf y - A\mathbf x). If x_j \ne 0, the soft threshold gives x_j = x_j + τg_j - τλ\,\mathrm{sign}(x_j), so g_j = λ\,\mathrm{sign}(x_j). If x_j = 0, then |0 + τg_j| \le τλ, i.e. |g_j| \le λ. These are exactly the lasso conditions \mathbf a_j^\top\mathbf r = λ\,\mathrm{sign}(x_j) and |\mathbf a_j^\top\mathbf r| \le λ.
B10. The refit solves the normal equations A_S^\top(\mathbf y - A_S\mathbf x_S) = 0, i.e. A_S^\top\mathbf r = 0: the correlation of the residual with every chosen column is 0. The next pick maximises |\mathbf a_j^\top\mathbf r|, which is 0 for the chosen columns, so a new column is chosen as long as \mathbf r \ne 0 is not orthogonal to all columns.
Explore: ISTA, FISTA and OMP
A signal of 256 samples, sparse in a dictionary (spikes, Haar, db4, DCT, or spikes ∪ DCT), seen through the identity, a Gaussian blur or a random mask, plus noise. Top: truth (black), data (grey), estimate (blue). Middle: the coefficients (green: on the true support, red: false atoms; open circles: the true values, red if missed) and the band ±τλ. Bottom: the objective gap of ISTA and FISTA against the iteration, and the error against λ/λmax. Step (½ ISTA) shows one iteration in two frames.
E1. Preset identity: ISTA = one soft threshold. Read λ, λmax and the counts after 1 iteration. Raise the iterations to 100: does anything change? Why?
E2. Preset blur: ISTA needs many steps (20 iterations). Read the relative error and the counts. Raise the iterations to 100 and then 1000 (ISTA), and compare with FISTA at 100 (preset FISTA vs ISTA). Use the gap plot.
E3. Press reset on the blur preset and use step (½ ISTA) a few times. Describe what the gradient frame and the threshold frame look like in the coefficient panel.
E4. Preset step size too large: diverges (FISTA, τL = 2.2). What happens? Set τL to 1.5, then 2.0.
E5. Preset OMP is exact here (DCT-sparse, 60 % missing, no noise). Read the error. Switch to FISTA: what is different, and why?
E6. Preset OMP commits to the wrong atom. Which atoms does OMP pick? Switch to FISTA: are the two close spikes found? What is the price?
E7. Preset spikes + sines need both bases. Count the nonzeros. Switch the dictionary to DCT only and to spikes only. Which gives the best reconstruction, and with how many coefficients?
E8. Preset shrinkage bias and debiasing. Read the relative error, tick debias, and read it again.
E1. λ = 0.523, λmax = 2.08; all 10 true spikes found, no false atoms; relative error 0.327. 100 iterations change nothing: with A = I and τ = 1, one ISTA step from 0 is the soft threshold of \mathbf y, which is already the exact solution.
E2. ISTA, 20 iterations: 42 nonzeros (8 on the support, 34 false, 2 true spikes missed), error 0.82. ISTA 100: 27 nonzeros, error 0.72; ISTA 1000: 13 nonzeros, error 0.45. FISTA 100: 14 nonzeros, error 0.45, objective gap 3·10⁻⁵ against 1·10⁻² for ISTA at 100. The same answer, ten times sooner. The two missed spikes lie close to others: the blur merges them.
E3. The gradient frame is a smooth, blurred correction spread over many coefficients, mostly inside the band; the threshold sets everything inside the band ±τλ to 0 and lowers the rest by τλ. Early iterations have small values (the start is 0); the support grows near the true spikes and the false atoms between them slowly disappear.
E4. The objective explodes (gap about 10³⁷ after 50 iterations): all 256 coefficients become huge. At τL = 1.5 FISTA converges (error 0.46); at 2.0 it already diverges. Keep τ ≤ 1/L.
E5. OMP recovers the signal exactly (error 0.000) with the 6 true atoms. FISTA finds the same 6 atoms but the error is 0.013: shrinkage bias (λ = 0.03 is subtracted from each kept coefficient), and its λ cannot be 0.
E6. OMP picks position 128 first, between the true spikes at 126 and 130, then 40, 200, 90 and 122: two false atoms, two missed, error 1.2 (worse than nothing). FISTA puts weight on both true spikes (no misses, error 0.44), but with 17 small false atoms around them.
E7. Spikes ∪ DCT: 7 nonzeros, error 0.096. DCT only: 22 nonzeros, error 0.31 (the spikes ring). Spikes only: 214 nonzeros, error 0.24 (the cosines need almost every sample). The union is both the sparsest and the most accurate.
E8. Without debiasing the error is 0.415 (every spike is λ = 0.66 too low); with debiasing 0.080: the same 10 spikes, refitted by least squares.
Explore: restoring images with a sparsity prior
The same problem in 2D (256 × 256): denoise, deblur or inpaint the micrograph, a cartoon image or a texture, with no prior, Tikhonov (smoothness), wavelet ℓ1 (Haar, db4, optionally with cycle spinning) or TV. FISTA runs progressively; the plot shows the PSNR against the iteration (dashed: the observed image).
E9. Preset deblur: Tikhonov rings, then deblur: wavelet ℓ1 does not. Compare the error maps near the edges of the shapes. Then choose the prior none: what happens to the PSNR curve as the iterations go on?
E10. Preset λ too large: texture gone. Lower λ step by step and watch the stripes come back. Which λ gives the best PSNR? Is the best-PSNR image also the one you would show?
E11. Preset a scratch is fine, a large hole is not. Where does the restoration succeed, and what fills the square hole? Try TV and Haar.
E12. Preset remove the text: what is left of the letters? Why are the thin parts easier than the thick parts?
E9. Tikhonov leaves ripples along every edge (the error map shows bands of alternating sign on both sides of each contour); wavelet ℓ1 with cycle spinning keeps the edges sharp and the error concentrates in a thin line along them. With no prior the PSNR rises for a few iterations and then falls: least squares fits the noise more and more (semi-convergence, the early stopping of regularisation).
E10. At large λ the stripes are shrunk away (their coefficients are small, of the order of the noise threshold) and only the disc remains. Lowering λ brings them back together with some noise; the best PSNR lies at an intermediate λ. That image still has visibly weaker stripes than the truth: the PSNR rewards removing noise more than keeping low-contrast texture.
E11. The thin scratches disappear almost completely (the atoms around them see enough known pixels). The 16 × 16 hole is filled with a smooth average of its surroundings: no texture, no continuation of edges that cross it. TV fills it with flat patches (piecewise constant), Haar with blocks.
E12. The thin strokes vanish almost completely; the thick parts of the letters leave faint, smooth blobs. A thick stroke is a hole wider than the fine-scale atoms, so as in E11 only the coarse atoms can fill it.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in sparse/src/ista/exercise.rs. Vectors are lists of numbers and a matrix is a list of rows; mat_vec (A\mathbf x), mat_t_vec (A^\top\mathbf r) and objective are given.
C1. soft_threshold(v, t) (every entry soft-thresholded at t) and ista_step(a, y, x, λ, τ), one ISTA iteration:
r = y − A x # residual: data minus prediction
z = x + τ · Aᵀ r # gradient (Landweber) step
x_new = soft(z, τ λ) # soft threshold of every entry
The check is B1 (a): \mathbf x_1 = (0.8, 0.6), \mathbf x_2 = (0.96, 0.58).
C2. ista(a, y, λ, τ, iterations): start at \mathbf x = 0 and repeat ista_step. The checks are B1 (b) and (d): the iterates reach (1.6, 0) with an exact zero and the objective 0.9, and on a blurred signal the objective never increases.
C3. largest_gain(a, iterations): L, the largest eigenvalue of A^\top A, by the power method:
v = any vector of length 1
repeat:
w = Aᵀ (A v)
L = length of w
v = w / L
The checks are B3: L = 2.25 for the matrix of B1, L = 1 for a normalised blur.
C4. fista(a, y, λ, τ, iterations): ISTA steps from an extrapolated point:
x = 0, z = 0, t = 1
repeat:
x_new = ista_step(z)
t_new = (1 + sqrt(1 + 4 t²)) / 2
z = x_new + (t − 1)/t_new · (x_new − x)
x = x_new, t = t_new
The checks: the same minimum (1.6, 0) for B1, and on a blurred signal a much smaller gap to the minimum than ISTA after 100 iterations.
C5. omp(a, y, max_atoms, tolerance): orthogonal matching pursuit:
S = no columns, r = y, x = 0
while S has fewer than max_atoms columns and ‖r‖² > tolerance:
j = the column with the largest |a_jᵀ r| / ‖a_j‖
add j to S
x_S = least-squares fit of y with the columns in S # the others stay 0
r = y − A x
The checks are B2, (2, 0) after one atom, and B4 (c): the masked DCT atoms, atom 0 first with a = 1.82, then a = 2, b = 1.
Then cargo run -p sparse -- ista and open the pictures in out/ista/. The program deblurs a signal of 128 samples with five spikes (at 20, 52, 70, 76 and 105; blur σ = 2, noise 0.01) with λ = 0.01. In the pictures the truth is black, the data grey and the result blue dots.
C6. How many iterations do ISTA and FISTA need to get within 10^{-6} of the minimum of the objective? Look at ista-20.png and fista-1000.png, and at the printed numbers of nonzeros after 1, 10, 100 and 1000 ISTA iterations. Describe how the estimate develops.
C7. Which positions does OMP pick (omp.png)? Its relative error is much larger than that of FISTA, although the picture looks almost right. Why?
C8. Count the cost. One ISTA iteration with a dense n \times n matrix costs about 2n^2 multiplications. How many is that for a 256 × 256 image (n = 65\,536), and how much memory would the matrix take (8 bytes per number)? How does a blur avoid this?
The model solution is sparse/src/ista/solution.rs in the download.
C6. ISTA needs about 3100 iterations, FISTA about 150: twenty times fewer, at the same cost per iteration (the lecture’s signal: 993 and 121). After 20 ISTA iterations every spike is still a cluster of small coefficients spread over the width of the blur; the counts fall from 54 nonzeros after 1 iteration to 30, 16 and 10 after 10, 100 and 1000 (relative error 0.91, 0.83, 0.67, 0.22). After 1000 FISTA iterations (error 0.12) all five spikes sit at the right positions, including the close pair at 70 and 76, each a little too low (shrinkage bias), with a few tiny extra coefficients next to them.
C7. OMP picks 20, 52, 71, 77 and 105: the close pair at 70 and 76 is found one sample off each time. Under the blur, neighbouring atoms are almost the same, and the overlap of the two bumps plus the noise make the atom one sample over fit slightly better. OMP refits the values but never replaces an atom it has chosen. A spike one sample off counts as a missed spike plus a false one, hence the relative error of 0.85 against 0.12 for FISTA.
C8. 2 \cdot 65\,536^2 \approx 8.6 \cdot 10^9 multiplications per iteration, and 65\,536^2 \cdot 8 bytes = 34 GB for the matrix. A blur is never stored as a matrix: it is a small kernel (a convolution, or a multiplication in the Fourier domain), which costs about (kernel size) × n or n\log n per iteration.
Where this leads
ISTA fills in what the data do not determine with the sparsest continuation. The next unit turns this around: if a sparse prior can fill missing data, we can decide to measure less, and design the measurements so that the missing part can be filled reliably: compressed sensing (next unit).