Repository navigation
[MRG+2] Fix excessive memory usage in random forest prediction #8672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
379ff2d
5e28248
88c0720
806ed48
27aad4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -374,6 +374,19 @@ def feature_importances_(self): | |
| return sum(all_importances) / len(self.estimators_) | ||
|
|
||
|
|
||
| # This is a utility function for joblib's Parallel. It can't go locally in | ||
| # ForestClassifier or ForestRegressor, because joblib complains that it cannot | ||
| # pickle it when placed there. | ||
|
|
||
| def accumulate_prediction(predict, X, out): | ||
| prediction = predict(X, check_input=False) | ||
| if len(out) == 1: | ||
| out[0] += prediction | ||
| else: | ||
| for i in range(len(out)): | ||
| out[i] += prediction[i] | ||
|
|
||
|
|
||
| class ForestClassifier(six.with_metaclass(ABCMeta, BaseForest, | ||
| ClassifierMixin)): | ||
| """Base class for forest of trees-based classifiers. | ||
|
|
@@ -565,31 +578,20 @@ class in a leaf. | |
| # Assign chunk of trees to jobs | ||
| n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) | ||
|
|
||
| # Parallel loop | ||
| all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose, | ||
| backend="threading")( | ||
| delayed(parallel_helper)(e, 'predict_proba', X, | ||
| check_input=False) | ||
| # avoid storing the output of every estimator by summing them here | ||
| all_proba = [np.zeros((X.shape[0], j), dtype=np.float64) | ||
| for j in np.atleast_1d(self.n_classes_)] | ||
| Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( | ||
| delayed(accumulate_prediction)(e.predict_proba, X, all_proba) | ||
| for e in self.estimators_) | ||
|
|
||
| # Reduce | ||
| proba = all_proba[0] | ||
|
|
||
| if self.n_outputs_ == 1: | ||
| for j in range(1, len(all_proba)): | ||
| proba += all_proba[j] | ||
|
|
||
| for proba in all_proba: | ||
| proba /= len(self.estimators_) | ||
|
|
||
| if len(all_proba) == 1: | ||
| return all_proba[0] | ||
| else: | ||
| for j in range(1, len(all_proba)): | ||
| for k in range(self.n_outputs_): | ||
| proba[k] += all_proba[j][k] | ||
|
|
||
| for k in range(self.n_outputs_): | ||
| proba[k] /= self.n_estimators | ||
|
|
||
| return proba | ||
| return all_proba | ||
|
|
||
| def predict_log_proba(self, X): | ||
| """Predict class log-probabilities for X. | ||
|
|
@@ -678,14 +680,18 @@ def predict(self, X): | |
| # Assign chunk of trees to jobs | ||
| n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs) | ||
|
|
||
| # avoid storing the output of every estimator by summing them here | ||
| if self.n_outputs_ > 1: | ||
| y_hat = np.zeros((X.shape[0], self.n_outputs_), dtype=np.float64) | ||
| else: | ||
| y_hat = np.zeros((X.shape[0]), dtype=np.float64) | ||
|
|
||
| # Parallel loop | ||
| all_y_hat = Parallel(n_jobs=n_jobs, verbose=self.verbose, | ||
| backend="threading")( | ||
| delayed(parallel_helper)(e, 'predict', X, check_input=False) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this now works without |
||
| Parallel(n_jobs=n_jobs, verbose=self.verbose, backend="threading")( | ||
| delayed(accumulate_prediction)(e.predict, X, [y_hat]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused about the [y_hat] aspect. It seems to indicate that regardless of if y_hat is multi-output or not, that |
||
| for e in self.estimators_) | ||
|
|
||
| # Reduce | ||
| y_hat = sum(all_y_hat) / len(self.estimators_) | ||
| y_hat /= len(self.estimators_) | ||
|
|
||
| return y_hat | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't the right condition, I think. Isn't it examining a 2d array whose length is the number of samples. Rather you can test for the presence of attribute
shapeto distinguish array from list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that tests are passing makes me think I've missed something or tests are too weak...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this revised function,
outis always a list. It contains only one element in the single output case. The function could have either of these two interfaces:I figured (2) was simpler, but I can change to (1) if desired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion on any of the solution.
However, using
if isinstance()is straight forward regarding the aim of theifstatement.With your solution, I think that you would need a comment preceding the
ifstatement for the sake of clarity.