Lab: NMF and its convergence checks
The representation chapter's second lab. Fit nonnegative matrix factorization with multiplicative updates, check that the error never rises, and see why the error alone can't choose the number of parts.
What runs in CodeSplash
- REP016Nonnegative matrix factorization (NMF) Runs in CodeSplash
- Non-negative matrix factorization Verified in Python and R
You will be able to
- Fit NMF with multiplicative updates and track the relative error
- Use a monotone error trace as a check on the code
- Explain why reconstruction error keeps falling as parts are added
- Report the starting point, because NMF finds a local optimum
The question
ReadNMFNon-negative matrix factorizationSplits non-negative data into a few additive parts so every row is a weighted sum of part profiles. writes a nonnegative table as a sum of nonnegative parts, which often makes the parts readable: a spectrum, a topic, a face feature. Two questions come with every fit. Did the algorithm work? And how many parts are there? The lab answers the first with a check the updates guarantee, and shows why the obvious answer to the second is wrong.
Explore it
ExploreAExplorable · NMF finds the parts
Why does a factorization that can only add find parts that look like the pieces the data were built from?
At the same rank, which reconstructs the images more closely?
Commit your prediction above to see the result
Make your prediction to see the result.
Change the number of parts and watch the reconstruction and the error trace. Then press Break it to see what too many parts do: the error barely moves while the parts start splitting real structure and fitting noise.
Do it yourself
ExploreThe notebook fits a 12 × 10 table made from three parts, checks that no update raises the error, and compares the final error across ranks one to five.
Notebook exercise · ~12 min
NMF and its convergence checks
Fit nonnegative matrix factorization with multiplicative updates, check that the error never rises, and see why error alone can't choose the rank.
- The fit's relative error
Fit three parts with
nmf(V, 3). Store the relative error ‖V − W H‖ / ‖V‖ (Frobenius norms) of the result inerr3.Show a solution
PythonW, H, trace = nmf(V, 3) err3 = np.linalg.norm(V - W @ H) / np.linalg.norm(V)Rfit <- nmf(V, 3) err3 <- norm(V - fit$W %*% fit$H, "F") / norm(V, "F") - Does the error ever rise?
The multiplicative updates should never increase the error. Check the trace of the rank-3 fit: store
True(R:TRUE) inmonotoneif no step raised the error by more than 1e-12, elseFalse.Show a solution
Pythonmonotone = bool(np.all(np.diff(trace) <= 1e-12))Rmonotone <- all(diff(fit$trace) <= 1e-12) - Error at every rank
Fit ranks 1 to 5 (same seed) and store their final relative errors, in order, in
errs. Does the error ever stop falling as the rank grows?Show a solution
Pythonerrs = [nmf(V, r)[2][-1] for r in range(1, 6)]Rerrs <- sapply(1:5, function(r) tail(nmf(V, r)$trace, 1))
In the app this opens as a Python or R notebook with the sample data, and its checks mark your progress.
In CodeSplash
In the appDiscover's Map the data includes NMF, with the reconstruction error in its result. Run it at a few ranks with the same seed, then again with another seed at the rank you prefer; if the parts change a lot between starts, the rank is not well supported.
Check: the lab
CheckpointCheck yourself
The Lee–Seung multiplicative updates should never increase the Frobenius error. Your trace rises at some step. What's the most likely explanation?
Show the answer
Answer: a
A guaranteed property makes a strong test of the code. A rising trace means the implementation doesn't do what the theory says.
Check yourself
The error falls sharply up to three parts, then barely changes. What should you conclude about the rank?
Show the answer
Answer: a
More parts can always fit the table at least as well, so the smallest error never picks the rank by itself.
Recap
Recap- A monotone error trace checks the updates; a rise means a bug.
- Error keeps falling with rank, so it can't choose the rank alone.
- NMF stops at a local optimum: report the start and check stability.
Concepts in this lesson