Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

python - sklearn, joblib: Loading large model causes memory error

I have trained and saved an sklearn RandomForestClassifier in the following way:

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)
clf = RandomForestClassifier(max_depth=2, random_state=0).fit(X, y)

import joblib
joblib.dump(clf, "D:/mymodel.gz", compress=3)

The final model (as saved on HDD) is about 6 GB large.

When I try to load the model again using:

clf_loaded = joblib.load("D:/mymodel.gz")
pred = clf_loaded.predict(X)

I get a memory error even though my available RAM is just under 60 GB.

It seems that joblib.load() requires a huge amount of RAM in excess of the original file size (6 GB).

Q: What are alternative options to save an sklearn model locally and load it again without consuming massive RAM space?

As far as I know, pickle will not outperform joblib and is per docs not recommended for sklearn models.

I checked some other questions such as this, this, this, and this but could not find a workable solution.

question from:https://stackoverflow.com/questions/66067303/sklearn-joblib-loading-large-model-causes-memory-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...