scikit-learn / scikit-learn/scikit-learn
More memory efficient LinearClassifierMixin.predict
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 67.3k
- Forks
- 27.4k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 58
Description
I'm running out of memory with LogisticRegression.predict on a dataset with,
- n_samples = 1M, n_feature=8k, n_classes=800
This happens as LinearClassifierMixin.predict, needs to compute the decision function for all classes which involves a (n_samples, n_features) x (n_features, n_classes) multiplication, resulting in a (n_samples, n_classes) dense matrix (in my case ~6 GB).
Batching in n_samples is a solution, but it would have been nice to have a standard way of doing that, either as a util function that would be used to decorate e.g. batch_apply(LinearClassifierMixin.predict)(X) or a fit param LinearClassifierMixin.predict(X, batch_size=1000).
This is somewhat related to parallizing predict in general (since there batching also happens). Can't find the corresponding issue right now.
This particular case is made worse by the fact that LogisticRegression.coef_ is not enforced to be float32 for float32 sparse training input (with the liblinear solver) related https://github.com/scikit-learn/scikit-learn/issues/8769.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at LinearClassifierMixin.predict and follow how LogisticRegression.predict computes the decision function for all classes. Reproduce the reported large-input memory behavior, then evaluate the proposed batching approaches and their API implications. Done means prediction handles large sample and class dimensions without requiring the full dense decision matrix in memory.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100