You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Random Forest: Memory increases linearly with n_estimators #8244
It would be great if you could put together a stand-alone snippet which reproduces the behaviour. AFAICT your script depends on some non-standard package (vigra) and the data you run it on is not easily available.
Sorry, maybe my choice for the issue title was bad, I can see that it is expected that the memory consumption grows linearly with the number of trees, but right now it grows with a factor of two.
This really is a showstopper if you want to use a Random Forest with several trees for large feature matrices. It appears like the feature matrix is copied for each tree.
I have seen other Random Forest implementations (e.g. vigra) that don't need this and hence don't suffer from similar RAM issues.
If you are still interested, I could change the script that produced the numbers and upload the features I have used.
If you are still interested, I could change the script that produced the numbers and upload the features I have used.
Please do. The memory consumption you observe is strange: as you say your memory usage looks like your input data gets copied n_estimators times which I don't think is expected or at the very least deserves further investigation. The easier you make the problem easy to reproduce the better feedback you will get. Bonus points if you can reproduce the problem on a randomly generated dataset.
@lesteve : Thanks for your feedback, I will update the script and try to reproduce the issue with a more generic dataset later. @glouppe: the labels are provided as uint8 and their shape is (11501,).
How many classes do you have? Can you check that model.classes_ is as you expect? (the size of node is linear with len(model.classes_), and the number of nodes might grow up to n_samples)
I have tried to reproduce the behaviour with a standard dataset and used the digits dataset.
Interestingly, there the issue does not occur (the max RAM usage is approximately constant with the trees). I have already tried some obvious things like converting my data to the same types as the digits data, but that hasn't helped:
The upload is finished. Let me know if I can offer you any more help.
Also, I should mention that I have run into similar RAM problems with the Random Forest Implementation in the past, but I have never bothered to actually profile it before.
Anyway, if you are interested in a different dataset that reproduces these errors I could probably find some (though it won't be generic either).
aren't default parameters liable to overfit the data, meaning that the
learnt trees effectively copy the data into rules (although there should be
some logarithmic compression of the data)?
Have you tried using parameters which reduce the tree size to see if this
remains an issue?
On 31 Jan 2017 8:14 am, "Constantin Pape" <[email protected]> wrote:
The upload is finished. Let me know if I can offer you any more help.
Also, I should mention that I have run into similar RAM problems with the
Random Forest Implementation in the past, but I have never bothered to
actually profile it before.
Anyway, if you are interested in a different dataset that reproduces these
errors I could probably find some (though it won't be generic either).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8244 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz69ruVRMpzQno_ow69_Zs8krR97neks5rXlK5gaJpZM4Lw8B8>
.
@jnothman That's an interesting point, however I don't really see how it should affect the RAM usage during prediction as observed. After all the trees should simply be walked to the corresponding leaves for all test instances.
Anyway I will give training forests with reduced complexity a try tomorrow.
I will also profile the two RF implementations I am benchmarking against, maybe they are showing the same effects and I simply haven't noticed because they are implemented in a fashion that is more memory efficient overall.
@lesteve: I see, so if I understand this correctly, the proble is that for each tree the probability array is pre-allocated. Probably this is done, because the parallelisation is done over the trees.
Note that I think this issue could be fixed by parallelising over the instances instead.
Indeed, I have rerun the profiling also for the vigra random forest (which parallelises prediction over the instances) and there the ram issue does not occur:
Yes, we could change to parallelising over instances in the predict_proba
case, as long as we don't increase overhead for predicting a small number
of instances.
Activity
lesteve commentedon Jan 30, 2017
It would be great if you could put together a stand-alone snippet which reproduces the behaviour. AFAICT your script depends on some non-standard package (vigra) and the data you run it on is not easily available.
glouppe commentedon Jan 30, 2017
agramfort commentedon Jan 30, 2017
indeed. It's expected.
constantinpape commentedon Jan 30, 2017
Sorry, maybe my choice for the issue title was bad, I can see that it is expected that the memory consumption grows linearly with the number of trees, but right now it grows with a factor of two.
This really is a showstopper if you want to use a Random Forest with several trees for large feature matrices. It appears like the feature matrix is copied for each tree.
I have seen other Random Forest implementations (e.g. vigra) that don't need this and hence don't suffer from similar RAM issues.
If you are still interested, I could change the script that produced the numbers and upload the features I have used.
lesteve commentedon Jan 30, 2017
Please do. The memory consumption you observe is strange: as you say your memory usage looks like your input data gets copied
n_estimatorstimes which I don't think is expected or at the very least deserves further investigation. The easier you make the problem easy to reproduce the better feedback you will get. Bonus points if you can reproduce the problem on a randomly generated dataset.glouppe commentedon Jan 30, 2017
Are the labels provided as int? with a proper shape of
(n_samples,)?(misformatting the
yarray might result in doing multi-output random forests, which eats a lot more of ram)constantinpape commentedon Jan 30, 2017
@lesteve : Thanks for your feedback, I will update the script and try to reproduce the issue with a more generic dataset later.
@glouppe: the labels are provided as
uint8and their shape is(11501,).glouppe commentedon Jan 30, 2017
How many classes do you have? Can you check that
model.classes_is as you expect? (the size of node is linear withlen(model.classes_), and the number of nodes might grow up ton_samples)constantinpape commentedon Jan 30, 2017
There are 4 different classes in the labels and the value of
rf.classes_is[0,1,2,3](this is also who they are presented in the labels).glouppe commentedon Jan 30, 2017
Could you check if you see the same issue with sklearn 0.17? Just to quickly rule out whether we would have introduced a regression somewhere.
constantinpape commentedon Jan 30, 2017
I have tried to reproduce the behaviour with a standard dataset and used the digits dataset.
Interestingly, there the issue does not occur (the max RAM usage is approximately constant with the trees). I have already tried some obvious things like converting my data to the same types as the digits data, but that hasn't helped:
I have updated the script I used for profiling:
https://github.com/constantinpape/rf_benchmarks/blob/master/scripts/profile_sklearn_mem_v2.py
I am uploading the data used for my experiments now (everything should be uploaded in ~2 hrs).
https://drive.google.com/open?id=0B4_sYa95eLJ1NHpZOU45V0NJeDA
@glouppe I have rechecked, the behaviour is the same in 0,17.
constantinpape commentedon Jan 30, 2017
The upload is finished. Let me know if I can offer you any more help.
Also, I should mention that I have run into similar RAM problems with the Random Forest Implementation in the past, but I have never bothered to actually profile it before.
Anyway, if you are interested in a different dataset that reproduces these errors I could probably find some (though it won't be generic either).
jnothman commentedon Jan 30, 2017
constantinpape commentedon Jan 30, 2017
@jnothman That's an interesting point, however I don't really see how it should affect the RAM usage during prediction as observed. After all the trees should simply be walked to the corresponding leaves for all test instances.
Anyway I will give training forests with reduced complexity a try tomorrow.
I will also profile the two RF implementations I am benchmarking against, maybe they are showing the same effects and I simply haven't noticed because they are implemented in a fashion that is more memory efficient overall.
jnothman commentedon Jan 31, 2017
lesteve commentedon Jan 31, 2017
I can reproduce the behaviour. AFAICT this is due to:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/ensemble/forest.py#L569
We create a list of
n_estimatorsindividual proba prediction, hence the memory usage.Excerpt of the output with memory_profiler with
n_estimators=10:The memory increment matches the back of the envelope calculation for the size of
all_proba:constantinpape commentedon Jan 31, 2017
@lesteve: I see, so if I understand this correctly, the proble is that for each tree the probability array is pre-allocated. Probably this is done, because the parallelisation is done over the trees.
Note that I think this issue could be fixed by parallelising over the instances instead.
Indeed, I have rerun the profiling also for the vigra random forest (which parallelises prediction over the instances) and there the ram issue does not occur:
jnothman commentedon Jan 31, 2017