Easily compose a model ensemble from your machine learning models
Project description
Model Composer
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
Supported ML libraries
- Tensorflow
Roadmap
- Support for more ML frameworks:
- PyTorch
- Scikit-learn
How to setup
pip install model_composer
Quick start
We define our composed model using a composed model spec which can be serialized in a human-readable file like yaml.
import yaml
example = """
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
"""
with open("example.yaml", "w") as f:
f.write(example)
We build each model separately 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")
# 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 then load the composed model spec and use it to make predictions.
import tensorflow as tf
from model_composer import TensorflowModelComposer
composed_model = TensorflowModelComposer().from_yaml("example.yaml")
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)
})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for model_composer-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c6cf6110a065db2fd58d7d6d29bcc1026788605f3c7a320d81881a40006619c |
|
MD5 | 48f8fe1432e6503192a70b6ea93d60f5 |
|
BLAKE2b-256 | b3adfeba69dcf9bcf2efb603380a1d6e7c7c25a600c3cfa6d3543031a6a48d66 |