Skip to main content

A simple package to track python objects.

Project description

MLRegistry

A simple package to track python objects based on the arguments they were created with. The documentation can be found here: https://mr-mapache.github.io/ml-registry/.

Table of contents:

Introduction

In certain scenarios, such as in machine learning, it's important to keep track of the objects created in your code and associate them with specific entities. For instance, a neural network is not only defined by its name but also by its hyperparameters. This package provides a streamlined way to register objects and retrieve them based on the arguments used during their creation, ensuring efficient tracking and management of these entities.

Installation

Install the package with pip:

pip install mlregistry

Example

Suppose you want to create a machine learning model and efficiently track its hyperparameters. Here's an example with a Perceptron class:

class Perceptron:
    def __init__(self, input_size: int, output_size: int):
        ...

Using the register function from the mlregistry package, you can easily achieve this:

from mlregistry import register

register(Perceptron)

Once registered, any new object initialized from the Perceptron class will automatically have its creation arguments stored. This makes it simple to track the hyperparameters and assign a unique identity to the object, based on its name and hyperparameters.

from mlregistry import getarguments
from mlregistry import gethash

model = Perceptron(input_size=10, output_size=1)
arguments = getarguments(model) 
hash = gethash(model)  # The hash acts as a locally unique identifier for the object

print(arguments) # {'input_size': 10, 'output_size': 1}
print(hash) # a8657a4057c4f7b3237aec904970630d

Notably, an object with the same name and identical arguments will always generate the same hash. This hash acts as a consistent local identifier, effectively treating machine learning models as entities with unique identities defined by their name and hyperparameters.

You can also register objects in a Registry instance. A Registry serves as a collection of types, allowing you to register and retrieve objects by their name.

Here’s an example:

from mlregistry import Registry


class Optimizer:
    def __init__(self, model_params, learning_rate: float):
        ...

registry = Registry[Optimizer]() # Use generics to have PEP484 type hints.
registry.register(Optimizer, excluded_args=[0], excluded_kwargs=['model_params']) 

In this example, the excluded_args and excluded_kwargs parameters are used to omit specific arguments from the hash calculation and the tracked parameters. These options are also available in the standalone register function.

Once registered, you can retrieve an object from the registry using its name:

optimizer = registry.get('Optimizer')(model_params={'param':'someparams'}, learning_rate=0.01)
optimizer_arguments = getarguments(optimizer)
print(optimizer_arguments) # {'learning_rate': 0.01} # model_params is excluded from the arguments

This feature is especially useful when you need to dynamically list available machine learning models, such as in a REST API, and create a model using only its name and hyperparameters:

print(registry.keys()) # ['Optimizer'] 
print(registry.signature('Optimizer')) # {'learning_rate': float}

The package includes additional functionality, which you can explore further in the documentation.

License

This project is licensed under the MIT License - Use it as you wish in your projects.

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.2.1.tar.gz (5.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.2.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mlregistry-1.2.1.tar.gz
Algorithm Hash digest
SHA256 2c30da9dd82a09b45357c9d16695d77878c35a9767e06145e10526d27a871e6f
MD5 d7eb502418d41e5d3580a2632e7daabe
BLAKE2b-256 4930496864d23c1b0d02d3db8636cecf3a57c988ed6de79bb8f22ae3f4ccc8dd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mlregistry-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9413a033556e42c785b9c47526acfad1502b8ee32d87c2f6f3e8c6bf64dc3b6f
MD5 923b13fb4e5882f6b1dcb397ee3bf6b4
BLAKE2b-256 90086684f03e9fb36a31e718e7a65c9d001ab450542d323db7aa338dd5384ee7

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