ITADN

Output Mismatch Between Converted C Code and Python When Using m2cgen to Export XGBoost Regressor

#595OpenJIA-CHANG-0503 创建于 2025-06-02
J
JIA-CHANG-0503commented
Hi, I trained a regression model using xgboost.XGBRegressor, and I'm trying to convert it into C code using m2cgen. Everything works fine when the model is small — the output from the generated C code matches the Python output. Versions: XGBoost: 1.6.2 m2cgen: 0.10.0 `import os import numpy as np import xgboost as xgb from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error import matplotlib.pyplot as plt from sklearn.utils import shuffle import m2cgen as m2c import sys sys.setrecursionlimit(2147430647) model = xgb.XGBRegressor( objective='reg:squarederror', n_estimators=430, learning_rate=0.297888899046473, max_depth=4, #subsample=0.8, #colsample_bytree=0.8, tree_method='gpu_hist' ) # train model model.fit(X_train, y_train) # predict y_pred = model.predict(X_test) c_code = m2c.export_to_c(model) plt.show() with open('xgboost.c', 'w') as f: f.write(c_code) print("C code generated successfully!")` With the following model parameters, I was able to successfully convert the model to C and verify that the outputs are consistent between Python and C. Below are comparison images of the outputs: ![Image](https://github.com/user-attachments/assets/bf69b3b8-6c1a-4b86-b64e-0acb63f5e606) However, when I increased n_estimators to 660 (while keeping all other parameters the same), the output from the generated C code no longer matches the Python result. I'm not sure what's causing this issue. All tests were conducted using the exact same input data for prediction. Have you encountered this issue before, or do you know how to fix it? The image below shows the output mismatch: ![Image](https://github.com/user-attachments/assets/c806e3d7-ee49-4e48-ba6c-02fe9f8761c3) Thanks in advance for your help!
0 条评论