Skip to content

purify() stops before the tensor is pure when some weights are zero (randomized order) #702

Open

Description

@flyingdoubleG

interpret.utils.purify often returns a tensor that is not pure when some cells have zero weight. It happens with the default is_randomized=True and tolerance=0, even for 2D tensors:

import numpy as np
from interpret.utils import purify

rng = np.random.default_rng(0)
fails = 0
for _ in range(200):
    w = rng.uniform(0.1, 3, (5, 6)) * (rng.random((5, 6)) > 0.6)  # ~60% empty cells
    s = rng.normal(size=(5, 6))
    pure, _, _ = purify(s, w)
    err = max(np.abs((pure * w).sum(0)).max(), np.abs((pure * w).sum(1)).max())
    fails += err > 1e-9
print(fails, "/ 200 not pure")  # 182 / 200 on 0.7.8

The weighted row and column sums of the result are off by up to about 0.9 here, with scores of order 1. A pure solution always exists, because purification is the weighted least-squares projection onto the pure subspace. With is_randomized=False the same inputs purify to about 1e-16 (0 failures in 200). Tensors without zero weights, or large tensors such as 32×32, are fine. Small and medium tensors with empty cells fail often, for 3D and 4D as well (e.g. 3×3×3 with 30% empty cells: 38/40).

I think the cause is the exit condition in Purify.cpp, while(!(impurityPrev <= impurityCur)). The loop stops after the first sweep whose total |update| is not smaller than the previous sweep's. With a fresh random fiber order in each sweep, that sum is not monotone, and empty cells make convergence slow, so a single non-decreasing sweep usually comes long before convergence. A Python replica of the loop (same fiber updates, same exit rule) reproduces the failure rate: 179/200 random, 2/200 ordered. Exiting only after, say, 20 sweeps without a new minimum, or once the impurity reaches the tolerance, gives 0/200 with random order.

purify_result is off by default in EBM training, so this mostly affects direct users of purify. Nothing is raised, so the impure output is silent.

I don't have a toolchain to build libebm here, so I'm filing this rather than a PR. The same exit pattern is at the end of the multiclass loop (while(bRetry && !(impurityPrev <= impurityCur))).

(Found while cross-checking purify against its documented guarantees with Claude Code; I reviewed the analysis.)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions