示例#1
0
文件: main.cpp 项目: jakisou/gbrt
int main(int argc, char ** argv)
{
    std::string input_file = "";
    std::string input_type = "l2r";
    std::string config_file = "./gbrt.conf";
    std::string act_type = "";
    std::string model_file = "./gbrt.model";
    int dimention = 1024;

    //----parse command line
    int opt_c;
    while ( (opt_c = getopt( argc, argv, "d:f:i:c:m:tp")) != EOF )
    {
        switch (opt_c)
        {
        case 'i':
            input_file = optarg;
            break;
        case 'f':
            input_type = optarg;
            break;
        case 'c':
            config_file = optarg;
            break;
        case 'm':
            model_file = optarg;
            break;
        case 't':
            act_type = "t";
            break;
        case 'p':
            act_type = "p";
            break;
        case 'd':
            dimention = atoi(optarg);
        default:
            break;
        }

    }

    //check options
    if ( act_type.length() == 0
        || input_file.length() == 0 )
    {
        std::cerr << "miss parameter!!" << endl;
        Usage();
        return 1;
    }
    else
    {
        cout << "parameters--------" << endl;
        cout << "  input file: " << input_file << endl;
        cout << "  input format (cvs, l2r): " << input_type<< endl;
        cout << "  config file: " << config_file << endl;
        cout << "  act type(t for train,p for predict): " << act_type << endl;
        cout << "  model file: " << model_file << endl;
        cout << "  max dimention(for L2R format): " << dimention << endl;
        cout << endl;
    }

    Data data;
    DataReader dr;

    if ( input_type == "cvs")
    {
         if ( false == dr.ReadDataFromCVS(input_file, data))
         {
             std::cerr << "error: read CVS file failed! " << input_file << std::endl;
             return 1;
         }
    }
    else
    {
        if ( false == dr.ReadDataFromL2R(input_file, data, dimention))
        {
         std::cerr << "error: read L2R file failed! " << input_file << std::endl;
         return 1;
        }
    }

    GBDT gbdt;

    if (!gbdt.LoadConfig(config_file))
        return 1;

    if (act_type == "t")
    {
        gbdt.Init();
        gbdt.Train(data);
        gbdt.SaveWeights(model_file);
    }
    else if( act_type == "p" )
    {
        T_VECTOR predictions;
        gbdt.LoadWeights(model_file);
        gbdt.PredictAllOutputs(data, predictions);

        //----output prediction----
        std::ifstream fs;
        fs.open(input_file.c_str(), std::ios_base::in);

        std::string prediction_file = input_file + ".prediction";
        std::fstream fs_out;
        fs_out.open(prediction_file.c_str(), std::ios_base::out);

        std::string strLine;
        unsigned int line_num = 0;
        while (getline(fs, strLine))
        {
             if (strLine.length() < 2)
             {
                 continue;
             }
             fs_out<< predictions[line_num] << std::endl;
             //for debug
             //cout << strLine << "\t" << predictions[line_num] << std::endl;
             line_num++;
        }

        fs.close();

    }


    return 0;
}