Lightgbm C++接口使用(一)

前言???

Lightgbm 是微软一款开源的随机森林学习库,该库 的牛逼之处就不用我们说明 啦。但是 目前网上关于Lightgbm的教程都是基于python或者R语言的,关于如何使用Lightgbm的C/C++接口却少有资料。刚好手上有个项目需要在线部署决策树模型,然而tensorflow 的效率不怎么好,python版的预测速度为1000条/s,而C++版本的预测速度为10000条/s。鉴于Tensorflow太“臃肿”,而LightGBM在决策树库里面很“轻”很“快”,因此我们想尝试使用Lightgbm的C++版本,看能不能提高预测性能。

官网并没有显式说明Lightgbm会提供C++接口,但实际上如果我们查看源码,我们可以知道Lightgbm就是使用C/C++编写的。因此,Lightgbm本身就有现成的C /C++ api,只不过官方没有给出这些api的使用方法。

但是!有源码一切都好办,尤其是Lightgbm还提供一个lightgbm可执行文件的main.cpp,通过分析该cpp,我们就可以很容易的知道,训练、预测应该使用那些函数。

步骤

OK,废话少说嘛,首先搭建环境。

1.下载源码:

git clone https://github.com/jmhIcoding/LightGBM.git ; cd LightGBM

该版本源码是从https://github.com/Microsoft/LightGBM.git fork出来 的,提供本链接的原因主要在于:为今后所有教程提供一个统一的代码版本,如果直接从官网clone下来 当官网release 新版本 而且改动较大时 ,教程介绍的相关功能可能会异常。

因此 强烈建议 参照本博客的朋友们 统一LightGBM版本。

2.编译官网版本,检测系统环境是否满足条件。

mkdir build;cd build

生产编译文件:

cmake ..

编译:

sudo make install

如果一切顺利的话,将没有任何报错信息。
测试一下:

lightgbm

如果输出以下信息,说明编译成功:

[LightGBM] [Info] Finished loading parameters
[LightGBM] [Fatal] No training/prediction data, application quit
Met Exceptions:
No training/prediction data, application quit

3.自定义代码

现在,我们想把开发自已的基于lightgbm的程序,那么应该怎么做呢?

一个最简单的方法是:切换到LightGBM的src目录,将其中的main.cpp修改即可。

该文件的路径:
假设LightGBM文件夹在XXX目录,则main.cpp的位置:

XXX/LightGBM/src/main.cpp

原代码内容:

#include <iostream>
#include <LightGBM/application.h>

int main(int argc, char** argv) {
  try {
    LightGBM::Application app(argc, argv);
    app.Run();
  }
  catch (const std::exception& ex) {
    std::cerr << "Met Exceptions:" << std::endl;
    std::cerr << ex.what() << std::endl;
    exit(-1);
  }
  catch (const std::string& ex) {
    std::cerr << "Met Exceptions:" << std::endl;
    std::cerr << ex << std::endl;
    exit(-1);
  }
  catch (...) {
    std::cerr << "Unknown Exceptions" << std::endl;
    exit(-1);
  }
}

增加一些测试代码:

#include <iostream>
#include <LightGBM/application.h>
#include <stdio.h>
int main(int argc, char** argv) {
    printf("Hello this is code is added by me....!!!!!!!!!!!!!!!!!!!!\n");
    printf("Nice..!!!!!!!!!!!!!!!!\n");
  try {
    LightGBM::Application app(argc, argv);
    app.Run();
  }
  catch (const std::exception& ex)
  {  
    std::cerr << "Met Exceptions:" << std::endl;
    std::cerr << ex.what() << std::endl;
    exit(-1);
  }
  catch (const std::string& ex) {
    std::cerr << "Met Exceptions:" << std::endl;
    std::cerr << ex << std::endl;
    exit(-1);
  }
  catch (...) {
    std::cerr << "Unknown Exceptions" << std::endl;
    exit(-1);
  }
}

主要加入两条printf语句。
然后 进行刚刚的build目录:

cd XXX/LightGBM/build/

直接make install即可

sudo make install

编译输出为:

ubuntu@gpu03:~/LightGBM/build$ sudo make install
[ 49%] Built target lightgbm
[100%] Built target _lightgbm
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/lightgbm
-- Up-to-date: /usr/local/lib/lib_lightgbm.so
-- Up-to-date: /usr/local/include/LightGBM
-- Up-to-date: /usr/local/include/LightGBM/export.h
-- Up-to-date: /usr/local/include/LightGBM/dataset.h
-- Up-to-date: /usr/local/include/LightGBM/R_object_helper.h
-- Up-to-date: /usr/local/include/LightGBM/metric.h
-- Up-to-date: /usr/local/include/LightGBM/tree.h
-- Up-to-date: /usr/local/include/LightGBM/application.h
-- Up-to-date: /usr/local/include/LightGBM/feature_group.h
-- Up-to-date: /usr/local/include/LightGBM/tree_learner.h
-- Up-to-date: /usr/local/include/LightGBM/objective_function.h
-- Up-to-date: /usr/local/include/LightGBM/network.h
-- Up-to-date: /usr/local/include/LightGBM/utils
-- Up-to-date: /usr/local/include/LightGBM/utils/pipeline_reader.h
-- Up-to-date: /usr/local/include/LightGBM/utils/text_reader.h
-- Up-to-date: /usr/local/include/LightGBM/utils/file_io.h
-- Up-to-date: /usr/local/include/LightGBM/utils/threading.h
-- Up-to-date: /usr/local/include/LightGBM/utils/random.h
-- Up-to-date: /usr/local/include/LightGBM/utils/log.h
-- Up-to-date: /usr/local/include/LightGBM/utils/openmp_wrapper.h
-- Up-to-date: /usr/local/include/LightGBM/utils/array_args.h
-- Up-to-date: /usr/local/include/LightGBM/utils/common.h
-- Up-to-date: /usr/local/include/LightGBM/lightgbm_R.h
-- Up-to-date: /usr/local/include/LightGBM/dataset_loader.h
-- Up-to-date: /usr/local/include/LightGBM/boosting.h
-- Up-to-date: /usr/local/include/LightGBM/c_api.h
-- Up-to-date: /usr/local/include/LightGBM/json11.hpp
-- Up-to-date: /usr/local/include/LightGBM/bin.h
-- Up-to-date: /usr/local/include/LightGBM/meta.h
-- Up-to-date: /usr/local/include/LightGBM/config.h
-- Up-to-date: /usr/local/include/LightGBM/prediction_early_stop.h

可以看出,make相当“智能”,并没有重新编译所有的代码,而是只把受main.cpp更新影响的目标重新编译了。
ok,我们运行一波:

ubuntu@gpu03:~/LightGBM/build$ lightgbm
Hello this is code is added by me....!!!!!!!!!!!!!!!!!!!!
Nice..!!!!!!!!!!!!!!!!
[LightGBM] [Info] Finished loading parameters
[LightGBM] [Fatal] No training/prediction data, application quit
Met Exceptions:
No training/prediction data, application quit

可以看到 ,已经打印出那两条测试信息。。。

OK!

猜你喜欢

转载自blog.csdn.net/jmh1996/article/details/80699837