Skip to main content

Register, retrieve and get metadata from machine learning models.

Project description

ml-registry

Register, manage, and track machine learning components easily, such as PyTorch models and optimizers. You can retrieve component metadata, inspect signatures, and ensure instance integrity through deterministic hashes.

Introduction

Tracking machine learning components can be challenging, especially when you have to name them, track their parameters, and ensure the instance you're using matches the one you trained. This library addresses these issues by providing a simple way to register, manage, and track machine learning components, such as models, optimizers, and datasets. It uses cryptographic hashes to create unique identifiers for components based on their names, signatures, and parameters.

Installation

Install the package with pip:

pip install mlregistry

Using conda:

conda install pip
pip install mlregistry

Example

Suppose you have a Perceptron model built with PyTorch. To start using the registry, import the Registry class and register the class you want to track:

from models import Perceptron
from mlregistry import Registry

# Register components
Registry.register(Perceptron)

The Registry class injects a metadata factory into the Perceptron model. This metadata includes:

  • Model name: Used to retrieve the model instance from the registry and recognize it during serialization.
  • Unique hash: Useful for identifying the model instance locally, based on the model’s name, signature, and constructor parameters.
  • Arguments: A tuple with positional and keyword arguments for reconstructing the model instance.
  • Signature: Includes model annotations, which is useful for exposing the model’s configuration and usage in request-response APIs.
from mlregistry import getmetadata, gethash, getsignature

registry = Registry() #Create a registry instance before or after registry of classes. 
perceptron = Perceptron(784, 256, 10, p=0.5, bias=True)

# Get metadata, hash, and signature of the model instance
hash = gethash(perceptron)
print(hash)  # e.g., "1a79a4d60de6718e8e5b326e338ae533"

metadata = getmetadata(perceptron)
print(metadata.arguments)  # {'input_size': 784, 'hidden_size': 256, 'output_size': 10, 'p': 0.5, 'bias': True}

signature = getsignature(perceptron)
print(signature)  # {input_size: int, hidden_size: int, output_size: int, p: float, bias: bool}

You can retrieve the model type from the registry:

model_type = registry.get('Perceptron')
model_instance = model_type(input_size=784, hidden_size=256, output_size=10, p=0.5, bias=True)

assert isinstance(model_instance, Perceptron)

This works with other components as well, like optimizers and datasets. For complex setups, consider creating a repository class to manage components and dependencies, simplifying pipeline persistence.

from torch.nn import Module, CrossEntropyLoss
from torch.optim import Optimizer, Adam
from torchvision.datasets import MNIST

class Container:
    models = Registry[Module]()
    criterions = Registry[Module]()
    optimizers = Registry[Optimizer](excluded_positions=[0], exclude_parameters={'params'})
    datasets = Registry[Dataset](excluded_positions=[0], exclude_parameters={'root', 'download'})

Container.models.register(Perceptron)
Container.optimizers.register(Adam)
Container.datasets.register(MNIST)

model = Perceptron(784, 256, 10, p=0.5, bias=True)
criterion = CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=1e-3)
dataset = MNIST('data', train=True, download=True)

dataset_metadata = getmetadata(dataset)
print(dataset_metadata)  # Serialize dataset metadata

optimizer_metadata = getmetadata(optimizer)
print(optimizer_metadata)  # Excluded parameters like 'params' or the first positional argument won’t appear in metadata

This approach enables component tracking and serialization without worrying about naming conflicts or manual parameter tracking.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlregistry-1.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlregistry-1.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file mlregistry-1.1.0.tar.gz.

File metadata

  • Download URL: mlregistry-1.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.1 Linux/6.8.0-1017-azure

File hashes

Hashes for mlregistry-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f135ee283db7207a8dc8ded2d54722851d2769beead9f7a5efd9c31c69739772
MD5 d5c5514651132ebebb2e1ecfc189b9b8
BLAKE2b-256 fa492c956664952b5a7c78a6a83c4dbc84dfd1caf230cc09b118d02142682ca0

See more details on using hashes here.

File details

Details for the file mlregistry-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mlregistry-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.1 Linux/6.8.0-1017-azure

File hashes

Hashes for mlregistry-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2f7596dac1bd736a23e910c6d8c2c44f9d74caf1ad53e36e5399e445f921903
MD5 2c2bbc50e20093fcb376773d9d0d8c7b
BLAKE2b-256 9ba92a0eed806f90724e2ef1cb26654ad294b92c588f4f6140a8f633a5c49367

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page