Skip to main content

Model Composer


PyPI version shields.io PyPI license PyPI pyversions Downloads Downloads

Motivation

This use-case prompted the development of model-composer:

  • You have two tensorflow models, one trained on weekday data and one trained on weekend data
  • You would like to compose a single tensorflow model that can be used to generate predictions for any day of the week.
  • You want the composed model to be natively defined in tensorflow - i.e. a single "computational graph" that can be easily loaded and used to make predictions.
  • You want a single composed model becasuse:
    • It is easier to maintain than having to implement the logic to compose the models in every service that needs to make predictions.
    • It ensures the performance of the composed model will remain consistent with a native tensorflow model of a similar complexity.
    • It is easier to deploy a single model than multiple models

Documentation

The official documentation is hosted on ReadTheDocs: https://model-composer.readthedocs.io/

Install

Using pip:

pip install model-composer

Extras

Make use of extras to install the model composer implementations that you need:

pip install model-composer[tensorflow]  # compose tensorflow models
pip install model-composer[cloudpathlib]  # load models from cloud storage
pip install model-composer[all]  # all extras

Quick start

Declare your composed model in a yaml file which defines the components and how they should be composed.

name: "ride_share_pricing"
components:
  - name: weekday_model
    path: weekday_model.tf
    type: tensorflow
    where:
      input: is_weekday
      operator: eq
      value: true
  - name: weekend_model
    path: weekend_model.tf
    type: tensorflow
    where:
      input: is_weekday
      operator: eq
      value: false

Each component needs to have the following properties:

  • name: The name of the component model
  • path: The path to the component model on disk
  • type: The type of the component model.
  • where: The condition at which the component model should be used.

We build the weekend model and save it to disk.

import tensorflow as tf

# Build the weekend model
weekend_model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,), name="distance"),
    tf.keras.layers.Dense(1, name="price")
])

weekend_model.compile(optimizer="adam", loss="mse")

weekend_model.fit(
    x={"distance": tf.convert_to_tensor([10, 20], dtype=tf.float32)},
    y=tf.convert_to_tensor([10, 20], dtype=tf.float32),
    epochs=10
)
weekend_model.save("weekend_model.tf")

We build the weekday model and save it to disk.

# Build the weekday model
weekday_model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,), name="distance"),
    tf.keras.layers.Dense(1, name="price")
])

weekday_model.compile(optimizer="adam", loss="mse")

weekday_model.fit(
    x={"distance": tf.convert_to_tensor([10, 20], dtype=tf.float32)},
    y=tf.convert_to_tensor([5, 10], dtype=tf.float32),
    epochs=10
)

# Save the models
weekday_model.save("weekday_model.tf")

We can now build our composed model from the example yaml spec.

import tensorflow as tf
from model_composer import TensorflowModelComposer

composed_model = TensorflowModelComposer().from_yaml("example.yaml")

assert isinstance(composed_model, tf.keras.Model)

composed_model.save("composed_model.tf")

loaded_model = tf.keras.models.load_model("composed_model.tf")

composed_model.predict({
  "is_weekday": tf.convert_to_tensor([True, False], dtype=tf.bool),
  "distance": tf.convert_to_tensor([10, 20], dtype=tf.float32)
})

Roadmap

  • Support for more ML frameworks:
    • PyTorch
    • Scikit-learn

Metadata

Release files for model-composer 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for model-composer 0.3.0
File Interpreter ABI Platform Actions
model_composer-0.3.0-py3-none-any.whl Python 3 none any Details for model_composer-0.3.0-py3-none-any.whl

Release files / model_composer-0.3.0-py3-none-any.whl

Download URL model_composer-0.3.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
088c80242de963d1f781b34487c727ec546cf648d79ce5ea2ff234c1e50a80a7
BLAKE2b-256 checksum
How to use checksums
f021c9bc04ff94bd1b1473c28deec523154e06621ee036774fbeb26d8b07e6dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.0 CPython/3.8.7 Darwin/22.5.0

Security for model-composer 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page