{"id":6018,"library":"onnxmltools","title":"ONNXMLTools","description":"ONNXMLTools facilitates the conversion of various machine learning models (e.g., scikit-learn, LightGBM, XGBoost, TensorFlow, SparkML) into the ONNX (Open Neural Network Exchange) format. This enables model interoperability across different frameworks and hardware. The current version is 1.16.0, and it maintains an active development cycle with new releases every few months.","status":"active","version":"1.16.0","language":"en","source_language":"en","source_url":"https://github.com/onnx/onnxmltools","tags":["onnx","machine-learning","model-conversion","interoperability","lightgbm","xgboost","scikit-learn","tensorflow"],"install":[{"cmd":"pip install onnxmltools","lang":"bash","label":"Base Installation"},{"cmd":"pip install onnxmltools onnx_lightgbm onnx_xgboost skl2onnx tf2onnx sparkml2onnx","lang":"bash","label":"Installation with common converters"}],"dependencies":[{"reason":"Core ONNX graph manipulation, definition, and validation.","package":"onnx","optional":false},{"reason":"Required for numerical operations, especially tensor handling.","package":"numpy","optional":false},{"reason":"Required for converting LightGBM models to ONNX.","package":"onnx_lightgbm","optional":true},{"reason":"Required for converting XGBoost models to ONNX.","package":"onnx_xgboost","optional":true},{"reason":"Required for converting scikit-learn models to ONNX.","package":"skl2onnx","optional":true},{"reason":"Required for converting TensorFlow and Keras models to ONNX.","package":"tf2onnx","optional":true},{"reason":"Required for converting SparkML models to ONNX.","package":"sparkml2onnx","optional":true}],"imports":[{"note":"While onnxmltools exposes conversion functions like `convert_lightgbm` directly, the corresponding framework-specific converter package (e.g., `onnx_lightgbm`) must be installed separately for the function to work.","symbol":"convert_lightgbm","correct":"from onnxmltools import convert_lightgbm"},{"note":"Used to define the input tensor types and shapes required by ONNX conversion functions.","symbol":"FloatTensorType","correct":"from onnxmltools.convert.common.data_types import FloatTensorType"}],"quickstart":{"code":"import lightgbm as lgb\nfrom onnxmltools import convert_lightgbm\nfrom onnxmltools.convert.common.data_types import FloatTensorType\nfrom onnx.checker import check_model\nfrom onnx import save\nimport numpy as np\n\n# Ensure required packages are installed\ntry:\n    import lightgbm # noqa: F401\n    import onnx_lightgbm # noqa: F401\n    import onnx # noqa: F401\nexcept ImportError as e:\n    print(f\"Skipping quickstart: Missing dependency. Please install lightgbm, onnx, and onnx_lightgbm. Error: {e}\")\n    exit()\n\n# 1. Train a LightGBM model\nX = np.array([[0, 0], [1, 1], [2, 2], [3, 3]], dtype=np.float32)\ny = np.array([0, 1, 1, 0], dtype=np.int32)\ngbm = lgb.LGBMClassifier(n_estimators=3, max_depth=2, learning_rate=0.1, random_state=42)\ngbm.fit(X, y)\n\n# 2. Define initial types for ONNX conversion\n# 'None' in FloatTensorType([None, 2]) means variable batch size\ninitial_type = [('float_input', FloatTensorType([None, 2]))]\n\n# 3. Convert to ONNX format, specifying a target opset (e.g., 17)\ntarget_opset = 17 # Or a lower opset depending on ONNX Runtime compatibility\nonnx_model = convert_lightgbm(gbm, initial_types=initial_type, target_opset=target_opset)\n\n# 4. Check the ONNX model for validity\ncheck_model(onnx_model)\n\n# 5. Save the ONNX model to a file\nsave(onnx_model, \"lightgbm_model.onnx\")\n\nprint(\"LightGBM model successfully converted to lightgbm_model.onnx\")","lang":"python","description":"This quickstart demonstrates converting a simple LightGBM classifier into the ONNX format. It highlights the use of `convert_lightgbm` and the importance of defining `initial_types` and `target_opset`. Remember to install `lightgbm`, `onnx`, and `onnx_lightgbm` separately for this to run."},"warnings":[{"fix":"Install the required converter package alongside `onnxmltools`. For example, for LightGBM models, use `pip install onnxmltools onnx_lightgbm lightgbm`.","message":"ONNXMLTools itself does not include the framework-specific converter packages (e.g., `skl2onnx`, `onnx_lightgbm`, `tf2onnx`). You must install these separately for the respective conversion functions to work.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully define `initial_types` using data types like `FloatTensorType`, `Int64TensorType`, etc., from `onnxmltools.convert.common.data_types`. Ensure the shape (e.g., `[None, num_features]`) matches your model's expected input.","message":"The `initial_types` parameter is mandatory for most conversion functions and defines the input tensor's name, type, and shape. Incorrect specification is a common source of errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Align `target_opset` with the capabilities of your ONNX Runtime environment and the features used in your original model. Consult the ONNX operator schema for supported opsets.","message":"Opset version compatibility is crucial. Choosing a `target_opset` that is too low might not support newer model features, while a `target_opset` that is too high might not be supported by your ONNX Runtime version.","severity":"breaking","affected_versions":"All versions"},{"fix":"Explicitly check the output tensor shape after conversion and adjust post-processing logic if necessary. Newer versions of `onnxmltools` and underlying converters aim to standardize this, but vigilance is recommended.","message":"For binary classification models, the output shape (e.g., probabilities) can sometimes vary between `[N, 1]` and `[N, 2]`, leading to discrepancies with expected ONNX Runtime outputs.","severity":"gotcha","affected_versions":">=1.13.0"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}