unionml.model.Model#
- class unionml.model.Model(name='model', init=None, *, dataset, hyperparameter_config=None)#
Initialize a UnionML Model.
The term UnionML Model refers to the specification of a model, which the user defines through the functional entrypoints, e.g.
unionml.model.Model.trainer()
. The term model object is used to refer to some instance of model from a machine learning framework such as the subclasses of theBaseEstimator
class in sklearn,Module
in pytorch, etc.- Parameters
name (
str
) β name of the modelinit (
Union
[Type
,Callable
,None
]) β a class or callable that produces a model object (e.g. an sklearn estimator) when invoked.dataset (
Dataset
) β a UnionML Dataset object to bind to the model.hyperparameter_config (
Optional
[Dict
[str
,Type
]]) βA dictionary mapping hyperparameter names to types. This is used to determine the hyperparameter names and types associated with the model object produced by the
init
argument. For example:>>> { ... "hyperparameter1": int, ... "hyperparameter2": str, ... "hyperparameter3": float, ... }
Methods
Register a function for producing metrics for given model object.
Register a function for initializing a model object.
Load a model object from disk.
Register a function for deserializing a model object to disk.
Generate predictions locally.
Create a Flyte task for generating predictions from a model object.
Create a Flyte prediction workflow using raw features.
Create a Flyte task for generating predictions from a model object.
Create a Flyte prediction workflow using features from the
dataset.reader
as the data source.Register a function that generates predictions from a model object.
Configure the
unionml.Model
for remote backend deployment.Deploy model services to a Flyte backend.
Fetch predictions from a Flyte execution.
Lists all the model versions of this UnionML app, in reverse chronological order.
Load a
ModelArtifact
based on the provided Flyte execution.Generate predictions on a remote Flyte backend.
Train a model object on a remote Flyte backend.
Wait for a
FlyteWorkflowExecution
to complete and returns the execution's output.Save the model object to disk.
Register a function for serializing a model object to disk.
Create a FastAPI serving app.
Train a model object locally
Create a Flyte task for training a model object.
Create a Flyte training workflow for model object training.
Register a function for training a model object.
Attributes
Model artifact associated with the
unionml.Model
.Path to the config file associated with the Flyte backend.
Path to Docker file used to package the UnionML app.
Hyperparameter type of the model object based on the
init
function signature.model_type
- rtype
Type
Name of the prediction workflow used to generate predictions from raw features.
Name of the prediction workflow used to generate predictions from the
dataset.reader
.Docker registry used to push UnionML app.
Name of the training workflow.
Parameters used to create a Flyte workflow for model object training.
- property artifact: Optional[unionml.model.ModelArtifact]#
Model artifact associated with the
unionml.Model
.- Return type
Optional
[ModelArtifact
]
- property config_file: Optional[str]#
Path to the config file associated with the Flyte backend.
- Return type
Optional
[str
]
- property dockerfile: Optional[str]#
Path to Docker file used to package the UnionML app.
- Return type
Optional
[str
]
- evaluator(fn)#
Register a function for producing metrics for given model object.
- property hyperparameter_type: Type#
Hyperparameter type of the model object based on the
init
function signature.- Return type
Type
- init(fn)#
Register a function for initializing a model object.
- load(file, *args, **kwargs)#
Load a model object from disk.
- loader(fn)#
Register a function for deserializing a model object to disk.
- predict(features=None, **reader_kwargs)#
Generate predictions locally.
You can either pass this function raw features via the
features
argument or you can pass in keyword arguments that will be forwarded to theunionml.Dataset.reader()
method as the feature source.- Parameters
features (
Optional
[Any
]) βRaw features that are pre-processed by the :py:class:
unionml.Dataset
methods in the following order:reader_kwargs β keyword arguments that correspond to the
unionml.Dataset.reader()
method signature.
- predict_from_features_task()#
Create a Flyte task for generating predictions from a model object.
This is used in the Flyte workflow produced by
predict_from_features_workflow
.
- predict_from_features_workflow()#
Create a Flyte prediction workflow using raw features.
- property predict_from_features_workflow_name#
Name of the prediction workflow used to generate predictions from raw features.
- predict_task()#
Create a Flyte task for generating predictions from a model object.
This is used in the Flyte workflow produced by
predict_workflow
.
- predict_workflow()#
Create a Flyte prediction workflow using features from the
dataset.reader
as the data source.
- property predict_workflow_name#
Name of the prediction workflow used to generate predictions from the
dataset.reader
.
- predictor(fn=None, **predict_task_kwargs)#
Register a function that generates predictions from a model object.
- property registry: Optional[str]#
Docker registry used to push UnionML app.
- Return type
Optional
[str
]
- remote(registry=None, image_name=None, dockerfile='Dockerfile', config_file=None, project=None, domain=None)#
Configure the
unionml.Model
for remote backend deployment.- Parameters
registry (
Optional
[str
]) β Docker registry used to push UnionML app.image_name (
Optional
[str
]) β image name to give to the Docker image associated with the UnionML app.dockerfile (
str
) β path to the Dockerfile used to package the UnionML app.config_file (
Optional
[str
]) β path to the flytectl config to use for deploying your UnionML app to a Flyte backend.project (
Optional
[str
]) β deploy your app to this Flyte project name.project β deploy your app to this Flyte domain name.
- remote_deploy(app_version=None)#
Deploy model services to a Flyte backend.
- remote_fetch_predictions(execution)#
Fetch predictions from a Flyte execution.
- Parameters
execution (
FlyteWorkflowExecution
) β a Flyte workflow execution, which is the output ofremote_predict(..., wait=False)
.- Return type
Any
- remote_list_model_versions(app_version=None, limit=10)#
Lists all the model versions of this UnionML app, in reverse chronological order.
- Parameters
app_version (
Optional
[str
]) β if provided, lists the model versions associated with this app version. By default, this uses the current git sha of the repo, which versions your UnionML app.limit (
int
) β limit the number results to fetch.
- Return type
List
[str
]
- remote_load(execution)#
Load a
ModelArtifact
based on the provided Flyte execution.- Parameters
execution (
FlyteWorkflowExecution
) β a Flyte workflow execution, which is the output ofremote_train(..., wait=False)
.
- remote_predict(app_version=None, model_version=None, wait=True, *, features=None, **reader_kwargs)#
Generate predictions on a remote Flyte backend.
You can either pass this function raw features via the
features
argument or you can pass in keyword arguments that will be forwarded to theunionml.Dataset.reader()
method as the feature source.- Parameters
app_version (
Optional
[str
]) β if provided, executes a prediction job using the specified UnionML app version. By default, this uses the current git sha of the repo, which versions your UnionML app.model_version (
Optional
[str
]) β if provided, executes a prediction job using the specified model version. By default, this uses the latest Flyte execution id as the model version.wait (
bool
) β if True, this is a synchronous operation, returning aModelArtifact
. Otherwise, this function returns aFlyteWorkflowExecution
.features (
Optional
[Any
]) βRaw features that are pre-processed by the :py:class:
unionml.Dataset
methods in the following order:reader_kwargs β keyword arguments that correspond to the
unionml.Dataset.reader()
method signature.
- Return type
Union
[Any
,FlyteWorkflowExecution
]
- remote_train(app_version=None, wait=True, *, hyperparameters=None, loader_kwargs=None, splitter_kwargs=None, parser_kwargs=None, trainer_kwargs=None, **reader_kwargs)#
Train a model object on a remote Flyte backend.
- Parameters
app_version (
Optional
[str
]) β if provided, executes a training job using the specified UnionML app version. By default, this uses the current git sha of the repo, which versions your UnionML app.wait (
bool
) β if True, this is a synchronous operation, returning aModelArtifact
. Otherwise, this function returns aFlyteWorkflowExecution
.hyperparameters (
Optional
[Dict
[str
,Any
]]) β a dictionary mapping hyperparameter names to values. This is passed into theinit
callable to initialize a model object.loader_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.loader
function. This will override any defaults set in the function definition.splitter_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.splitter
function. This will override any defaults set in the function definition.parser_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.parser
function. This will override any defaults set in the function definition.trainer_kwargs (
Optional
[Dict
[str
,Any
]]) β a dictionary mapping training parameter names to values. There training parameters are determined by the keyword-only arguments of themodel.trainer
function.reader_kwargs β keyword arguments that correspond to the
unionml.Dataset.reader()
method signature.
- Return type
Union
[ModelArtifact
,FlyteWorkflowExecution
]
- remote_wait(execution, **kwargs)#
Wait for a
FlyteWorkflowExecution
to complete and returns the executionβs output.- Return type
Any
- save(file, *args, **kwargs)#
Save the model object to disk.
- saver(fn)#
Register a function for serializing a model object to disk.
- serve(app, remote=False, model_version='latest')#
Create a FastAPI serving app.
- Parameters
app (
FastAPI
) β AFastAPI
app to use for model serving.
- train(hyperparameters=None, loader_kwargs=None, splitter_kwargs=None, parser_kwargs=None, trainer_kwargs=None, **reader_kwargs)#
Train a model object locally
- Parameters
hyperparameters (
Optional
[Dict
[str
,Any
]]) β a dictionary mapping hyperparameter names to values. This is passed into theinit
callable to initialize a model object.loader_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.loader
function. This will override any defaults set in the function definition.splitter_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.splitter
function. This will override any defaults set in the function definition.parser_kwargs (
Optional
[Dict
[str
,Any
]]) β key-word arguments to pass to the registeredunionml.Dataset.parser
function. This will override any defaults set in the function definition.trainer_kwargs (
Optional
[Dict
[str
,Any
]]) β a dictionary mapping training parameter names to values. There training parameters are determined by the keyword-only arguments of themodel.trainer
function.reader_kwargs β keyword arguments that correspond to the
unionml.Dataset.reader()
method signature.
- Return type
Tuple
[Any
,Any
]
- train_task()#
Create a Flyte task for training a model object.
This is used in the Flyte workflow produced by
train_workflow
.
- train_workflow()#
Create a Flyte training workflow for model object training.
- property train_workflow_name#
Name of the training workflow.
- trainer(fn=None, **train_task_kwargs)#
Register a function for training a model object.
- property trainer_params: Dict[str, inspect.Parameter]#
Parameters used to create a Flyte workflow for model object training.
- Return type
Dict
[str
,Parameter
]