Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue of loading a Python trained LGB regression model using C-API for prediction. #4021

Closed
BladeCoda opened this issue Feb 25, 2021 · 5 comments
Labels

Comments

@BladeCoda
Copy link

BladeCoda commented Feb 25, 2021

Hello, I get a issue while using C API to load a LGB regression model which is trained in python(python 3.6, lightgbm 3.1.1).
The result predicted by C API loaded model is greatly differs from which is directly predicted by python library.

Here is how I train the regression model and do further prediction in python (brief code):

lgb_train = lgb.Dataset(train_feature, train_target)   
params = {
	'boosting_type': 'gbdt',
	'objective': 'regression',
	'max_depth': 8,
	'num_leaves': 255,
	'learning_rate': 0.05,
	'feature_fraction': 0.5, 
	'verbose': -1 
}
print('Starting training...')
gbm = lgb.train(params, lgb_train, num_boost_round=1500)
gbm.save_model(model_path)

gbm = lgb.Booster(model_file=model_path)  
result_predict = gbm.predict(test_feature, num_iteration=gbm.best_iteration)`

The result predicted by the python loaded model is just fine. However, when I load this saved model using C-API for prediction, the predicted result is totally not as my expected. Here is the brief code about how I load the model for prediction:

  • I didn't post detailed features because there are too long, but I carefully check those features and ensured they are totally the same as what I used in python experiment.
#include "LightGBM/c_api.h"
#include <string>
#include <pthread.h>

class LightGbmAlgorithm
{
public:
    LightGbmAlgorithm()
    {
	    ;
    }

    ~LightGbmAlgorithm()
    {
        LGBM_BoosterFree(this->handle);
        LGBM_FastConfigFree(this->fastConfig);
    }
	
	int initConfig(int prediceType, int dataType, int inputLength, int startIteration, int numIteration)
	{
	    if (0 != LGBM_BoosterPredictForMatSingleRowFastInit(this->handle, prediceType, startIteration,
                                                            numIteration, dataType, inputLength, "", &(this->fastConfig)))
		{
			printf("lightgbm predict config failed [%s]......\n", LastErrorMsg());
			return -1;
		}
		return 0;
	}


    int handleData(void *ptrFeature, void *ptrOutput)
    {
        int64_t outLength = 0;
        INT32 dataType = 0;
        if (0 != LGBM_BoosterPredictForMatSingleRowFast(fastConfig, ptrFeature, &outLength, (double *)ptrOutput))
        {
            printf("lightgbm predict failed [%s]......\n", LastErrorMsg());
            return -1;
        }
        return (INT32)outLength;
    }

    INT32 loadModel(const char *modelPath)
    {
        return LGBM_BoosterCreateFromModelfile(modelPath, &(this->num_iterations), &(this->handle));
    }

private:
    BoosterHandle handle;
    INT32 num_iterations;        // Number of iterations of this booster
    FastConfigHandle fastConfig;
};


int main()
{
    string path = "path to model";
    LightGbmAlgorithm *algObject = new LightGbmAlgorithm();
	algObject->loadModel(path.c_str());
	if ( 0 != algObject->initConfig(C_API_PREDICT_NORMAL, C_API_DTYPE_FLOAT64, 108, 0, -1))
	{
	    _exit(1);
	}
	double data = "features to send";
	double result = "result to save";
	handleData(&data, &result);
	printf("result = {%lf}", result);
}

In fact, I already successfully test a classification task using C-API loaded python model, and the predicted result is corrected. But when I use the same code to load a regression model, it goes wrong.

Is there something I omitted to load a python trained regression model using C-API?

@JackYangzg
Copy link

I have the same problem. using python to train model and predict using c api for regression task.
the c api output is different with the python output.

It's a Bug?

@shiyu1994
Copy link
Collaborator

@BladeCoda @JackYangzg Thanks for using LightGBM. I think this is related to #3778. If the model is produced with the released python package (version 3.1.1), and loaded by C_API from latest master branch, there can be a problem. Could you please try if the fix in #4056 resolves your problems?

@StrikerRUS
Copy link
Collaborator

@BladeCoda The above-mentioned fix is in 3.2.0 release, so you can check it.

@no-response
Copy link

no-response bot commented Apr 25, 2021

This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one. Thank you for taking the time to improve LightGBM!

@no-response no-response bot closed this as completed Apr 25, 2021
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants