skops Documentation

repository·main·Indexed 19 days ago

https://github.com/skops-dev/skops

A toolset for machine learning in production, focusing on the secure persistence of scikit-learn models and the creation of model cards for the Hugging Face Hub. It provides skops.io for safer alternatives to pickle, skops.card for generating and managing model documentation, and skops.hf_hub for integrating models with the Hugging Face ecosystem.

Tokens
6K
Snippets
20
Records
39
Agent score
68%

What's inside skops

  1. Overview of skops features

    main

    skops is a Python library designed to help share scikit-learn based models and deploy them to production. It provides two primary modules:

    • skops.io: Provides secure persistence for scikit-learn estimators and other objects without relying on the insecure pickle module.
    • skops.card: Provides tools to create model cards that explain a model's purpose and usage. These cards are designed to be stored as README.md files on the Hugging Face Hub, featuring pre-populated metadata that allows the Hub to understand the model.
  2. Create a Hugging Face model repository for scikit-learn models

    main

    The Skops Model Card Creator provides a streamlined interface to prepare, edit, and upload scikit-learn models to the Hugging Face Hub along with a professional model card. The process follows three main steps:

    1. Prepare the model repository: Upload your model, input data, define task types, and select a template.
    2. Edit the model card: Use a visual editor to customize the documentation, sections, and metrics.
    3. Create the model repository: Authenticate with Hugging Face and finalize the repository creation.

    This tool is ideal for users who want to host scikit-learn or compatible models (like XGBoost with the sklearn wrapper) on Hugging Face with minimal manual markdown editing.

  3. Understand skops version compatibility

    main

    The skops persistence format uses a protocol version (found in the file's schema) to manage updates.

    • Forward Compatibility: skops ensures that files created with older protocol versions will continue to load in newer versions of skops. If an older version contains a known bug, skops will issue a warning during loading.
    • Best Practice: Periodically load and dump old skops files using the latest version of skops to upgrade them to the newest protocol version.
    • Warning: Only use official release versions of skops to create files. Using development versions (e.g., from the main git branch) may result in files that are incompatible with future releases.
  4. Convert models to ONNX for deployment

    main
    For deployment scenarios where ONNX is preferred, skops aims to improve the experience of converting scikit-learn models to the ONNX format. While tools like skl2onnx exist, skops focuses on providing better support for complex estimators such as Pipelines and ColumnTransformers, and improving the ability to check if a specific model is compatible with ONNX conversion.
  5. Understand the structure of the default skops model card template

    main

    The default model card template in skops uses Jinja2-style placeholders to generate a standardized documentation file for machine learning models. When a model card is generated, the following sections are populated using the provided card_data:

    • Model description: A high-level overview of the model.
    • Intended uses & limitations: Details on how the model should and should not be used.
    • Training Procedure: Includes a collapsible section for Hyperparameters (rendered via hyperparameter_table) and a Model Plot section.
    • Evaluation Results: Details regarding the evaluation process (eval_methods) and the resulting metrics (eval_results).
    • Model Card Authors: List of contributors.
    • Model Card Contact: Contact information for the authors.
    • Citation: A BibTeX block for citing the model.

    If specific data is missing, the template defaults to displaying [More Information Needed] for several fields.

  6. Roadmap: C extension type support

    main

    Currently, skops cannot persist or load arbitrary C extension types directly.

    Workaround: You can persist these types if a Python object wraps them and implements the __getstate__ and __setstate__ methods to handle the persistence logic.

    Future Plans: skops plans to develop an API to help third-party libraries make their C extension types natively compatible with skops.

  7. Use the .skops format for secure model persistence

    main
    To avoid the security risks associated with standard Python pickles, skops provides a dedicated .skops format for persisting scikit-learn models. This format is stable, production-ready, and supports a wide range of models, including those outside of the scikit-learn ecosystem. It is designed as a safer alternative for deploying models in production environments.
  8. Understand Model Card content and structure

    main

    A model card in skops is a README.md file that documents a model's purpose, training, and limitations.

    skops provides several default free-text slots:

    • Model description: A description of the model.
    • Intended uses & limitations: Intended use, limitations, potential biases, and domain-specific risks.
    • How to Get Started with the Model: Code snippets for loading and using the model.
    • Model Card Authors: Authors of the card.
    • Model Card Contact: Contact information for questions.
    • Citation: Bibtex style citations.
    • Evaluation Results: Results that are parsed as a table.

    Additionally, skops automatically generates:

    • Hyperparameters: The model's hyperparameters.
    • Model Plot: A diagram of the model (useful for complex sklearn.pipeline.Pipeline objects).
  9. Integrate with Hugging Face Hub using skops.hf_hub

    main
    The skops.hf_hub module provides utilities for integrating scikit-learn models with the Hugging Face Hub. This includes functionality for creating model repositories, uploading models and input data, and managing model card templates for Hugging Face repositories.
  10. Generate model cards for scikit-learn models

    main
    The skops library includes tools to generate model cards, which provide documentation and metadata for machine learning models. These model cards are designed to have minimal external dependencies, making them suitable for potential adoption by upstream scikit-learn. Future developments include an application to create or modify model cards, allowing users to upload models and data to generate visualizations and inspections.