m2cgen (Model 2 Code Generator)

repository·master·Indexed 25 days ago

https://github.com/bayeswitnesses/m2cgen

A lightweight library that transpiles trained statistical models from libraries such as scikit-learn, XGBoost, and LightGBM into native code. It supports multiple target languages including C, C#, Java, Go, JavaScript, Python, Rust, and others via a Python API and a command-line interface.

Tokens
1.4K
Snippets
3
Records
8
Agent score
34%

What's inside m2cgen

  1. Troubleshoot m2cgen errors

    master

    RecursionError: maximum recursion depth exceeded

    This occurs when generating code for ensemble models.

    • Solution 1: Reduce the number of trained estimators in the model.
    • Solution 2: Increase the maximum recursion depth using sys.setrecursionlimit(<new_depth>) in your Python environment.

    ImportError: No module named <module_name_here>

    This happens when the pickle protocol cannot deserialize the model object.

    • Solution: Ensure that the package providing the model's class definition is installed in your environment and that the classes are defined at the top level of an importable module.

    Discrepancies in prediction results

    If the generated code produces different results than the original Python model:

    • Data Types: m2cgen works exclusively with float64 (double) data types. If your original model expects different types, try casting your input data to float64 manually.
    • Floating-point arithmetic: Small differences may occur due to how different languages implement floating-point arithmetic.
  2. Transpile a model to Java using the Python API

    master

    You can use the m2cgen Python library to export a trained model directly into native code. For example, to export a scikit-learn LinearRegression model to Java:

    from sklearn.datasets import load_diabetes
    from sklearn import linear_model
    import m2cgen as m2c
    
    X, y = load_diabetes(return_X_y=True)
    
    estimator = linear_model.LinearRegression()
    estimator.fit(X, y)
    
    code = m2c.export_to_java(estimator)
  3. Understand classification output behavior

    master

    The output format of the generated code depends on the model type:

    Linear / Linear SVM / Kernel SVM

    • Binary: Scalar value; signed distance of the sample to the hyperplane for the second class.
    • Multiclass: Vector value; signed distance of the sample to the hyperplane per each class.
    • Note: Consistent with LinearClassifierMixin.decision_function.

    SVM

    • Outlier detection: Scalar value; signed distance of the sample to the separating hyperplane (positive for inlier, negative for outlier).
    • Binary: Scalar value; signed distance of the sample to the hyperplane for the second class.
    • Multiclass: Vector value; one-vs-one score for each class, shape (n_samples, n_classes * (n_classes-1) / 2).
    • Note: Consistent with BaseSVC.decision_function when decision_function_shape is set to ovo.

    Tree / Random Forest / Boosting

    • Binary: Vector value; class probabilities.
    • Multiclass: Vector value; class probabilities.
    • Note: Consistent with the predict_proba method of the original estimator (e.g., DecisionTreeClassifier, RandomForestClassifier, XGBClassifier, LGBMClassifier).
  4. Use the m2cgen CLI to generate code from a pickle file

    master

    The m2cgen CLI allows you to generate code from serialized model objects (pickle protocol).

    Note: For unpickling to work, the classes of the serialized model must be defined in the top level of an importable module in the environment where you run the CLI.

    $ m2cgen <pickle_file> --language <language> [--indent <indent>] [--function_name <function_name>] [--class_name <class_name>] [--module_name <module_name>] [--package_name <package_name>] [--namespace <namespace>] [--recursion-limit <recursion_limit>]

    Piping is also supported:

    $ cat <pickle_file> | m2cgen --language <language>
  5. m2cgen CLI flags and options

    master

    The following flags are available for the m2cgen CLI tool:

    FlagShortDescription
    --language-lRequired. The target language (e.g., python, java, c, go, javascript, c_sharp, rust, etc.).
    --function_name-fnName of the generated function.
    --class_name-cnName of the generated class (language dependent).
    --package_name-pnPackage name for the generated code (language dependent).
    --module_name-mnModule name for the generated code (language dependent).
    --namespace-nsNamespace for the generated code (language dependent).
    --indent-iIndentation level for the generated code (default: 4).
    --recursion-limit-rlSets the maximum depth of the Python interpreter stack (default: max int32).
    --pickle-lib-plThe library used to load the model file. Choices: pickle, joblib (default: pickle).
    --version-vShow version information.