Skops
Skops is a set of tools designed for machine learning in production, focusing on model persistence, sharing, and documentation. It provides secure object dumping/loading, integrates with Hugging Face Hub for sharing, and helps generate Model Cards. The library is currently at version 0.13.0 and maintains a regular release cadence, typically monthly or bi-monthly, addressing bug fixes, enhancements, and security improvements.
Warnings
- breaking Loading untrusted models or Model Cards from external sources can lead to arbitrary code execution due to pickle's security vulnerabilities. Skops offers security mechanisms like the `trusted` argument for `skops.io.load` and `allow_insecure_loading` for `skops.card.Card`.
- gotcha Model persistence is highly sensitive to library versions. Models saved with one version of `skops`, `scikit-learn`, `numpy`, or other dependencies might not load or behave identically with significantly different versions.
- gotcha The `skops convert` CLI command, designed to convert model files (e.g., `joblib` to `skops`), is a 'best-effort' attempt and does not guarantee an exact, bit-for-bit identical conversion.
Install
-
pip install skops
Imports
- dump
from skops.io import dump
- load
from skops.io import load
- Card
from skops.card import Card
- push
from skops.hub import push
Quickstart
from sklearn.linear_model import LogisticRegression
from skops.io import dump, load
# Train a simple model
model = LogisticRegression(random_state=42)
model.fit([[0, 0], [0, 1], [1, 0], [1, 1]], [0, 0, 1, 1])
# Save the model securely
dump(model, 'model.skops')
# Load the model
loaded_model = load('model.skops')
# Make a prediction with the loaded model
prediction = loaded_model.predict([[0, 0]])
print(f"Original model class: {type(model).__name__}")
print(f"Loaded model class: {type(loaded_model).__name__}")
print(f"Prediction: {prediction[0]}")