Skip to content

Random Forest: Memory increases linearly with n_estimators #8244

Closed

Description

@constantinpape

I have benchmarked different random forest implementation and found that its RAM consumption increases linearly with n_estimators:
https://github.com/constantinpape/rf_benchmarks#sklearn-ram-issues
This leads to memory errors for relatively small feature matrices, when using several estimators.
See https://github.com/constantinpape/rf_benchmarks/blob/master/scripts/profile_sklearn_mem.py
for the script I have used for profiling.
Probably there is a feature matrix for every tree happening.
I am using version 0.18.1.

Activity

  1. lesteve commented on Jan 30, 2017

    @lesteve
    Member

    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.

  2. glouppe commented on Jan 30, 2017

    @glouppe
    Contributor
  3. agramfort commented on Jan 30, 2017

    @agramfort
    Member

    indeed. It's expected.

  4. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    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.

  5. lesteve commented on Jan 30, 2017

    @lesteve
    Member

    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.

  6. glouppe commented on Jan 30, 2017

    @glouppe
    Contributor

    Are the labels provided as int? with a proper shape of (n_samples,)?

    (misformatting the y array might result in doing multi-output random forests, which eats a lot more of ram)

  7. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    @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,).

  8. glouppe commented on Jan 30, 2017

    @glouppe
    Contributor

    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)

  9. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    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).

  10. glouppe commented on Jan 30, 2017

    @glouppe
    Contributor

    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.

  11. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    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:

    Max-Ram usage for dtype(features, labels)
    n_estimators float32, uint8 float64, uint8 float32, int64 float64, int64
    1 894.75 MB 1933.18 MB 894.11 MB 1933.68 MB
    2 1146.34 MB 2184.76 MB 1145.66 MB 2185.18 MB
    4 1634.82 MB 2673.25 MB 1634.07 MB 2673.68 MB
    5 1879.06 MB 2917.52 MB 1878.37 MB 2917.91 MB
    10 3100.17 MB 4138.61 MB 3099.48 MB 4139.04 MB
    25 6763.53 MB 7801.96 MB 6762.87 MB 7802.41 MB

    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.

  12. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    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).

  13. jnothman commented on Jan 30, 2017

    @jnothman
    Member
  14. constantinpape commented on Jan 30, 2017

    @constantinpape
    Author

    @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.

  15. jnothman commented on Jan 31, 2017

    @jnothman
    Member
  16. lesteve commented on Jan 31, 2017

    @lesteve
    Member

    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_estimators individual proba prediction, hence the memory usage.

    Excerpt of the output with memory_profiler with n_estimators=10:

       566 1625.527 MiB    0.000 MiB           n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs)
       567                             
       568                                     # Parallel loop
       569 1625.527 MiB    0.000 MiB           all_proba = Parallel(n_jobs=n_jobs, verbose=self.verbose,
       570 1625.527 MiB    0.000 MiB                                backend="threading")(
       571 4074.062 MiB 2448.535 MiB               delayed(parallel_helper)(e, 'predict_proba', X,
       572                                                                   check_input=False)
       573 4074.062 MiB    0.000 MiB               for e in self.estimators_)
       574 4074.062 MiB    0.000 MiB           print('len(all_proba):', len(all_proba))
       575 4074.062 MiB    0.000 MiB           print('all_proba[0].shape:', all_proba[0].shape)
    

    The memory increment matches the back of the envelope calculation for the size of all_proba:

    n_samples * itemsize_in_bytes * n_classes * n_estimators
    8000000   * 8                 * 4         * 10            ~= 2441 MiB
    
  17. constantinpape commented on Jan 31, 2017

    @constantinpape
    Author

    @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:

    n_estimatores vigra RF3 sklearn RF
    1 771.30 954.99
    2 771.07 1206.70
    4 770.89 1694.92
    5 771.03 1939.07
    10 773.66 3160.06
    25 775.52 6823.37
  18. jnothman commented on Jan 31, 2017

    @jnothman
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions