Repository navigation
Description
We encountered the following behavior of AdaBoost when working on the scikit-learn mooc. Here is a script that reproduces the problem:
We compute the validation curves for 3 models with different numbers of leaf nodes in the weak learners and each time study the impact of the number of trees on the train and validation errors.
from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import validation_curve
from sklearn.datasets import fetch_california_housing
import matplotlib.pyplot as plt
data, target = fetch_california_housing(return_X_y=True)
for max_leaf_nodes in [50, 100, 500]:
adaboost = AdaBoostRegressor(
learning_rate=1.,
base_estimator=DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes),
)
param_range = [1, 2, 5, 10, 20, 50, 100, 500]
train_scores, test_scores = validation_curve(
adaboost,
data,
target,
param_name="n_estimators",
param_range=param_range,
scoring="neg_mean_absolute_error",
n_jobs=4,
)
train_errors, test_errors = -train_scores, -test_scores
plt.figure()
plt.errorbar(param_range, train_errors.mean(axis=1),
yerr=train_errors.std(axis=1), label="Training score",
alpha=0.7)
plt.errorbar(param_range, test_errors.mean(axis=1),
yerr=test_errors.std(axis=1), label="Cross-validation score",
alpha=0.7)
plt.legend()
plt.xscale("log")
plt.xticks(param_range)
plt.ylabel("Mean absolute error in k$\n(smaller is better)")
plt.xlabel("# estimators")
_ = plt.title(
f"AdaBoost regressor with {max_leaf_nodes=}\n"
f"best CV score: {test_errors.mean(axis=1).min():.4f}"
)Here are the results:
For 500 leaves we get the expected behavior: both the training and validation errors decrease when increasing the number of trees. At some point we reach a plateau of diminishing returns (expected, at least for validation error).
However for lower number of leaves (e.g. the second plot with max_leaf_nodes=50), the training set is growing with more trees! This is really something that I would not have expected. I wonder if this is not revealing a bug in our implementation.
For reference, for lower values of max_leaf_nodes both the training and validation errors stay do not decrease (and even increase again) and they stay close to one another (severe under fitting) with large error bars.



Activity
europeanplaice commentedon Jul 3, 2021
My suggestion is that if the prediction score is not improved compared to the previous score, we should reset
sample_weight.I'm not an expert but my understanding is as follows. Forgive me if I'm wrong.
Sample weights are updated so that the weak learner study intensively the data the previous weak learner can't predict well.
It means the weak learner is given challenging data more frequently than one step before.
However, if the weak learner is too weak to restudy such data, prediction scores deteriorate because the percentage of the challenging data goes up.
The weak learner can't get insights because the task is too difficult.
Consequently, although we increase the number of trees, we can't get the expected result.
My proposal
From
scikit-learn/sklearn/ensemble/_weight_boosting.py
Lines 1138 to 1146 in e4ef854
To like this
The results were

I'm not sure this problem is a bug and I wonder resetting
sample_weightmakes this something other than AdaBoost.ogrisel commentedon Jul 4, 2021
Very interesting analysis and clever fix.
I agree. But maybe we could add a flag to enable or disable the trick you suggest. @europeanplaice do you know if any paper been published in the weight-based boosting literature to analyze this problem?
For reference, I tried to run the experiment in the description replacing AdaBoost with gradient boosting and there is no problem: training error always trends towards zero with more trees, irrespective of the size of the trees.
[-]AdaBoost can underfit more with larger number of trees[/-][+]AdaBoost's training error can increase with a larger number of trees[/+]glemaitre commentedon Jul 4, 2021
I am not sure entirely but I kind of recall something similar with the classification problem while preparing the MOOC.
Thought I might be wrong since this is quite a long time ago (but I kind of recall plotting a score and not an error and seeing a bell curve like).
ogrisel commentedon Jul 5, 2021
For information, I have tried:
Those changes did not impact the shape of the training error curve with
max_leaf_nodes=50: each time, the training error starts to increase withn_estimator > 10.europeanplaice commentedon Jul 5, 2021
@ogrisel @glemaitre
Thank you for the additional information.
I've found a paper that seems to be related to this issue.
An Investigation into How ADABOOST Affects Classifier Diversity
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.22.9349&rep=rep1&type=pdf
According to this paper, diversity plays an important role in performance.
Also, here are the histograms of the shuffled indexes of train data in this example. I recorded them on tensorboard.
A histogram of the shuffled indexes stacks at every iteration. A peak of a histogram shows that there exist some indexes frequently repeated.
Below is the histogram of the first attempt that was underfitted (max_leaf_nodes=50).
As you can see, there are a few peaks, and these peaks don't change as iterations proceed.
It seems like AdaBoost got caught in local minima.
However, introducing resetting
sample_weightgive diversity to some extent.ogrisel commentedon Jul 5, 2021
Thanks for the reference. The paragraph you highlighted seems to accurately describe what we are experiencing. I don't understand the histograms though. Could you give more details on how they are constructed? In particular what is the x axis and what are the stacks?
ogrisel commentedon Jul 5, 2021
For reference I also tried to decrease the learning rate (and increase the maximum number of trees): the problem appears later (e.g. around
n_estimator=100forlearning_rate=0.1but it still there. So decreasing the learning rate might only be a way to hide this convergence problem.europeanplaice commentedon Jul 5, 2021
Sorry for the lack of explanation.
https://github.com/europeanplaice/scikit-learn/blob/adaboost_with_tensorboard/sklearn/ensemble/_weight_boosting.py
This is the code that records histograms on tensorboard.
x-axis is the index number and y-axis is the index of the weak estimators.
z-axis (height of histograms ) is the count of the index that is repeatedly chosen.
The histogram was recorded at this line.
tf.summary.histogram("bootstrap_idx", bootstrap_idx, iboost)DAKSHA2001 commentedon Aug 29, 2021
take
ogrisel commentedon Sep 7, 2021
Thinking again about this issue, I think we could implement the strategy suggested by @europeanplaice above (#20443 (comment)) but it should not be the default behavior of this estimator because it would not be the "real" Adaboost anymore.
Maybe what we could do is:
no_improvement="warn"and monitor the value of beta: if it stop improving, raise a warning suggesting to warn the user that the training loss does not decrease when adding more base estimator and suggest to try with different hyper-parameters for the base estimators to allow for more diversity or to setno_improvement="reset_weights"orno_improvement="stop"explicitly.no_improvement="reset_weights"would implement the strategy above,no_improvement="stop"would just implement early stopping,no_improvement="continue"would just silence the warning.Not sure the latter two options are that useful, but they are easy to implement and give full control to the user and make it possible to keep the behavior of scikit-learn 0.24.2 with the warning.
europeanplaice commentedon Sep 7, 2021
Thank you for leading the discussion and indicating the implementation guide. I'll try to implement it according to your remark.
glemaitre commentedon Jun 2, 2023
Since I observed the same behaviour on
AdaBoostClassifier. To get this behaviour, I had the combination of a low learning rate, with an expressive model. The train and test errors of AdaBoost are the following using both algorithms:One thing that I noticed is the error of the weak learner at each stage:
Somehow, one would expect the values as seen in "SAMME". It is counterintuitive to have a lower error for the latest weak learner.
Code to reproduce the issue:
Details
glemaitre commentedon Jun 3, 2023
Actually, AdaBoost is minimizing the exponential loss which is not necessarily minimizing the misclassification error. It might be one of the issue that observe. Also the papers are not written with a learning rate and I am not sure that we are placing it at the right location.