Repository navigation
adding adaptive learning rate for minibatch k-means - #30051
BenJourdan wants to merge 20 commits into
Conversation
|
The diff is indeed relatively small, however, the paper is quite recent, and the improvements are marginal. So I'll let @ogrisel , @lorentzenchr , @jeremiedbb, and @GaelVaroquaux weigh in here. |
|
I have similar feelings. Unfortunately, arxiv.org seems to be unresponsive since yesterday for me. I cannot check the benchmark results from the paper. @BenJourdan could you please add results for full-batch k-means to your plots? I am wondering if this can allow MB-k-means to reach the same scores as full-batch k-means on those problems. |
35c5b19 to
158897a
Compare
|
Thanks for the update. So from those experiments, it appears that the new lr scheme can empirically help MBKMeans close the (smallish) gap with full-batch KMeans in terms of clustering quality while keeping favorable runtimes for datasets with many data points (e.g. MNIST size or larger). But since the method was recently published, this PR does not technically meet our inclusion criteria, although we could be less strict in cases where this is an incremental improvement of an existing method implemented in scikit-learn. I will mention this PR at our next monthly meeting. |
|
What was the verdict @ogrisel? |
|
There were no clear objections to include this, and I think a few of us are in favor of including it. |
|
@ogrisel @adrinjalali what happens next? Should I start updating the branch? |
|
@BenJourdan seems like it. |
65e49cb to
320a4b4
Compare
|
Should I keep updating the branch until (if lol) someone gets assigned? Not sure what the convention is. |
|
Hi @ogrisel, @adrinjalali, Just checking in—since there were no objections during the meeting and some support for inclusion, would it make sense to remove the Needs Decision label and move toward review/approval? Please let me know what you'd recommend as the next step. Thanks again for your time and feedback so far! |
|
I've removed "need decision" here. I think we can move forward with this. |
|
Great! Let us know if we should do anything on our end. |
|
@antoinebaker would you mind having a look at this PR for a review? |
antoinebaker
left a comment
There was a problem hiding this comment.
Thanks for the PR @BenJourdan ! Here a first round of review.
|
I feel the code could be simplified by first defining a learning rate: if adaptive_lr:
lr = sqrt(wsum / wsum_batch)
else:
lr = wsum / (weight_sums[cluster_idx] + wsum)and then do the common updates for feature_idx in range(n_features):
centers_new[cluster_idx, feature_idx] = (1 - lr) * centers_old[cluster_idx, feature_idx]
for k in range(n_indices):
sample_idx = indices[k]
for feature_idx in range(n_features):
weight_idx = sample_weight[sample_idx] / wsum
centers_new[cluster_idx, feature_idx] += lr * weight_idx * X[sample_idx, feature_idx] |
There was a problem hiding this comment.
Thanks for the feedback. I'll add most of your suggestions as are.
Introducing the learning rate to avoid duplicating code is a good idea. However, it's a bit messy since we need to do an optimization that avoids explicitly computing the means of each batch for a given center. I'll have a go at redrafting those parts.
Thanks!
0bf05ba to
0094c8e
Compare
0094c8e to
13fe041
Compare
|
@BenJourdan please (at almost all cost) avoid force pushing here. It makes review harder. |
There was a problem hiding this comment.
Pull request overview
This PR adds an optional adaptive learning-rate update rule to MiniBatchKMeans, wiring a new adaptive_lr parameter through the Python estimator, the _mini_batch_step helper, and the Cython dense/sparse update kernels, with accompanying documentation and a regression test.
Changes:
- Add
adaptive_lrparameter toMiniBatchKMeansand plumb it throughfit/partial_fitinto the minibatch update step. - Implement adaptive learning-rate center updates in the Cython dense and sparse minibatch update routines.
- Add a targeted unit test for
partial_fitbehavior + a whatsnew entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
sklearn/cluster/tests/test_k_means.py |
Adds a regression test validating adaptive-LR updates for partial_fit. |
sklearn/cluster/_kmeans.py |
Introduces the adaptive_lr public parameter and passes it through fit/partial_fit to minibatch updates. |
sklearn/cluster/_k_means_minibatch.pyx |
Implements the adaptive learning-rate update rule in dense and sparse center updates. |
doc/whats_new/upcoming_changes/sklearn.cluster/30051.enhancement.rst |
Documents the new adaptive_lr=True feature in the changelog. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds an adaptive_lr option to sklearn.cluster.MiniBatchKMeans to update cluster centers using an adaptive learning rate (per the referenced arXiv paper), and wires it through both fit and partial_fit paths with corresponding tests and release notes.
Changes:
- Introduce
adaptive_lrparameter onMiniBatchKMeansand propagate it through_mini_batch_step. - Implement adaptive learning-rate center updates in the Cython minibatch update kernels (dense + sparse).
- Add targeted unit tests for expected update behavior and a whats-new entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
sklearn/cluster/_kmeans.py |
Adds adaptive_lr parameter, validation tweaks, plumbing into minibatch step, and class doc/reference updates. |
sklearn/cluster/_k_means_minibatch.pyx |
Implements adaptive learning rate update rule in dense/sparse minibatch center updates. |
sklearn/cluster/tests/test_k_means.py |
Adds regression tests validating adaptive LR behavior for partial_fit and fit. |
doc/whats_new/upcoming_changes/sklearn.cluster/30051.enhancement.rst |
Documents the new adaptive_lr=True capability in release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
Adds an optional adaptive learning rate path to MiniBatchKMeans, with Cython-level update logic and corresponding tests + release note, to improve minibatch center updates (per arXiv:2304.00419).
Changes:
- Introduce
adaptive_lrparameter onMiniBatchKMeansand thread it through minibatch update calls. - Implement adaptive learning rate updates in
_k_means_minibatch.pyxfor dense and sparse minibatch center updates. - Add targeted tests for
adaptive_lrbehavior and a whatsnew entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
sklearn/cluster/tests/test_k_means.py |
Adds tests covering adaptive LR convergence behavior and rejection of negative sample_weight. |
sklearn/cluster/_kmeans.py |
Adds adaptive_lr parameter plumbing + sample_weight validation changes; updates docs/constraints. |
sklearn/cluster/_k_means_minibatch.pyx |
Implements adaptive learning rate update rule in Cython minibatch update kernels. |
doc/whats_new/upcoming_changes/sklearn.cluster/30051.enhancement.rst |
Announces the new adaptive_lr=True feature in MiniBatchKMeans. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR adds an optional “adaptive learning rate” variant to MiniBatchKMeans updates (per arXiv:2304.00419), wiring it through the Python estimator API down to the Cython mini-batch center update kernels, with accompanying tests and release notes.
Changes:
- Add
adaptive_lrparameter toMiniBatchKMeansand plumb it through_mini_batch_stepinto the dense/sparse minibatch update routines. - Implement adaptive learning-rate center updates in
sklearn/cluster/_k_means_minibatch.pyx. - Add targeted tests for
fit/partial_fitbehavior and negativesample_weightvalidation; document the enhancement in the whatsnew entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
sklearn/cluster/_kmeans.py |
Exposes adaptive_lr on MiniBatchKMeans, validates sample weights accordingly, and forwards the flag into _mini_batch_step / Cython update calls. |
sklearn/cluster/_k_means_minibatch.pyx |
Adds adaptive learning-rate support to dense and sparse minibatch center update kernels. |
sklearn/cluster/tests/test_k_means.py |
Adds tests covering adaptive LR convergence behavior and negative sample_weight rejection when enabled. |
doc/whats_new/upcoming_changes/sklearn.cluster/30051.enhancement.rst |
Notes the new adaptive_lr=True feature in upcoming release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
I've added a changelog entry (
I also accepted most of copilot's suggestions, although an "all-zero sample weights" check was unnecessary since |
|
@adrinjalali ready for you to take another look |
adrinjalali
left a comment
There was a problem hiding this comment.
It would take me quite some time to be able to do a proper review here, but trying to move this forward, I had a chat with my claude on this PR, and after about 2 hours of back and forth and me reading the code, I'm comfortable posting here what claude says. Of course I'd need you to take this as "Adrin thinks this makes sense" rather than "Adrin certainly believes all of this" @BenJourdan . I'm also genuinely curious to hear if these comments are helpful.
Co-authored-by: Adrin Jalali <[email protected]>
|
Hey @adrinjalali, I applied most of the changes Claude suggested. Most were useful but the one about not calling it an adaptive EMA and the one about Edit: The suggested change to add the |
|
@adrinjalali Gentle ping here. Thanks again for taking a look at this. If there are any remaining changes that would help get this over the line, we’d be very happy to make them. |
|
@adrinjalali @ogrisel @antoinebaker — I’d like to get a definitive outcome on this PR. It has now been open for nearly two years. Could a maintainer please do one of the following:
We’re happy to address concrete blockers, but we can’t keep maintaining an indefinitely idle PR. If nobody has the bandwidth to own the final review, a clear decision to close it is completely fine. |





Reference Issues/PRs
None
What does this implement/fix? Explain your changes.
This request implements a recent learning rate for minibatch k-means which can be superior to the default learning rate. We implement this with the flag
adaptive_lrthat defaults to false.Details can be found in this paper that appeared in ICLR 2023. Extensive experiments can be found in this manuscript - ignore the kernel k-means results. We also added a benchmark that produces the following plot which shows the learning rate is the same or better than the default on dense datasets.
Any other comments?
This is a reasonably small code change. We add a flag to the MinibatchKmeans constructor and the _k_means_minibatch.pyx cython file. The learning rate implementation is straightforward. In the benchmarks, it appears to take a few more iterations for the adaptive learning rate to converge, often resulting in better solutions. When we removed early stopping we observed the running time is about the same.
This should be a cleaner version of #30045 (I made a mess since I'm still pretty new to git).