Skip to content

LogisticRegression fits one-vs-rest where sklearn fits multinomial softmax #433

Closed

Description

@godofecht

Split out from #408, which is now fixed for the regularization half.

Background

#408 found the canonical benchmark was passing penalty_l2(0.001) flat while sklearn used C=1.0. The correct mapping is alpha = 1/(C * n_samples), so the old constant was 8.3x too weak on iris and 1.4x too strong on digits. PR #430 fixed that with penalty_l2_from_c(C, n_samples).

With regularization now equal, a structural difference is visible that the mismatched alpha had been partly masking:

row Flow sklearn
iris accuracy 0.899999976 0.933333333
iris coef_frobenius_norm 4.937423 4.517331
digits accuracy 0.969444454 0.972222222
digits coef_frobenius_norm 15.171825 10.364157

sklearn fits one multinomial softmax; flow-scikit fits one-vs-rest. On iris the entire accuracy difference is one test sample of thirty: at equal regularization OvR gets 27 and multinomial gets 28.

Consequence right now

The iris score gap is 0.0333 against a score_abs_tolerance of 0.02, so the row reclassifies to not parity verified, the canonical count drops from 19/19 to 18/19, and publish_headline_v2.py refuses to publish. That was an accepted, deliberate tradeoff: the benchmark is now honest and the claim is temporarily lower, rather than the claim being propped up by comparing differently-regularized models.

What would fix it

Implement multinomial (softmax) logistic regression and use it for the multiclass case, matching sklearn's default. The objective is the cross-entropy over a softmax across all classes fitted jointly, rather than K independent binary fits combined afterwards.

Notes for whoever takes it:

  • multiclass_logistic_fit / multiclass_logistic_decide already exist in lib/scikit/linear.flow; check whether that path is OvR too, and whether the benchmark uses it.
  • The penalty convention established by fix: match sklearn's C=1.0 in the canonical LogisticRegression benchmark (#408) #430 must carry over: Flow minimises the mean loss plus 0.5 * alpha * ||w||^2, so alpha = 1/(C * n_samples) remains the mapping.
  • sklearn does not penalize the intercept. Flow already does not; keep it that way.
  • Softmax is numerically delicate. Subtract the row max before exponentiating. Note Flow's constraint against inline exp() in a nested while loop, recorded in AGENTS.md.
  • Both canonical LogisticRegression rows will move. Report before and after prominently rather than burying it, and state the effect on the 18/19 count.

Restoring 19/19

If multinomial closes iris to within 0.02, the row returns to eligible and the count goes back to 19/19 on honest terms. If it does not, the remaining difference should be recorded in parity_contract.json as a declared semantic difference with the measurement attached, in the same style as the KMeans work in #203, rather than by widening a tolerance.

Activity

  1. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    Multinomial closes it. Measured, not predicted.

    Implemented multinomial softmax in logistic_regression_fit and routed nc > 2 through it. The binary path is untouched. Objective is the mean cross-entropy of the softmax over all classes fitted jointly, plus 0.5 * alpha * ||W||^2 summed over the class weight rows, intercepts unpenalized, so penalty_l2_from_c(C, m) from #430 carries over with no change.

    Canonical benchmark, single run, macOS, FLOW_OPT_LEVEL=0, -framework Accelerate:

    row before (OvR) after (multinomial) sklearn gap after
    LogisticRegression iris accuracy 0.899999976 0.933333337 0.933333333 0.000000004
    LogisticRegression digits accuracy 0.969444454 0.975000024 0.972222222 0.002777802

    Both are inside score_abs_tolerance 0.02. iris was 0.0333 outside.

    Coefficient diagnostics moved the same way:

    detail before after sklearn
    iris coef_frobenius_norm 4.93742323 4.51708984 4.51733123
    digits coef_frobenius_norm 15.1718254 10.3909101 10.3641397
    iris intercepts -2.414, -0.946, -3.428 -0.305, 1.909, -1.604 -0.306, 1.909, -1.603

    The intercepts are the clearest signal that the two models are now the same object. One-vs-rest pushed every intercept far negative because each binary subproblem sees a 1:2 class imbalance; the joint softmax does not.

    Diffed every RESULT| and DETAIL| record between origin/main and the branch: 165 records, keys identical, 10 differ, and all 10 are the two LogisticRegression rows and their details. The other 17 canonical rows are byte-identical.

    Cost: iris fit 0.077 ms -> 0.139 ms, digits fit 17.25 ms -> 25.97 ms. The joint problem is one LBFGS over nc * (n + 1) parameters instead of nc independent ones over n + 1, and on digits it runs to the 100-iteration cap where the OvR subproblems converged earlier.

    PR to follow.

  2. godofecht commented on Aug 19, 2026

    @godofecht
    OwnerAuthor

    State of main right now, and a baseline subtlety worth not forgetting

    After #430 merged, main's Remaining issue implementation workflow fails, exactly as predicted and accepted:

    - ('LogisticRegression', 'iris',   'accuracy') score |delta| regressed 4e-09  -> 0.0333334
    - ('LogisticRegression', 'digits', 'accuracy') score |delta| regressed 1.3e-08 -> 0.00277777
    

    Two things to note.

    Only iris breaks eligibility. The digits gap of 0.00277777 is well inside its score_abs_tolerance of 0.02. It is flagged because the disparity gate compares against the frozen baseline, not because the row fails parity. So the canonical count is 18/19, not 17/19.

    The word "regressed" is misleading here, and the baseline is the reason. That baseline was frozen while the benchmark was comparing a Flow model at alpha = 0.001 against a sklearn model at C = 1.0, which on iris is 8.3x weaker regularization. The old 4e-09 agreement was an artifact of two differently-regularized models happening to land on the same accuracy, not evidence of parity. The new numbers are the first honest measurement of this row.

    Consequence for whoever finishes this issue: once multinomial lands, disparity_history.json still holds a snapshot taken under the mismatched-alpha benchmark. Comparing against it will keep producing spurious regression lines for these two rows even after the gap closes. The baseline needs re-freezing on main after the fix, and it is worth saying in that PR that the pre-#430 snapshot is not a valid comparison point for LogisticRegression.

    This is the same class of problem #425 addressed for the published page: a ratio or a delta measured against a baseline taken under different conditions is not a measurement of change. Here the conditions were a benchmark bug rather than a machine change.

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