ONNX-Export von LightGBM
Download Python Samples Ein Zip-Archiv mit allen Samples finden Sie hier: Beispiele zum ONNX-Export |
LGBM Regressor
from lightgbm import LGBMRegressor
from skl2onnx.common.data_types import FloatTensorType
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from onnxmltools.convert import convert_lightgbm
import onnx
# # Generate data for regression
X, y = make_regression(n_samples=300, n_features=10, n_informative=10, n_targets=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)
# # Construct LightGBM-Model
model = LGBMRegressor(objective='regression', max_depth=31, learning_rate=0.05, n_estimators=300)
model.fit(X_train, y_train,eval_set=[(X_test,y_test),(X_train,y_train)],eval_metric='rmse', verbose=20)
# # Convert model to ONNX
onnxfile = 'lgbm-regressor.onnx'
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
onnx_model = convert_lightgbm(model, initial_types=initial_type, target_opset=12)
onnx.checker.check_model(onnx_model)
with open(onnxfile, "wb") as f:
f.write( onnx_model.SerializeToString())
f.close()
exit()
LGBM Regressor (gamma objective)
import numpy as np
from lightgbm import LGBMRegressor
from skl2onnx.common.data_types import FloatTensorType
from sklearn.model_selection import train_test_split
from onnxmltools.convert import convert_lightgbm
import onnx
# # Generate data for regression
N_ROWS = 1000
N_COLS = 4
X = np.random.randn(N_ROWS, N_COLS)
# # For 'poisson' and 'gamma' objective, all target values need to be non-negative
y = abs(np.random.randn(N_ROWS))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)
# # Construct LightGBM-Model
model = LGBMRegressor(objective='gamma', max_depth=-1, learning_rate=0.05, n_estimators=300)
model.fit(X_train, y_train,eval_set=[(X_test,y_test),(X_train,y_train)],eval_metric='rmse', verbose=20)
# # Convert model to ONNX
onnxfile = 'lgbm-regressor-gamma.onnx'
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
onnx_model = convert_lightgbm(model, initial_types=initial_type, target_opset=12)
onnx.checker.check_model(onnx_model)
with open(onnxfile, "wb") as f:
f.write( onnx_model.SerializeToString())
f.close()
exit()
LGBM Regressor (poisson objective)
import numpy as np
from lightgbm import LGBMRegressor
from skl2onnx.common.data_types import FloatTensorType
from sklearn.model_selection import train_test_split
from onnxmltools.convert import convert_lightgbm
import onnx
# # Generate data for regression
N_ROWS = 1000
N_COLS = 4
X = np.random.randn(N_ROWS, N_COLS)
# # For 'poisson' and 'gamma' objective, all target values need to be non-negative
y = abs(np.random.randn(N_ROWS))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)
# # Construct LightGBM-Model
model = LGBMRegressor(objective='poisson', max_depth=-1, learning_rate=0.05, n_estimators=300)
model.fit(X_train, y_train,eval_set=[(X_test,y_test),(X_train,y_train)],eval_metric='rmse', verbose=20)
# # Convert model to ONNX
onnxfile = 'lgbm-regressor-poisson.onnx'
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
onnx_model = convert_lightgbm(model, initial_types=initial_type, target_opset=12)
onnx.checker.check_model(onnx_model)
with open(onnxfile, "wb") as f:
f.write( onnx_model.SerializeToString())
f.close()
exit()
LGBM Classifier
import onnx
import onnx_graphsurgeon as gs
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
from skl2onnx.common.data_types import FloatTensorType
from onnxmltools.convert import convert_lightgbm
# # Load data for classification
X, y = load_iris(return_X_y = True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)
# # Construct LightGBM Model
model = LGBMClassifier(objective='multiclass', learning_rate=0.05, max_depth=-1, n_estimators=100, random_state=42)
model.fit(X_train,y_train,eval_set=[(X_test,y_test),(X_train,y_train)],verbose=20,eval_metric='logloss')
# # Convert model to ONNX
onnxfile = 'lgbm-iris.onnx'
initial_type = [('float_input', FloatTensorType([None, X.shape[1]]))]
# # Zipmap should be always turned off as it's not implemented in TF3800
onnx_model = convert_lightgbm(model, initial_types=initial_type, zipmap=False)
# # Manipulate graph to force its ONNX conformity (necessary to correct a bug in onnxmltools version 1.11.1)
graph = gs.import_onnx(onnx_model)
graph.name = "LGBMClassifier"
tree_node = [node for node in graph.nodes if node.op == "TreeEnsembleClassifier"][0]
tree_node.name = "TreeEnsembleClassifier"
tree_node.outputs = graph.outputs
tree_node.outputs[0].shape = [None]
tree_node.outputs[1].shape = [None, model.n_classes_]
# # Collect 2 artifacts of the converter
cast_node = [node for node in graph.nodes if node.op == "Cast"][0]
mul_node = [node for node in graph.nodes if node.op == "Mul"][0]
# # Clear outputs of these two nodes
mul_node.outputs.clear()
cast_node.outputs.clear()
# # Remove these nodes from the graph
graph.cleanup().toposort()
onnx_model = gs.export_onnx(graph)
nodes = onnx_model.graph.node
for node in nodes:
# # Modify node attributes.
if node.op_type == "TreeEnsembleClassifier":
node.domain = "ai.onnx.ml" # # Domain info is required for ONNX conformity
node.doc_string = "Converted from LGBMClassifier() model"
# # Export to ONNX file
onnx.checker.check_model(onnx_model)
with open(onnxfile, "wb") as f:
f.write( onnx_model.SerializeToString())
f.close()
exit()