Skip to content

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.

Applied ~22 min6 steps

What runs in CodeSplash

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

Read first

01

The question

Read

NMFNon-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.

02

Explore it

Explore

AExplorable · 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?

How sure?
10
200
0.30
0.05
40
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

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.

03

Do it yourself

Explore

The 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.

  1. 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 in err3.

    Show a solution
    Python
    W, H, trace = nmf(V, 3)
    err3 = np.linalg.norm(V - W @ H) / np.linalg.norm(V)
    R
    fit <- nmf(V, 3)
    err3 <- norm(V - fit$W %*% fit$H, "F") / norm(V, "F")
  2. 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) in monotone if no step raised the error by more than 1e-12, else False.

    Show a solution
    Python
    monotone = bool(np.all(np.diff(trace) <= 1e-12))
    R
    monotone <- all(diff(fit$trace) <= 1e-12)
  3. 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
    Python
    errs = [nmf(V, r)[2][-1] for r in range(1, 6)]
    R
    errs <- 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.

04

In CodeSplash

In the app

Discover'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.

05

Check: the lab

Checkpoint

Check 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.

06

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