SharpLearning icon indicating copy to clipboard operation
SharpLearning copied to clipboard

Looking for an example for loading from a Stream source

Open Yandychang1 opened this issue 1 year ago • 4 comments

I am looking for an example to load a trained XGBoostClassifier model from a Stream rather than from a file. I am planning to save it as an embedded resource, so I want the ability to load my model from a DLL.

Yandychang1 avatar Dec 08 '23 00:12 Yandychang1

I’m early in exploring this repo but looking for the same capability. I think maybe it can be done with the methods that load from a TextReader:

using TextReader reader = new StreamReader(GetEmbeddedResourceStream()); ….Load(() => reader);

madelson avatar Dec 14 '23 12:12 madelson

It seems to me that for xgboost classifiers and regressors, this may not apply, at least for now.

Yandychang1 avatar Jan 12 '24 02:01 Yandychang1

Hi @Yandychang1

Not sure if you already found an alternative for this.

It is possible to load using a StreamReader for all models except for the XGBoost models sadly. The reason is that SharpLearning simply wraps XGBoost.net and uses the Booster class from that library to load the model from file. You can see the call here for the RegressionXGBoostModel: https://github.com/mdabros/SharpLearning/blob/master/src/SharpLearning.XGBoost/Models/RegressionXGBoostModel.cs#L96

From a quick look there does not seem to be an obvious way to instantiate the Booster class from a stream sadly. If you can find a way, I would be happy to add an overload for it in SharpLearning.

mdabros avatar Jan 20 '24 11:01 mdabros

I noticed the same thing, so far I have not found a way to fix this.

Yandychang1 avatar Jan 21 '24 15:01 Yandychang1